1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
|
#!/usr/bin/perl -w
use strict;
use File::Spec;
use lib '.', File::Spec->catdir( File::Spec->curdir, 't', 'lib' );
use Alzabo::Test::Utils;
use Test::More;
my @rdbms_names = Alzabo::Test::Utils->rdbms_names;
unless (@rdbms_names)
{
plan skip_all => 'no test config provided';
exit;
}
my $tests_per_run = 340;
my $test_count = $tests_per_run * @rdbms_names;
my %SINGLE_RDBMS_TESTS = ( mysql => 23,
pg => 11,
);
foreach my $rdbms ( keys %SINGLE_RDBMS_TESTS )
{
next unless grep { $_ eq $rdbms } @rdbms_names;
$test_count += $SINGLE_RDBMS_TESTS{$rdbms};
}
plan tests => $test_count;
Alzabo::Test::Utils->remove_all_schemas;
foreach my $rdbms (@rdbms_names)
{
if ( $rdbms eq 'mysql' )
{
# prevent subroutine redefinition warnings
local $^W = 0;
eval 'use Alzabo::SQLMaker::MySQL qw(:all)';
}
elsif ( $rdbms eq 'pg' )
{
local $^W = 0;
eval 'use Alzabo::SQLMaker::PostgreSQL qw(:all)';
}
Alzabo::Test::Utils->make_schema($rdbms);
run_tests($rdbms);
Alzabo::Test::Utils->remove_schema($rdbms);
}
sub run_tests
{
my $rdbms = shift;
my $config = Alzabo::Test::Utils->test_config_for($rdbms);
my $s = Alzabo::Runtime::Schema->load_from_file( name => $config->{schema_name} );
# tests setting basic parameters and connecting to RDBMS
{
eval_ok( sub { $s->set_user('foo') },
"Set user for schema to foo" );
eval_ok( sub { $s->set_password('foo') },
"Set password for schema to foo" );
eval_ok( sub { $s->set_host('foo') },
"Set host for schema to foo" );
eval_ok( sub { $s->set_port(1234) },
"Set port for schema to 1234" );
$s->$_(undef) foreach qw( set_user set_password set_host set_port );
$s->connect( Alzabo::Test::Utils->connect_params_for($rdbms) );
$s->set_referential_integrity(1);
}
{
my $dbh = $s->driver->handle;
isa_ok( $dbh, ref $s->driver->{dbh},
"Object returned by \$s->driver->handle method" );
eval_ok( sub { $s->driver->handle($dbh) },
"Set \$s->driver->handle" );
}
my $emp_t = $s->table('employee');
my $dep_t = $s->table('department');
my $proj_t = $s->table('project');
my $emp_proj_t = $s->table('employee_project');
my %dep;
eval_ok( sub { $dep{borg} = $dep_t->insert( values => { name => 'borging' } ) },
"Insert borging row into department table" );
is( $dep{borg}->select('name'), 'borging',
"The borg department name should be 'borging'" );
{
my @all = $dep{borg}->select;
is( @all, 3,
"select with no columns should return all the values" );
is( $all[1], 'borging',
"The second value should be the department name" );
my %all = $dep{borg}->select_hash;
is( keys %all, 3,
"select_hash with no columns should return two keys" );
ok( exists $all{department_id},
"The returned hash should have a department_id key" );
ok( exists $all{name},
"The returned hash should have a department_id key" );
is( $all{name}, 'borging',
"The value of the name key be the department name" );
}
$dep{lying} = $dep_t->insert( values => { name => 'lying to the public' } );
my $borg_id = $dep{borg}->select('department_id');
delete $dep{borg};
eval_ok( sub { $dep{borg} = $dep_t->row_by_pk( pk => $borg_id ) },
"Retrieve borg department row via row_by_pk method" );
isa_ok( $dep{borg}, 'Alzabo::Runtime::Row',
"Borg department" );
is( $dep{borg}->select('name'), 'borging',
"Department's name should be 'borging'" );
eval { $dep_t->insert( values => { name => 'will break',
manager_id => 1 } ); };
my $e = $@;
isa_ok( $e, 'Alzabo::Exception::ReferentialIntegrity',
"Exception thrown from attempt to insert a non-existent manager_id into department" );
my %emp;
eval_ok( sub { $emp{bill} = $emp_t->insert( values => { name => 'Big Bill',
dep_id => $borg_id,
smell => 'robotic',
cash => 20.2,
} ) },
"Insert Big Bill into employee table" );
my %data = $emp{bill}->select_hash( 'name', 'smell' );
is( $data{name}, 'Big Bill',
"select_hash - check name key" );
is( $data{smell}, 'robotic',
"select_hash - check smell key" );
is( $emp{bill}->is_live, 1,
"->is_live should be true for real row" );
eval { $emp_t->insert( values => { name => undef,
dep_id => $borg_id,
smell => 'robotic',
cash => 20.2,
} ); };
$e = $@;
isa_ok( $e, 'Alzabo::Exception::NotNullable',
"Exception thrown from inserting a non-nullable column as NULL" );
is( $e->table_name, 'employee',
"NotNullable exceptions contain table name" );
is( $e->schema_name, $config->{schema_name},
"NotNullable exceptions contain schema name" );
{
my $new_emp;
eval_ok( sub { $new_emp = $emp_t->insert( values => { name => 'asfalksf',
dep_id => $borg_id,
smell => undef,
cash => 20.2,
} ) },
"Inserting a NULL into a non-nullable column that has a default should not produce an exception" );
eval_ok( sub { $new_emp->delete },
"Delete a just-created employee" );
}
eval { $emp_t->insert( values => { name => 'YetAnotherTest',
dep_id => undef,
cash => 1.1,
} ) };
$e = $@;
isa_ok( $e, 'Alzabo::Exception::Params',
"Exception thrown from attempt to insert a NULL into dep_id for an employee" );
eval { $emp{bill}->update( dep_id => undef ) };
$e = $@;
isa_ok( $e, 'Alzabo::Exception::Params',
"Exception thrown from attempt to update dep_id to NULL for an employee" );
{
my $updated = $emp{bill}->update( cash => undef, smell => 'hello!' );
ok( $updated, 'update() did change values' );
ok( ! defined $emp{bill}->select('cash'),
"Bill has no cash" );
}
{
my $updated = $emp{bill}->update( cash => undef, smell => 'hello!' );
ok( ! $updated, 'update() did not change values' );
}
ok( $emp{bill}->select('smell') eq 'hello!',
"smell for bill should be 'hello!'" );
eval { $emp{bill}->update( name => undef ) };
$e = $@;
isa_ok( $e, 'Alzabo::Exception::NotNullable',
"Exception thrown from attempt to update a non-nullable column to NULL" );
eval_ok( sub { $dep{borg}->update( manager_id => $emp{bill}->select('employee_id') ) },
"Set manager_id column for borg department" );
eval_ok( sub { $emp{2} = $emp_t->insert( values =>
{ name => 'unit 2',
smell => 'good',
dep_id => $dep{lying}->select('department_id') } ) },
"Create employee 'unit 2'" );
my $emp2_id = $emp{2}->select('employee_id');
delete $emp{2};
my $cursor;
my $x = 0;
eval_ok( sub { $cursor =
$emp_t->rows_where
( where => [ $emp_t->column('employee_id'), '=', $emp2_id ] );
while ( my $row = $cursor->next )
{
$x++;
$emp{2} = $row;
}
},
"Retrieve 'unit 2' employee via rows_where method and cursor" );
is( $x, 1,
"Check count of rows found where employee_id == $emp2_id" );
is( $cursor->count, 1,
"Make sure cursor's count() is accurate" );
is( $emp{2}->select('name'), 'unit 2',
"Check that row found has name of 'unit 2'" );
{
my $row;
eval_ok( sub { $row =
$emp_t->one_row
( where =>
[ $emp_t->column('employee_id'), '=', $emp2_id ] ) },
"Retrieve 'unit 2' employee via one_row method" );
is( $row->select('name'), 'unit 2',
"Check that the single row returned has the name 'unit 2'" );
}
{
my $row;
eval_ok( sub { $row =
$emp_t->one_row
( where =>
[ $emp_t->column('employee_id'), '=', $emp2_id ],
quote_identifiers => 1,
) },
"Retrieve 'unit 2' employee via one_row method with quote_identifiers" );
is( $row->select('name'), 'unit 2',
"Check that the single row returned has the name 'unit 2'" );
}
my %proj;
$proj{extend} = $proj_t->insert( values => { name => 'Extend',
department_id => $dep{borg}->select('department_id') } );
$proj{embrace} = $proj_t->insert( values => { name => 'Embrace',
department_id => $dep{borg}->select('department_id') } );
$emp_proj_t->insert( values => { employee_id => $emp{bill}->select('employee_id'),
project_id => $proj{extend}->select('project_id') } );
$emp_proj_t->insert( values => { employee_id => $emp{bill}->select('employee_id'),
project_id => $proj{embrace}->select('project_id') } );
my $fk = $emp_t->foreign_keys_by_table($emp_proj_t);
my @emp_proj;
my @cursor_counts;
eval_ok( sub { $cursor = $emp{bill}->rows_by_foreign_key( foreign_key => $fk );
while ( my $row = $cursor->next )
{
push @emp_proj, $row;
push @cursor_counts, $cursor->count;
} },
"Fetch rows via ->rows_by_foreign_key method (expect cursor)" );
is( scalar @emp_proj, 2,
"Check that only two rows were returned" );
is( $emp_proj[0]->select('employee_id'), $emp{bill}->select('employee_id'),
"Check that employee_id in employee_project is same as bill's" );
is( $emp_proj[0]->select('project_id'), $proj{extend}->select('project_id'),
"Check that project_id in employee_project is same as extend project" );
foreach (1..2)
{
is( $cursor_counts[$_ - 1], $_,
"cursor->count should be 1..2" );
}
my $emp_proj = $emp_proj[0];
$fk = $emp_proj_t->foreign_keys_by_table($emp_t);
my $emp;
eval_ok( sub { $emp = $emp_proj->rows_by_foreign_key( foreign_key => $fk ) },
"Fetch rows via ->rows_by_foreign_key method (expect row)" );
is( $emp->select('employee_id'), $emp_proj->select('employee_id'),
"The returned row should have bill's employee_id" );
$x = 0;
my @rows;
eval_ok( sub { $cursor = $emp_t->all_rows;
$x++ while $cursor->next
},
"Fetch all rows from employee table" );
is( $x, 2,
"Only 2 rows should be found" );
$cursor->reset;
my $count = $cursor->all_rows;
is( $x, 2,
"Only 2 rows should be found after cursor reset" );
{
my $cursor;
eval_ok( sub { $cursor =
$s->join( join => [ $emp_t, $emp_proj_t, $proj_t ],
where =>
[ $emp_t->column('employee_id'), '=',
$emp{bill}->select('employee_id') ],
order_by => $proj_t->column('project_id'),
quote_identifiers => 1,
) },
"Join employee, employee_project, and project tables where employee_id = bill's employee id with quote_identifiers" );
my @rows = $cursor->next;
is( scalar @rows, 3,
"3 rows per cursor ->next call" );
is( $rows[0]->table->name, 'employee',
"First row is from employee table" );
is( $rows[1]->table->name, 'employee_project',
"Second row is from employee_project table" );
is( $rows[2]->table->name, 'project',
"Third row is from project table" );
my $first_proj_id = $rows[2]->select('project_id');
@rows = $cursor->next;
my $second_proj_id = $rows[2]->select('project_id');
ok( $first_proj_id < $second_proj_id,
"Order by clause should cause project rows to come back" .
" in ascending order of project id" );
}
{
my $cursor;
eval_ok( sub { $cursor =
$s->join( join => [ $emp_t, $emp_proj_t, $proj_t ],
where =>
[ [ $proj_t->column('project_id'), '=',
$proj{extend}->select('project_id') ],
'or',
[ $proj_t->column('project_id'), '=',
$proj{embrace}->select('project_id') ],
],
order_by => $proj_t->column('project_id') ) },
"Join employee, employee_project, and project tables with OR in where clause" );
1 while $cursor->next;
is( $cursor->count, 2,
"join with OR in where clause should return two sets of rows" );
}
# Alias code
{
my $e_alias;
eval_ok( sub { $e_alias = $emp_t->alias },
"Create an alias object for the employee table" );
my $p_alias;
eval_ok( sub { $p_alias = $proj_t->alias },
"Create an alias object for the project table" );
eval_ok( sub { $cursor =
$s->join( join => [ $e_alias, $emp_proj_t, $p_alias ],
where => [ $e_alias->column('employee_id'), '=', 1 ],
order_by => $p_alias->column('project_id'),
) },
"Join employee, employee_project, and project tables where" .
" employee_id = 1 using aliases" );
my @rows = $cursor->next;
is( scalar @rows, 3,
"3 rows per cursor ->next call" );
is( $rows[0]->table->name, 'employee',
"First row is from employee table" );
is( $rows[1]->table->name, 'employee_project',
"Second row is from employee_project table" );
is( $rows[2]->table->name, 'project',
"Third row is from project table" );
}
# Alias code & multiple joins to the same table
{
my $p_alias = $proj_t->alias;
eval_ok( sub { $cursor = $s->join( select => [ $p_alias, $proj_t ],
join => [ $p_alias, $emp_proj_t, $proj_t ],
where => [ [ $p_alias->column('project_id'), '=', 1 ],
[ $proj_t->column('project_id'), '=', 1 ] ],
) },
"Join employee_project and project table (twice) using aliases" );
my @rows = $cursor->next;
is( scalar @rows, 2,
"2 rows per cursor ->next call" );
is( $rows[0]->table->name, 'project',
"First row is from project table" );
is( $rows[1]->table->name, 'project',
"Second row is from project table" );
is( $rows[0]->table, $rows[1]->table,
"The two rows should share the same table object (the alias should be gone at this point)" );
}
{
my @rows;
eval_ok( sub { @rows = $s->one_row( tables => [ $emp_t, $emp_proj_t, $proj_t ],
where => [ $emp_t->column('employee_id'), '=', 1 ],
order_by => $proj_t->column('project_id') ) },
"Join employee, employee_project, and project tables where employee_id = 1 using one_row method" );
is( $rows[0]->table->name, 'employee',
"First row is from employee table" );
is( $rows[1]->table->name, 'employee_project',
"Second row is from employee_project table" );
is( $rows[2]->table->name, 'project',
"Third row is from project table" );
}
$cursor = $s->join( join => [ $emp_t, $emp_proj_t, $proj_t ],
where => [ $emp_t->column('employee_id'), '=', 1 ],
order_by => [ $proj_t->column('project_id'), 'desc' ] );
@rows = $cursor->next;
my $first_proj_id = $rows[2]->select('project_id');
@rows = $cursor->next;
my $second_proj_id = $rows[2]->select('project_id');
ok( $first_proj_id > $second_proj_id,
"Order by clause should cause project rows to come back in descending order of project id" );
$cursor = $s->join( join => [ $emp_t, $emp_proj_t, $proj_t ],
where => [ $emp_t->column('employee_id'), '=', 1 ],
order_by => [ $proj_t->column('project_id'), 'desc' ] );
@rows = $cursor->next;
$first_proj_id = $rows[2]->select('project_id');
@rows = $cursor->next;
$second_proj_id = $rows[2]->select('project_id');
ok( $first_proj_id > $second_proj_id,
"Order by clause (alternate form) should cause project rows to come back in descending order of project id" );
eval_ok( sub { $cursor = $s->join( select => [ $emp_t, $emp_proj_t, $proj_t ],
join => [ [ $emp_t, $emp_proj_t ],
[ $emp_proj_t, $proj_t ] ],
where => [ $emp_t->column('employee_id'), '=', 1 ] ) },
"Join with join as arrayref of arrayrefs" );
@rows = $cursor->next;
is( scalar @rows, 3,
"3 rows per cursor ->next call" );
is( $rows[0]->table->name, 'employee',
"First row is from employee table" );
is( $rows[1]->table->name, 'employee_project',
"Second row is from employee_project table" );
is( $rows[2]->table->name, 'project',
"Third row is from project table" );
{
my $cursor;
eval_ok( sub { $cursor = $s->join( join => [ [ $emp_t, $emp_proj_t ],
[ $emp_proj_t, $proj_t ] ],
where => [ $emp_t->column('employee_id'), '=', 1 ] ) },
"Same join with no select parameter" );
my @rows = $cursor->next;
@rows = sort { $a->table->name cmp $b->table->name } @rows;
is( scalar @rows, 3,
"3 rows per cursor ->next call" );
is( ( grep { $_->table->name eq 'employee' } @rows ), 1,
"First row is from employee table" );
is( ( grep { $_->table->name eq 'employee_project' } @rows ), 1,
"Second row is from employee_project table" );
is( ( grep { $_->table->name eq 'project' } @rows ), 1,
"Third row is from project table" );
}
eval { $s->join( select => [ $emp_t, $emp_proj_t, $proj_t ],
join => [ [ $emp_t, $emp_proj_t ],
[ $emp_proj_t, $proj_t ],
[ $s->tables( 'outer_1', 'outer_2' ) ] ],
where => [ $emp_t->column('employee_id'), '=', 1 ] ) };
$e = $@;
isa_ok( $e, 'Alzabo::Exception::Logic',
"Exception thrown from join with table map that does not connect" );
eval_ok( sub { @rows = $s->join( join => $emp_t,
where => [ $emp_t->column('employee_id'), '=', 1 ] )->all_rows },
"Join with a single table" );
is( @rows, 1,
"Only one row should be returned" );
is( $rows[0]->select('employee_id'), 1,
"Returned employee should be employee number one" );
{
$s->table('outer_2')->insert( values => { outer_2_name => 'will match something',
outer_2_pk => 1 },
);
$s->table('outer_2')->insert( values => { outer_2_name => 'will match nothing',
outer_2_pk => 99 },
);
$s->table('outer_1')->insert( values => { outer_1_name => 'test1 (has matching join row)',
outer_2_pk => 1 },
);
$s->table('outer_1')->insert( values => { outer_1_name => 'test2 (has no matching join row)',
outer_2_pk => undef },
);
{
my $cursor;
eval_ok( sub { $cursor =
$s->join
( select => [ $s->tables( 'outer_1', 'outer_2' ) ],
join =>
[ left_outer_join =>
$s->tables( 'outer_1', 'outer_2' ) ]
) },
"Do a left outer join" );
my @sets = $cursor->all_rows;
is( scalar @sets, 2,
"Left outer join should return 2 sets of rows" );
# re-order so that the set with 2 valid rows is always first
unless ( defined $sets[0]->[1] )
{
my $set = shift @sets;
push @sets, $set;
}
is( $sets[0]->[0]->select('outer_1_name'), 'test1 (has matching join row)',
"The first row in the first set should have the name 'test1 (has matching join row)'" );
is( $sets[0]->[1]->select('outer_2_name'), 'will match something',
"The second row in the first set should have the name 'will match something'" );
is( $sets[1]->[0]->select('outer_1_name'), 'test2 (has no matching join row)',
"The first row in the second set should have the name 'test12 (has no matching join row)'" );
ok( ! defined $sets[1]->[1],
"The second row in the second set should not be defined" );
}
{
my $cursor;
eval_ok( sub { $cursor =
$s->join
( select => [ $s->tables( 'outer_1', 'outer_2' ) ],
join =>
[ [ left_outer_join =>
$s->tables( 'outer_1', 'outer_2' ),
[ $s->table('outer_2')->column( 'outer_2_pk' ),
'!=', 1 ],
] ],
order_by =>
$s->table('outer_1')->column('outer_1_name')
) },
"Do a left outer join" );
my @sets = $cursor->all_rows;
is( scalar @sets, 2,
"Left outer join should return 2 sets of rows" );
is( $sets[0]->[0]->select('outer_1_name'), 'test1 (has matching join row)',
"The first row in the first set should have the name 'test1 (has matching join row)'" );
is( $sets[0]->[1], undef,
"The second row in the first set should be undef" );
is( $sets[1]->[0]->select('outer_1_name'), 'test2 (has no matching join row)',
"The first row in the second set should have the name 'test1 (has matching join row)'" );
is( $sets[1]->[1], undef,
"The second row in the second set should be undef" );
}
{
my $fk = $s->table('outer_1')->foreign_keys_by_table( $s->table('outer_2') );
my $cursor;
eval_ok( sub { $cursor =
$s->join
( select => [ $s->tables( 'outer_1', 'outer_2' ) ],
join =>
[ [ left_outer_join =>
$s->tables( 'outer_1', 'outer_2' ),
$fk,
[ $s->table('outer_2')->column( 'outer_2_pk' ),
'!=', 1 ],
] ],
order_by =>
$s->table('outer_1')->column('outer_1_name')
) },
"Do a left outer join" );
my @sets = $cursor->all_rows;
is( scalar @sets, 2,
"Left outer join should return 2 sets of rows" );
is( $sets[0]->[0]->select('outer_1_name'), 'test1 (has matching join row)',
"The first row in the first set should have the name 'test1 (has matching join row)'" );
is( $sets[0]->[1], undef,
"The second row in the first set should be undef" );
is( $sets[1]->[0]->select('outer_1_name'), 'test2 (has no matching join row)',
"The first row in the second set should have the name 'test1 (has matching join row)'" );
is( $sets[1]->[1], undef,
"The second row in the second set should be undef" );
}
{
my $cursor;
eval_ok( sub { $cursor =
$s->join
( select => [ $s->tables( 'outer_1', 'outer_2' ) ],
join =>
[ [ right_outer_join =>
$s->tables( 'outer_1', 'outer_2' ) ] ]
) },
"Attempt a right outer join" );
my @sets = $cursor->all_rows;
is( scalar @sets, 2,
"Right outer join should return 2 sets of rows" );
# re-order so that the set with 2 valid rows is always first
unless ( defined $sets[0]->[0] )
{
my $set = shift @sets;
push @sets, $set;
}
is( $sets[0]->[0]->select('outer_1_name'), 'test1 (has matching join row)',
"The first row in the first set should have the name 'test1 (has matching join row)'" );
is( $sets[0]->[1]->select('outer_2_name'), 'will match something',
"The second row in the first set should have the name 'will match something'" );
ok( ! defined $sets[1]->[0],
"The first row in the second set should not be defined" );
is( $sets[1]->[1]->select('outer_2_name'), 'will match nothing',
"The second row in the second set should have the name 'test12 (has no matching join row)'" );
}
{
my $cursor;
# do the same join, but with specified foreign key
my $fk = $s->table('outer_1')->foreign_keys_by_table( $s->table('outer_2') );
eval_ok( sub { $cursor =
$s->join
( select => [ $s->tables( 'outer_1', 'outer_2' ) ],
join =>
[ [ right_outer_join =>
$s->tables( 'outer_1', 'outer_2' ), $fk ] ]
) },
"Attempt a right outer join, with explicit foreign key" );
my @sets = $cursor->all_rows;
is( scalar @sets, 2,
"Right outer join should return 2 sets of rows" );
# re-order so that the set with 2 valid rows is always first
unless ( defined $sets[0]->[0] )
{
my $set = shift @sets;
push @sets, $set;
}
is( $sets[0]->[0]->select('outer_1_name'), 'test1 (has matching join row)',
"The first row in the first set should have the name 'test1 (has matching join row)'" );
is( $sets[0]->[1]->select('outer_2_name'), 'will match something',
"The second row in the first set should have the name 'will match something'" );
ok( ! defined $sets[1]->[0],
"The first row in the second set should not be defined" );
is( $sets[1]->[1]->select('outer_2_name'), 'will match nothing',
"The second row in the second set should have the name 'test12 (has no matching join row)'" );
}
}
my $id = $emp{bill}->select('employee_id');
$emp{bill}->delete;
eval { $emp{bill}->select('name'); };
$e = $@;
isa_ok( $e, 'Alzabo::Exception::NoSuchRow',
"Exception thrown from attempt to select from deleted row object" );
{
my $row =
$emp_proj_t->row_by_pk
( pk =>
{ employee_id => $id,
project_id => $proj{extend}->select('project_id') } );
is( $row, undef,
"make sure row was deleted by cascading delte" );
}
is( $dep{borg}->select('manager_id'), 1,
"The manager_id for the borg department will be 1 because the object does not the database was changed" );
$dep{borg}->refresh;
my $dep_id = $dep{borg}->select('department_id');
$emp_t->insert( values => { name => 'bob', smell => 'awful', dep_id => $dep_id } );
$emp_t->insert( values => { name => 'rachel', smell => 'horrid', dep_id => $dep_id } );
$emp_t->insert( values => { name => 'al', smell => 'bad', dep_id => $dep_id } );
{
my @emps;
eval_ok ( sub { @emps = $emp_t->all_rows( order_by =>
[ $emp_t->column('name') ] )->all_rows },
"Select all employee rows with arrayref to order_by" );
is( scalar @emps, 4,
"There should be 4 rows in the employee table" );
is( $emps[0]->select('name'), 'al',
"First row name should be al" );
is( $emps[1]->select('name'), 'bob',
"Second row name should be bob" );
is( $emps[2]->select('name'), 'rachel',
"Third row name should be rachel" );
is( $emps[3]->select('name'), 'unit 2',
"Fourth row name should be 'unit 2'" );
}
{
my @emps;
eval_ok ( sub { @emps = $emp_t->all_rows( order_by =>
[ $emp_t->column('name') ],
quote_identifiers => 1,
)->all_rows },
"Select all employee rows with arrayref to order_by with quote_identifiers" );
is( scalar @emps, 4,
"There should be 4 rows in the employee table" );
is( $emps[0]->select('name'), 'al',
"First row name should be al" );
is( $emps[1]->select('name'), 'bob',
"Second row name should be bob" );
is( $emps[2]->select('name'), 'rachel',
"Third row name should be rachel" );
is( $emps[3]->select('name'), 'unit 2',
"Fourth row name should be 'unit 2'" );
}
{
my @emps;
eval_ok( sub { @emps = $emp_t->all_rows( order_by => $emp_t->column('name') )->all_rows },
"Select all employee rows with column obj to order_by" );
is( scalar @emps, 4,
"There should be 4 rows in the employee table" );
is( $emps[0]->select('name'), 'al',
"First row name should be al" );
is( $emps[1]->select('name'), 'bob',
"Second row name should be bob" );
is( $emps[2]->select('name'), 'rachel',
"Third row name should be rachel" );
is( $emps[3]->select('name'), 'unit 2',
"Fourth row name should be 'unit 2'" );
}
{
my @emps;
eval_ok( sub { @emps = $emp_t->all_rows( order_by => [ $emp_t->column('name') ] )->all_rows },
"Select all employee rows with arrayref to order_by" );
is( scalar @emps, 4,
"There should be 4 rows in the employee table" );
is( $emps[0]->select('name'), 'al',
"First row name should be al" );
is( $emps[1]->select('name'), 'bob',
"Second row name should be bob" );
is( $emps[2]->select('name'), 'rachel',
"Third row name should be rachel" );
is( $emps[3]->select('name'), 'unit 2',
"Fourth row name should be 'unit 2'" );
}
{
my @emps;
eval_ok( sub { @emps = $emp_t->all_rows( order_by =>
[ $emp_t->column('smell') ] )->all_rows },
"Select all employee rows with arrayref to order_by (by smell)" );
is( scalar @emps, 4,
"There should be 4 rows in the employee table" );
is( $emps[0]->select('name'), 'bob',
"First row name should be bob" );
is( $emps[1]->select('name'), 'al',
"Second row name should be al" );
is( $emps[2]->select('name'), 'unit 2',
"Third row name should be 'unit 2'" );
is( $emps[3]->select('name'), 'rachel',
"Fourth row name should be rachel" );
}
{
my @emps;
eval_ok( sub { @emps = $emp_t->all_rows( order_by =>
[ $emp_t->column('smell'), 'desc' ] )->all_rows },
"Select all employee rows order by smell (descending)" );
is( $emps[0]->select('name'), 'rachel',
"First row name should be rachel" );
is( $emps[1]->select('name'), 'unit 2',
"Second row name should be 'unit 2'" );
is( $emps[2]->select('name'), 'al',
"Third row name should be al" );
is( $emps[3]->select('name'), 'bob',
"Fourth row name should be bob" );
}
eval_ok( sub { $count = $emp_t->row_count },
"Call row_count for employee table" );
is( $count, 4,
"The count should be 4" );
eval_ok( sub { $count = $emp_t->function( select => COUNT( $emp_t->column('employee_id') ) ) },
"Get row count via ->function method" );
is( $count, 4,
"There should still be just 4 rows" );
{
my $one;
eval_ok( sub { $one = $emp_t->function( select => 1 ) },
"Get '1' via ->function method" );
is( $one, 1,
"Getting '1' via ->function should return 1" );
}
{
my $statement;
eval_ok( sub { $statement = $emp_t->select( select => COUNT( $emp_t->column('employee_id') ) ) },
"Get row count via even spiffier new ->select method" );
isa_ok( $statement, 'Alzabo::DriverStatement',
"Return value from Table->select method" );
$count = $statement->next;
is( $count, 4,
"There should still be just 4 rows" );
}
{
my $st;
eval_ok( sub { $st = $emp_t->select( select => 1 ) },
"Get '1' via ->select method" );
is( $st->next, 1,
"Getting '1' via ->select should return 1" );
}
{
my @emps;
eval_ok( sub { @emps = $emp_t->all_rows( order_by =>
[ $emp_t->column('smell'), 'desc' ],
limit => 2 )->all_rows },
"Get all employee rows with ORDER BY and LIMIT" );
is( scalar @emps, 2,
"This should only return 2 rows" );
is( $emps[0]->select('name'), 'rachel',
"First row should be rachel" );
is( $emps[1]->select('name'), 'unit 2',
"Second row is 'unit 2'" );
}
{
my @emps;
eval_ok( sub { @emps = $emp_t->all_rows( order_by =>
[ $emp_t->column('smell'), 'desc' ],
limit => [2, 2] )->all_rows },
"Get all employee rows with ORDER BY and LIMIT (with offset)" );
is( scalar @emps, 2,
"This should only return 2 rows" );
is( $emps[0]->select('name'), 'al',
"First row should be al" );
is( $emps[1]->select('name'), 'bob',
"Second row is bob" );
}
$emp_t->set_prefetch( $emp_t->columns( qw( name smell ) ) );
my @p = $emp_t->prefetch;
is( scalar @p, 2,
"Prefetch method should return 2 column names" );
is( scalar ( grep { $_ eq 'name' } @p ), 1,
"One column should be 'name'" );
is( scalar ( grep { $_ eq 'smell' } @p ), 1,
"And the other should be 'smell'" );
is( $emp_t->row_count, 4,
"employee table should have 4 rows" );
{
my @emps = $emp_t->all_rows( order_by =>
[ $emp_t->column('smell'), 'desc' ],
limit => [2, 2] )->all_rows;
my $smell = $emps[0]->select('smell');
is( $emp_t->row_count( where => [ $emp_t->column('smell'), '=', $smell ] ), 1,
"Call row_count method with where parameter." );
$emps[0]->delete;
eval { $emps[0]->update( smell => 'kaboom' ); };
$e = $@;
isa_ok( $e, 'Alzabo::Exception::NoSuchRow',
"Exception thrown from attempt to update a deleted row" );
my $row_id = $emps[1]->id_as_string;
my $row;
eval_ok( sub { $row = $emp_t->row_by_id( row_id => $row_id ) },
"Fetch a row via the ->row_by_id method" );
is( $row->id_as_string, $emps[1]->id_as_string,
"Row retrieved via the ->row_by_id method should be the same as the row whose id was used" );
}
$emp_t->insert( values => { employee_id => 9000,
name => 'bob9000',
smell => 'a',
dep_id => $dep_id } );
$emp_t->insert( values => { employee_id => 9001,
name => 'bob9001',
smell => 'b',
dep_id => $dep_id } );
$emp_t->insert( values => { employee_id => 9002,
name => 'bob9002',
smell => 'c',
dep_id => $dep_id } );
my $eid_c = $emp_t->column('employee_id');
{
my @emps = $emp_t->rows_where( where => [ [ $eid_c, '=', 9000 ],
'or',
[ $eid_c, '=', 9002 ] ] )->all_rows;
@emps = sort { $a->select('employee_id') <=> $b->select('employee_id') } @emps;
is( @emps, 2,
"Do a query with 'or' and count the rows" );
is( $emps[0]->select('employee_id'), 9000,
"First row returned should be employee id 9000" );
is( $emps[1]->select('employee_id'), 9002,
"Second row returned should be employee id 9002" );
}
{
my @emps = $emp_t->rows_where( where => [ [ $emp_t->column('smell'), '!=', 'c' ],
'and',
(
'(',
[ $eid_c, '=', 9000 ],
'or',
[ $eid_c, '=', 9002 ],
')',
),
] )->all_rows;
is( @emps, 1,
"Do another complex query with 'or' and subgroups" );
is( $emps[0]->select('employee_id'), 9000,
"The row returned should be employee id 9000" );
}
{
my @emps = $emp_t->rows_where( where => [ (
'(',
[ $eid_c, '=', 9000 ],
'and',
[ $eid_c, '=', 9000 ],
')',
),
'or',
(
'(',
[ $eid_c, '=', 9000 ],
'and',
[ $eid_c, '=', 9000 ],
')',
),
] )->all_rows;
is( @emps, 1,
"Do another complex query with 'or', 'and' and subgroups" );
is( $emps[0]->select('employee_id'), 9000,
"The row returned should be employee id 9000" );
}
{
my @emps = $emp_t->rows_where( where => [ $eid_c, 'between', 9000, 9002 ] )->all_rows;
@emps = sort { $a->select('employee_id') <=> $b->select('employee_id') } @emps;
is( @emps, 3,
"Select using between should return 3 rows" );
is( $emps[0]->select('employee_id'), 9000,
"First row returned should be employee id 9000" );
is( $emps[1]->select('employee_id'), 9001,
"Second row returned should be employee id 9001" );
is( $emps[2]->select('employee_id'), 9002,
"Third row returned should be employee id 9002" );
}
{
my @emps;
eval_ok( sub { @emps = $emp_t->rows_where( where => [ '(', '(',
[ $eid_c, '=', 9000 ],
')', ')'
] )->all_rows },
"Nested subgroups should be allowed" );
is( @emps, 1,
"Query with nested subgroups should return 1 row" );
is( $emps[0]->select('employee_id'), 9000,
"The row returned should be employee id 9000" );
}
$emp_t->insert( values => { name => 'Smelly',
smell => 'a',
dep_id => $dep_id,
} );
{
my @emps = eval { $emp_t->rows_where( where => [ LENGTH( $emp_t->column('smell') ), '=', 1 ] )->all_rows };
is( @emps, 4,
"There should be only 4 employees where the length of the smell column is 1" );
}
{
my @emps;
eval_ok( sub { @emps = $emp_t->rows_where( where => [ LENGTH( $emp_t->column('smell') ), '=', 1 ],
limit => 2 )->all_rows },
"Select all employee rows with WHERE and LIMIT" );
is( scalar @emps, 2,
"Limit should cause only two employee rows to be returned" );
}
{
my @emps;
eval_ok( sub { @emps = $emp_t->rows_where( where => [ LENGTH( $emp_t->column('smell') ), '=', 1 ],
order_by => $emp_t->column('smell'),
limit => 2 )->all_rows },
"Select all employee rows with WHERE, ORDER BY, and LIMIT" );
is( scalar @emps, 2,
"Limit should cause only two employee rows to be returned (again)" );
}
{
my @emps;
eval_ok( sub { @emps = $emp_t->rows_where( where => [ '(',
[ $emp_t->column('employee_id'), '=', 9000 ],
')',
],
order_by => $emp_t->column('employee_id') )->all_rows },
"Query with subgroup followed by order by" );
is( @emps, 1,
"Query with subgroup followed by order by should return 1 row" );
is( $emps[0]->select('employee_id'), 9000,
"The row returned should be employee id 9000" );
}
my @smells = $emp_t->function( select => [ $emp_t->column('smell'), COUNT( $emp_t->column('smell') ) ],
group_by => $emp_t->column('smell') );
# map smell to count
my %smells = map { $_->[0] => $_->[1] } @smells;
is( @smells, 6,
"Query with group by should return 6 values" );
is( $smells{a}, 2,
"Check count of smell = 'a'" );
is( $smells{b}, 1,
"Check count of smell = 'b'" );
is( $smells{c}, 1,
"Check count of smell = 'c'" );
is( $smells{awful}, 1,
"Check count of smell = 'awful'" );
is( $smells{good}, 1,
"Check count of smell = 'good'" );
is( $smells{horrid}, 1,
"Check count of smell = 'horrid'" );
{
my $statement = $emp_t->select( select => [ $emp_t->column('smell'), COUNT( $emp_t->column('smell') ) ],
group_by => $emp_t->column('smell') );
my @smells = $statement->all_rows;
# map smell to count
%smells = map { $_->[0] => $_->[1] } @smells;
is( @smells, 6,
"Query with group by should return 6 values - via ->select" );
is( $smells{a}, 2,
"Check count of smell = 'a' - via ->select" );
is( $smells{b}, 1,
"Check count of smell = 'b' - via ->select" );
is( $smells{c}, 1,
"Check count of smell = 'c' - via ->select" );
is( $smells{awful}, 1,
"Check count of smell = 'awful' - via ->select" );
is( $smells{good}, 1,
"Check count of smell = 'good' - via ->select" );
is( $smells{horrid}, 1,
"Check count of smell = 'horrid' - via ->select" );
}
@rows = $emp_t->function( select => $emp_t->column('smell'),
where => [ LENGTH( $emp_t->column('smell') ), '=', 1 ],
order_by => $emp_t->column('smell') );
is( @rows, 4,
"There should only be four rows which have a single character smell" );
is( $rows[0], 'a',
"First smell should be 'a'" );
is( $rows[1], 'a',
"Second smell should be 'a'" );
is( $rows[2], 'b',
"Third smell should be 'b'" );
is( $rows[3], 'c',
"Fourth smell should be 'c'" );
{
my $statement = $emp_t->select( select => $emp_t->column('smell'),
where => [ LENGTH( $emp_t->column('smell') ), '=', 1 ],
order_by => $emp_t->column('smell') );
my @rows = $statement->all_rows;
is( @rows, 4,
"There should only be four rows which have a single character smell - via ->select" );
is( $rows[0], 'a',
"First smell should be 'a' - via ->select" );
is( $rows[1], 'a',
"Second smell should be 'a' - via ->select" );
is( $rows[2], 'b',
"Third smell should be 'b' - via ->select" );
is( $rows[3], 'c',
"Fourth smell should be 'c' - via ->select" );
}
@rows = $emp_t->function( select => $emp_t->column('smell'),
where => [ LENGTH( $emp_t->column('smell') ), '=', 1 ],
order_by => $emp_t->column('smell'),
limit => 2,
);
is( @rows, 2,
"There should only be two rows which have a single character smell - with limit" );
is( $rows[0], 'a',
"First smell should be 'a' - with limit" );
is( $rows[1], 'a',
"Second smell should be 'a' - with limit" );
{
my $statement = $emp_t->select( select => $emp_t->column('smell'),
where => [ LENGTH( $emp_t->column('smell') ), '=', 1 ],
order_by => $emp_t->column('smell'),
limit => 2,
);
my @rows = $statement->all_rows;
is( @rows, 2,
"There should only be two rows which have a single character smell - with limit via ->select" );
is( $rows[0], 'a',
"First smell should be 'a' - with limit via ->select" );
is( $rows[1], 'a',
"Second smell should be 'a' - with limit via ->select" );
}
my $extend_id = $proj{extend}->select('project_id');
my $embrace_id = $proj{embrace}->select('project_id');
foreach ( [ 9000, $extend_id ], [ 9000, $embrace_id ],
[ 9001, $extend_id ], [ 9002, $extend_id ] )
{
$emp_proj_t->insert( values => { employee_id => $_->[0],
project_id => $_->[1] } );
}
# find staffed projects
@rows = $s->function( select => [ $proj_t->column('name'),
COUNT( $proj_t->column('name') ) ],
join => [ $emp_proj_t, $proj_t ],
group_by => $proj_t->column('name') );
is( @rows, 2,
"Only two projects should be returned from schema->function" );
is( $rows[0][0], 'Embrace',
"First project should be Embrace" );
is( $rows[1][0], 'Extend',
"Second project should be Extend" );
is( $rows[0][1], 1,
"First project should have 1 employee" );
is( $rows[1][1], 3,
"Second project should have 3 employees" );
{
my $statement = $s->select( select => [ $proj_t->column('name'),
COUNT( $proj_t->column('name') ) ],
join => [ $emp_proj_t, $proj_t ],
group_by => $proj_t->column('name') );
my @rows = $statement->all_rows;
is( @rows, 2,
"Only two projects should be returned from schema->select" );
is( $rows[0][0], 'Embrace',
"First project should be Embrace - via ->select" );
is( $rows[1][0], 'Extend',
"Second project should be Extend - via ->select" );
is( $rows[0][1], 1,
"First project should have 1 employee - via ->select" );
is( $rows[1][1], 3,
"Second project should have 3 employees - via ->select" );
}
@rows = $s->function( select => [ $proj_t->column('name'),
COUNT( $proj_t->column('name') ) ],
join => [ $emp_proj_t, $proj_t ],
group_by => $proj_t->column('name'),
limit => [1, 1],
);
is( @rows, 1,
"Only one projects should be returned from schema->function - with limit" );
is( $rows[0][0], 'Extend',
"First project should be Extend - with limit" );
is( $rows[0][1], 3,
"First project should have 3 employees - with limit" );
{
my $statement = $s->select( select => [ $proj_t->column('name'),
COUNT( $proj_t->column('name') ) ],
join => [ $emp_proj_t, $proj_t ],
group_by => $proj_t->column('name'),
limit => [1, 1],
);
my @rows = $statement->all_rows;
is( @rows, 1,
"Only one projects should be returned from schema->select - with limit via ->select" );
is( $rows[0][0], 'Extend',
"First project should be Extend - with limit via ->select" );
is( $rows[0][1], 3,
"First project should have 3 employees - with limit via ->select" );
}
{
my @rows = $s->function( select => [ $proj_t->column('name'),
COUNT( $proj_t->column('name') ) ],
join => [ $emp_proj_t, $proj_t ],
group_by => $proj_t->column('name'),
order_by => [ COUNT( $proj_t->column('name') ), 'DESC' ] );
is( @rows, 2,
"Only two projects should be returned from schema->function ordered by COUNT(*)" );
is( $rows[0][0], 'Extend',
"First project should be Extend" );
is( $rows[1][0], 'Embrace',
"Second project should be Embrace" );
is( $rows[0][1], 3,
"First project should have 3 employee" );
is( $rows[1][1], 1,
"Second project should have 1 employees" );
}
{
my @rows = $s->function( select => [ $proj_t->column('name'),
COUNT( $proj_t->column('name') ) ],
join => [ $emp_proj_t, $proj_t ],
group_by => $proj_t->column('name'),
order_by => [ COUNT( $proj_t->column('name') ), 'DESC' ],
having => [ COUNT( $proj_t->column('name') ), '>', 2 ],
);
is( @rows, 1,
"Only one project should be returned from schema->function ordered by COUNT(*) HAVING COUNT(*) > 2" );
is( $rows[0][0], 'Extend',
"First project should be Extend" );
is( $rows[0][1], 3,
"First project should have 3 employee" );
}
{
my @rows;
eval_ok( sub { @rows = $s->function( select => 1,
join => [ $emp_proj_t, $proj_t ],
) },
"Call schema->function with scalar select" );
is( @rows, 4,
"Should return four rows" );
}
{
my $st;
eval_ok( sub { $st = $s->select( select => 1,
join => [ $emp_proj_t, $proj_t ],
) },
"Call schema->select with scalar select" );
my @rows = $st->all_rows;
is( @rows, 4,
"Should return four rows" );
}
my $p1 = $proj_t->insert( values => { name => 'P1',
department_id => $dep_id,
} );
my $p2 = $proj_t->insert( values => { name => 'P2',
department_id => $dep_id,
} );
eval_ok( sub { $cursor = $s->join( distinct => $dep_t,
join => [ $dep_t, $proj_t ],
where => [ $proj_t->column('project_id'), 'in',
map { $_->select('project_id') } $p1, $p2 ],
) },
"Do a join with distinct parameter set" );
@rows = $cursor->all_rows;
is( scalar @rows, 1,
"Setting distinct should cause only a single row to be returned" );
is( $rows[0]->select('department_id'), $dep_id,
"Returned row's department_id should be $dep_id" );
{
eval_ok( sub { $cursor =
$s->join( distinct => $emp_proj_t,
join => [ $emp_t, $emp_proj_t ],
where => [ $emp_t->column('employee_id'), 'in', 9001 ],
) },
"Do a join with distinct parameter set to a table with a multi-col PK" );
@rows = $cursor->all_rows;
is( scalar @rows, 1,
"Setting distinct should cause only a single row to be returned" );
is( $rows[0]->select('employee_id'), 9001,
"Returned row's employee_id should be 9001" );
}
{
eval_ok( sub { $cursor =
$s->join
( distinct => [ $emp_t, $emp_proj_t ],
join => [ $emp_t, $emp_proj_t ],
where =>
[ $emp_t->column('employee_id'), 'in', 9000, 9001 ],
) },
"Do a join with distinct parameter set to a table with a multi-col PK" );
@rows = $cursor->all_rows;
is( scalar @rows, 3,
"Setting distinct should cause only three rows to be returned" );
ok( ( grep { $_->[0]->select('employee_id') == 9000 } @rows ),
"Returned rows should include employee_id 9000" );
ok( ( grep { $_->[0]->select('employee_id') == 9001 } @rows ),
"Returned rows should include employee_id 9001" );
}
{
$proj_t->insert( values => { name => 'P99',
department_id => $dep{lying}->select('department_id'),
} );
eval_ok( sub { $cursor = $s->join( distinct => $dep_t,
join => [ $dep_t, $proj_t ],
order_by => $proj_t->column('name'),
) },
"Do a join with distinct and order_by not in select" );
@rows = $cursor->all_rows;
if ( $rdbms eq 'pg' )
{
is( scalar @rows, 5, "distinct should cause only five rows to be returned" );
}
else
{
is( scalar @rows, 2, "distinct should cause only two rows to be returned" );
}
is( $rows[0]->select('department_id'), $dep{borg}->select('department_id'),
'first row is borg department' );
is( $rows[-1]->select('department_id'), $dep{lying}->select('department_id'),
'last row is lying department' );
# Prevents a warning later about destroying a DBI handle with
# active statement handles.
undef $cursor;
}
# insert rows used to test order by with multiple columns
my $start_id = 999_990;
foreach ( [ qw( OB1 bad ) ],
[ qw( OB1 worse ) ],
[ qw( OB2 bad ) ],
[ qw( OB2 worse ) ],
[ qw( OB3 awful ) ],
[ qw( OB3 bad ) ],
)
{
$emp_t->insert( values => { employee_id => $start_id++,
name => $_->[0],
smell => $_->[1],
dep_id => $dep_id } );
}
@rows = $emp_t->rows_where( where => [ $emp_t->column('employee_id'), 'BETWEEN',
999_990, 999_996 ],
order_by => [ $emp_t->columns( 'name', 'smell' ) ] )->all_rows;
is( $rows[0]->select('name'), 'OB1',
"First row name should be OB1" );
is( $rows[0]->select('smell'), 'bad',
"First row smell should be bad" );
is( $rows[1]->select('name'), 'OB1',
"Second row name should be OB1" );
is( $rows[1]->select('smell'), 'worse',
"Second row smell should be bad" );
is( $rows[2]->select('name'), 'OB2',
"Third row name should be OB2" );
is( $rows[2]->select('smell'), 'bad',
"Third row smell should be bad" );
is( $rows[3]->select('name'), 'OB2',
"Fourth row name should be OB2" );
is( $rows[3]->select('smell'), 'worse',
"Fourth row smell should be worse" );
is( $rows[4]->select('name'), 'OB3',
"Fifth row name should be OB3" );
is( $rows[4]->select('smell'), 'awful',
"Fifth row smell should be awful" );
is( $rows[5]->select('name'), 'OB3',
"Sixth row name should be OB3" );
is( $rows[5]->select('smell'), 'bad',
"Sixth row smell should be bad" );
@rows = $emp_t->rows_where( where => [ $emp_t->column('employee_id'), 'BETWEEN',
999_990, 999_996 ],
order_by => [ $emp_t->column('name'), 'desc', $emp_t->column('smell'), 'asc' ] )->all_rows;
is( $rows[0]->select('name'), 'OB3',
"First row name should be OB3" );
is( $rows[0]->select('smell'), 'awful',
"First row smell should be awful" );
is( $rows[1]->select('name'), 'OB3',
"Second row name should be OB3" );
is( $rows[1]->select('smell'), 'bad',
"Second row smell should be bad" );
is( $rows[2]->select('name'), 'OB2',
"Third row name should be OB2" );
is( $rows[2]->select('smell'), 'bad',
"Third row smell should be bad" );
is( $rows[3]->select('name'), 'OB2',
"Fourth row name should be OB2" );
is( $rows[3]->select('smell'), 'worse',
"Fourth row smell should be worse" );
is( $rows[4]->select('name'), 'OB1',
"Fifth row name should be OB1" );
is( $rows[4]->select('smell'), 'bad',
"Fifth row smell should be bad" );
is( $rows[5]->select('name'), 'OB1',
"Sixth row name should be OB1" );
is( $rows[5]->select('smell'), 'worse',
"Sixth row smell should be worse" );
if ( $rdbms eq 'mysql' )
{
my $emp;
eval_ok( sub { $emp = $emp_t->insert( values => { name => UNIX_TIMESTAMP(),
dep_id => $dep_id } ) },
"Insert using SQL function UNIX_TIMESTAMP()" );
like( $emp->select('name'), qr/\d+/,
"Name should be all digits (unix timestamp)" );
eval_ok( sub { $emp->update( name => LOWER('FOO') ) },
"Do update using SQL function LOWER()" );
is( $emp->select('name'), 'foo',
"Name should be 'foo'" );
eval_ok( sub { $emp->update( name => REPEAT('Foo', 3) ) },
"Do update using SQL function REPEAT()" );
is( $emp->select('name'), 'FooFooFoo',
"Name should be 'FooFooFoo'" );
eval_ok( sub { $emp->update( name => UPPER( REPEAT('Foo', 3) ) ) },
"Do update using nested SQL functions UPPER(REPEAT())" );
is( $emp->select('name'), 'FOOFOOFOO',
"Name should be 'FOOFOOFOO'" );
$emp_t->insert( values => { name => 'Timestamp',
dep_id => $dep_id,
tstamp => time - 100_000 } );
my $cursor;
eval_ok( sub { $cursor =
$emp_t->rows_where( where =>
[ [ $emp_t->column('tstamp'), '!=', undef ],
[ $emp_t->column('tstamp'), '<', UNIX_TIMESTAMP() ] ] ) },
"Do select with where condition that uses SQL function UNIX_TIMESTAMP()" );
my @rows = $cursor->all_rows;
is( scalar @rows, 1,
"Only one row should have a timestamp value that is not null and that is less than the current time" );
is( $rows[0]->select('name'), 'Timestamp',
"That row should be named Timestamp" );
# Fulltext support tests
my $snuffle_id = $emp_t->insert( values => { name => 'snuffleupagus',
smell => 'invisible',
dep_id => $dep_id } )->select('employee_id');
@rows = $emp_t->rows_where( where => [ MATCH( $emp_t->column('name') ), AGAINST('abathraspus') ] )->all_rows;
is( @rows, 0,
"Make sure that fulltext search doesn't give a false positive" );
@rows = $emp_t->rows_where( where => [ MATCH( $emp_t->column('name') ), AGAINST('snuffleupagus') ] )->all_rows;
is( @rows, 1,
"Make sure that fulltext search for snuffleupagus returns 1 row" );
is( $rows[0]->select('employee_id'), $snuffle_id,
"Make sure that the returned row is snuffleupagus" );
my $rows = $emp_t->function( select => [ $emp_t->column('employee_id'), MATCH( $emp_t->column('name') ), AGAINST('snuffleupagus') ],
where => [ MATCH( $emp_t->column('name') ), AGAINST('snuffleupagus') ] );
my ($id, $score) = @$rows;
is( $id, $snuffle_id,
"Returned row should still be snuffleupagus" );
like( $score, qr/\d+(?:\.\d+)?/,
"Returned score should be some sort of number (integer or floating point)" );
ok( $score > 0,
"The score should be greater than 0 because the match was successful" );
eval_ok( sub { @rows = $emp_t->all_rows( order_by => [ IF( 'employee_id < 100',
$emp_t->column('employee_id'),
$emp_t->column('smell') ),
$emp_t->column('employee_id'),
],
)->all_rows },
"Order by IF() function" );
is( @rows, 16,
"Seventeen rows should have been returned" );
is( $rows[0]->select('employee_id'), 3,
"First row should be id 3" );
is( $rows[-1]->select('employee_id'), 999993,
"Last row should be id 999993" );
eval_ok( sub { @rows = $emp_t->all_rows( order_by => RAND() )->all_rows },
"order by RAND()" );
is ( @rows, 16,
"This should return 16 rows" );
}
elsif ( $rdbms eq 'pg' )
{
my $emp;
eval_ok( sub { $emp = $emp_t->insert( values => { name => NOW(),
dep_id => $dep_id } ) },
"Do insert using SQL function NOW()" );
like( $emp->select('name'), qr/\d+/,
"Name should be all digits (Postgres timestamp)" );
eval_ok( sub { $emp->update( name => LOWER('FOO') ) },
"Do update using SQL function LOWER()" );
is( $emp->select('name'), 'foo',
"Name should be 'foo'" );
eval_ok( sub { $emp->update( name => REPEAT('Foo', 3) ) },
"Do update using SQL function REPEAT()" );
is( $emp->select('name'), 'FooFooFoo',
"Name should be 'FooFooFoo'" );
eval_ok( sub { $emp->update( name => UPPER( REPEAT('Foo', 3) ) ) },
"Do update using nested SQL functions UPPER(REPEAT())" );
is( $emp->select('name'), 'FOOFOOFOO',
"Name should be 'FOOFOOFOO'" );
$emp_t->insert( values => { name => 'Timestamp',
dep_id => $dep_id,
tstamp => time - 100_000 } );
my $cursor;
eval_ok( sub { $cursor =
$emp_t->rows_where( where =>
[ [ $emp_t->column('tstamp'), '!=', undef ],
[ $emp_t->column('tstamp'), '<', NOW() ] ] ) },
"Do select with where condition that uses SQL function NOW()" );
my @rows = $cursor->all_rows;
is( scalar @rows, 1,
"Only one row should have a timestamp value that is not null and that is less than the current time" );
is( $rows[0]->select('name'), 'Timestamp',
"That row should be named Timestamp" );
}
# Potential rows
my $p_emp;
eval_ok( sub { $p_emp = $emp_t->potential_row },
"Create potential row object");
is( $p_emp->is_live, 0,
"potential_row should ! ->is_live" );
is( $p_emp->select('smell'), 'grotesque',
"Potential Employee should have default smell, 'grotesque'" );
{
my $updated = $p_emp->update( cash => undef, smell => 'hello!' );
ok( $updated, 'update() did change values' );
ok( ! defined $p_emp->select('cash'),
"Potential Employee cash column is not defined" );
}
{
my $updated = $p_emp->update( cash => undef, smell => 'hello!' );
ok( ! $updated, 'update() did not change values' );
}
is( $p_emp->select('smell'), 'hello!',
"smell for employee should be 'hello!' after update" );
$p_emp->update( name => 'Ilya' );
is( $p_emp->select('name'), 'Ilya',
"New employee got a name" );
$p_emp->update( dep_id => $dep_id );
is( $p_emp->select('dep_id'), $dep_id,
"New employee got a department" );
eval { $p_emp->update( wrong => 'column' ) };
$e = $@;
isa_ok( $e, 'Alzabo::Exception::Params',
"Exception thrown from attempt to update a column which doesn't exist" );
eval { $p_emp->update( name => undef ) };
$e = $@;
isa_ok( $e, 'Alzabo::Exception::NotNullable',
"Exception thrown from attempt to update a non-NULLable column in a potential row to null" );
eval_ok( sub { $p_emp->make_live( values => { smell => 'cottony' } ) },
"Make potential row live");
is( $p_emp->select('name'), 'Ilya',
"Formerly potential employee row object should have same name as before" );
is( $p_emp->select('smell'), 'cottony',
"Formerly potential employee row object should have new smell of 'cottony'" );
eval_ok ( sub { $p_emp->delete },
"Delete new employee" );
eval_ok( sub { $p_emp = $emp_t->potential_row( values => { cash => 100 } ) },
"Create potential row object and set some fields ");
is( $p_emp->select('cash'), 100,
"Employee cash should be 100" );
eval { $emp_t->rows_where( where => [ $eid_c, '=', 9000,
$eid_c, '=', 9002 ] ) };
$e = $@;
isa_ok( $e, 'Alzabo::Exception::Params',
"Exception from where clause as single arrayref with <>3 elements" );
{
# test that DriverStatement objects going out of scope leave
# $@ alone!
eval
{
my $cursor = $emp_t->all_rows;
die "ok\n";
};
is( $@, "ok\n",
"\$\@ should be 'ok'" );
}
{
my $row;
eval_ok( sub { $row =
$emp_t->one_row
( where => [ $emp_t->column('name'), '=', 'nonexistent' ] ) },
"Call ->one_row with a query guaranteed to fail" );
ok( ! defined $row,
"Make sure that the query really returned nothing" );
}
{
is( scalar $proj_t->prefetch,
( scalar $proj_t->columns -
$proj_t->primary_key_size -
scalar ( grep { $_->is_blob } $proj_t->columns ) ),
"Check that schema->prefetch_all_but_blobs is on by default" );
}
{
$proj_t->set_prefetch();
$s->prefetch_all;
is( scalar $proj_t->prefetch,
( scalar $proj_t->columns -
scalar $proj_t->primary_key_size ),
"Check that schema->prefetch_all works" );
}
{
$proj_t->set_prefetch();
$s->prefetch_all_but_blobs;
is( scalar $proj_t->prefetch,
( scalar $proj_t->columns -
$proj_t->primary_key_size -
scalar ( grep { $_->is_blob } $proj_t->columns ) ),
"Check that schema->prefetch_all_but_blobs works" );
}
{
$s->prefetch_none;
is( scalar $proj_t->prefetch, 0,
"Check that schema->prefetch_none works" );
}
{
$s->prefetch_all;
my $cursor;
eval_ok( sub { $cursor =
$s->join( join => [ $emp_t, $emp_proj_t, $proj_t ],
where => [ $emp_t->column('employee_id'), '=', 9001 ] ) },
"Join with join as arrayref of arrayrefs" );
my @rows = $cursor->next;
is( scalar @rows, 3,
"3 rows per cursor ->next call" );
is( ( grep { defined } @rows ), 3,
"Make sure all rows are defined" );
is( $rows[0]->select('employee_id'), 9001,
"First rows should have employee_id == 9001" );
is( $rows[0]->select('name'), 'bob9001',
"First rows should have employee with name eq 'bob9001'" );
is( $rows[2]->select('name'), 'Extend',
"First rows should have project with name eq 'Extend'");
}
{
my $foo = $emp_t->column('employee_id')->alias( as => 'foo' );
my $st = $emp_t->select( select => $foo );
my %h = $st->next_as_hash;
is( exists $h{foo}, 1,
"next_as_hash should return a hash with a 'foo' key" );
}
$s->disconnect;
}
|