1# Before `make install' is performed this script should be runnable with 2# `make test'. After `make install' it should work as `perl test.pl' 3 4######################### We start with some black magic to print on failure. 5 6# Change 1..1 below to 1..last_test_to_print . 7# (It may become useful if the test is moved to ./t subdirectory.) 8 9BEGIN { $| = 1; print "1..30\n"; } 10END {print "not ok 1\n" unless $loaded;} 11use DBIWrapper; 12use DBIWrapper::XMLParser; 13$loaded = 1; 14print "ok 1\n"; 15 16print "Comment out the following line to actually run these tests, after you setup your test database.\n"; 17exit 0; 18 19######################### End of black magic. 20 21# Insert your test code below (better if it prints "ok 13" 22# (correspondingly "not ok 13") depending on the success of chunk 13 23# of the test code): 24 25my $dbHost = "localhost"; 26my $dbName = "testing_db"; 27my $dbUser = "ss"; 28my $dbPasswd = "foo"; # make sure a password is specified, even if it isn't needed. 29my $dbType = "Pg"; # mysql for MySQL 30my $dbPort = "5432"; # 3306 for MySQL 31my $longRunningLog = "/tmp/dbiwrapper-long-running-sql.log"; 32my $sqlStatementLog = "/tmp/dbiwrapper-sql-statements.log"; 33my $logSQLStatements = 1; 34 35my $verbose = 0; 36 37if (-e $sqlStatementLog) 38{ 39 print "Deleting $sqlStatementLog...\n"; 40 unlink $sqlStatementLog; 41} 42 43print "Instantiating an instance...\n"; 44my $db = DBIWrapper->new(dbType => $dbType, dbHost => $dbHost, dbName => $dbName, dbUser => $dbUser, dbPasswd => $dbPasswd, dbPort => $dbPort, printError => 0, longRunningLog => $longRunningLog, longRunningRead => 1, longRunningWrite => 1, sqlStatementLog => $sqlStatementLog, logSQLStatements => $logSQLStatements); 45if ($db->error()) 46{ 47 die $db->errorMessage(); 48} 49print "ok 2\n"; 50 51print "Checking current date...\n"; 52my $sth = $db->read("SELECT now()"); 53if ($db->error()) 54{ 55 $db->close(); 56 die $db->errorMessage(); 57} 58my @row = $sth->fetchrow_array; 59$sth->finish; 60print "Current Database time is '$row[0]'.\n" if ($verbose); 61print "ok 3\n"; 62 63print "Getting all rows...\n"; 64$sth = $db->read(sql => "SELECT * FROM test_tb ORDER BY name, value"); 65if ($db->error()) 66{ 67 $db->close(); 68 die $db->errorMessage(); 69} 70while (@row = $sth->fetchrow_array) 71{ 72 print "\tname='" . $row[0] . "', value='" . $row[1] . "'\n" if ($verbose); 73} 74$sth->finish; 75print "ok 4\n"; 76 77print "Inserting dummy data and getting the ID value via getID()...\n"; 78$db->write(sql => "INSERT INTO test_tb (name, value) VALUES (?, ?)", plug => [ "dummy", "entry2" ]); 79if ($db->error()) 80{ 81 $db->close(); 82 die $db->errorMessage(); 83} 84my $id = $db->getID("test_tb.ID"); 85if ($db->error()) 86{ 87 $db->close(); 88 die $db->errorMessage(); 89} 90print "ID = '$id'\n"; 91print "ok 5\n"; 92 93print "Looking for new row (plug and substitute)...\n"; 94$sth = $db->read(sql => "SELECT * FROM test_tb WHERE name = ? ORDER BY name, ##?1##", plug => [ "dummy" ], substitute => [ "value" ]); 95if ($db->error()) 96{ 97 $db->close(); 98 die $db->errorMessage(); 99} 100while (@row = $sth->fetchrow_array) 101{ 102 print "\tname='" . $row[0] . "', value='" . $row[1] . "'\n" if ($verbose); 103} 104$sth->finish; # necessary as DBI doesn't appear to properly cleanup when the 105# end of data is reached. 106print "ok 6\n"; 107 108print "Getting all rows (again - using substitution)...\n"; 109$sth = $db->read(sql => "SELECT * FROM test_tb ORDER BY ##?1##, ##?2##", substitute => [ "name", "value" ]); 110if ($db->error()) 111{ 112 $db->close(); 113 die $db->errorMessage(); 114} 115while (@row = $sth->fetchrow_array) 116{ 117 print "\tname='" . $row[0] . "', value='" . $row[1] . "'\n" if ($verbose); 118} 119$sth->finish; 120print "ok 7\n"; 121 122print "Committing transaction...\n"; 123$db->commit; 124if ($db->error()) 125{ 126 $db->close(); 127 die $db->errorMessage(); 128} 129print "ok 8\n"; 130 131print "Deleting new row (testing substitution code)...\n"; 132my $rv = $db->write(sql => "DELETE FROM test_tb WHERE ##?1##", substitute => [ "name = 'dummy'" ]); 133if ($db->error()) 134{ 135 $db->close(); 136 die $db->errorMessage(); 137} 138print "'$rv' rows deleted.\n" if ($verbose); 139print "ok 9\n"; 140 141print "Committing transaction...\n"; 142$db->commit; 143if ($db->error()) 144{ 145 $db->close(); 146 die $db->errorMessage(); 147} 148print "ok 10\n"; 149 150print "Re-Inserting dummy data...\n"; 151$db->write(sql => "INSERT INTO test_tb (name, value) VALUES (?, ?)", plug => [ "dummy", "entry2" ]); 152if ($db->error()) 153{ 154 $db->close(); 155 die $db->errorMessage(); 156} 157print "ok 11\n"; 158 159print "Rolling back the transaction...\n"; 160$db->rollback; 161if ($db->error()) 162{ 163 $db->close(); 164 die $db->errorMessage(); 165} 166print "ok 12\n"; 167 168print "Getting all rows (again - using substitution)...\n"; 169$sth = $db->read(sql => "SELECT * FROM test_tb ORDER BY ##?1##, ##?2##", substitute => [ "name", "value" ]); 170if ($db->error()) 171{ 172 $db->close(); 173 die $db->errorMessage(); 174} 175while (@row = $sth->fetchrow_array) 176{ 177 print "\tname='" . $row[0] . "', value='" . $row[1] . "'\n" if ($verbose); 178} 179$sth->finish; 180print "ok 13\n"; 181 182print "Re-Inserting dummy data...\n"; 183$db->write(sql => "INSERT INTO test_tb (name, value) VALUES (?, ?)", plug => [ "dummy", "entry2" ]); 184if ($db->error()) 185{ 186 $db->close(); 187 die $db->errorMessage(); 188} 189print "ok 14\n"; 190 191print "Committing transaction...\n"; 192$db->commit; 193if ($db->error()) 194{ 195 $db->close(); 196 die $db->errorMessage(); 197} 198print "ok 15\n"; 199 200print "Getting all rows (again - using substitution)...\n"; 201$sth = $db->read(sql => "SELECT * FROM test_tb ORDER BY ##?1##, ##?2##", substitute => [ "name", "value" ]); 202if ($db->error()) 203{ 204 $db->close(); 205 die $db->errorMessage(); 206} 207while (@row = $sth->fetchrow_array) 208{ 209 print "\tname='" . $row[0] . "', value='" . $row[1] . "'\n" if ($verbose); 210} 211$sth->finish; 212print "ok 16\n"; 213 214print "Testing the readXML method...\n"; 215my $resultDoc = <<"END_OF_CODE"; 216<?xml version="1.0" encoding="ISO-8859-1"?> 217<resultset version="1.2"> 218 <select sql="SELECT name, value FROM test_tb ORDER BY name, value" plug="''"/> 219 <status result="Ok" error=""/> 220 <rows numRows="2" columns="1"> 221 <row> 222 <column name="name" value="dummy"/> 223 <column name="value" value="entry2"/> 224 </row> 225 <row> 226 <column name="name" value="test"/> 227 <column name="value" value="entry"/> 228 </row> 229 </rows> 230</resultset> 231END_OF_CODE 232 233my $xmlDoc = $db->readXML(sql => "SELECT name, value FROM test_tb ORDER BY name, value", columns => 1); 234if ($db->error()) 235{ 236 $db->close(); 237 die $xmlDoc; 238} 239print $xmlDoc if ($verbose); 240if ($xmlDoc eq $resultDoc) 241{ 242 print "ok 17\n"; 243} 244else 245{ 246 print "failed 17\n"; 247} 248 249print "Running through XMLParser...\n"; 250my $xmlParserObj = DBIWrapper::XMLParser->new(string => $xmlDoc); 251my $resultSetObj = $xmlParserObj->parse(); 252my $xmlDoc2 = $resultSetObj->generateXML(); 253 254if ($xmlDoc2 eq $xmlDoc) 255{ 256 print "ok 18\n"; 257} 258else 259{ 260 print "failed 18\n"; 261} 262 263print "Inserting complex data and retrieving via XML...\n"; 264my $complexData = <<"END_OF_DATA"; 265This is a "test"! 266I 'wonder' what DBI will do with 267this &embeded <stuff>!? 268END_OF_DATA 269 270$db->write(sql => "INSERT INTO test_tb (name, value) VALUES (?, ?)", plug => [ "complex", $complexData ]); 271if ($db->error()) 272{ 273 $db->close(); 274 die $db->errorMessage(); 275} 276 277$resultDoc = <<"END_OF_CODE"; 278<?xml version="1.0" encoding="ISO-8859-1"?> 279<resultset version="1.2"> 280 <select sql="SELECT name, value FROM test_tb WHERE name = 'complex' ORDER BY name, value" plug="''"/> 281 <status result="Ok" error=""/> 282 <rows numRows="1" columns="1"> 283 <row> 284 <column name="name" value="complex"/> 285 <column name="value" value="This is a "test"!\\nI 'wonder' what DBI will do with\\nthis &embeded <stuff>!?\\n"/> 286 </row> 287 </rows> 288</resultset> 289END_OF_CODE 290 291$xmlDoc = $db->readXML(sql => "SELECT name, value FROM test_tb WHERE name = 'complex' ORDER BY name, value", columns => 1); 292if ($db->error()) 293{ 294 $db->close(); 295 die $xmlDoc; 296} 297print $xmlDoc if ($verbose); 298if ($xmlDoc eq $resultDoc) 299{ 300 print "ok 19\n"; 301} 302else 303{ 304 print "failed 19\n"; 305} 306 307$db->rollback; 308 309print "Testing the readXML method (noncolumnar)...\n"; 310my $resultDoc = <<"END_OF_CODE"; 311<?xml version="1.0" encoding="ISO-8859-1"?> 312<resultset version="1.2"> 313 <select sql="SELECT name, value FROM test_tb ORDER BY name, value" plug="''"/> 314 <status result="Ok" error=""/> 315 <rows numRows="2" columns="0"> 316 <row name="dummy" value="entry2"/> 317 <row name="test" value="entry"/> 318 </rows> 319</resultset> 320END_OF_CODE 321 322my $xmlDoc = $db->readXML(sql => "SELECT name, value FROM test_tb ORDER BY name, value"); 323if ($db->error()) 324{ 325 $db->close(); 326 die $xmlDoc; 327} 328print $xmlDoc if ($verbose); 329if ($xmlDoc eq $resultDoc) 330{ 331 print "ok 20\n"; 332} 333else 334{ 335 print "failed 20\n"; 336} 337 338print "Inserting complex data and retrieving via XML (noncolumnar)...\n"; 339my $complexData = <<"END_OF_DATA"; 340This is a "test"! 341I 'wonder' what DBI will do with 342this &embeded <stuff>!? 343END_OF_DATA 344 345$db->write(sql => "INSERT INTO test_tb (name, value) VALUES (?, ?)", plug => [ "complex", $complexData ]); 346if ($db->error()) 347{ 348 $db->close(); 349 die $db->errorMessage(); 350} 351 352$resultDoc = <<"END_OF_CODE"; 353<?xml version="1.0" encoding="ISO-8859-1"?> 354<resultset version="1.2"> 355 <select sql="SELECT name, value FROM test_tb WHERE value LIKE '%"test"%' ORDER BY name, value" plug="''"/> 356 <status result="Ok" error=""/> 357 <rows numRows="1" columns="0"> 358 <row name="complex" value="This is a "test"!\\nI 'wonder' what DBI will do with\\nthis &embeded <stuff>!?\\n"/> 359 </rows> 360</resultset> 361END_OF_CODE 362 363$xmlDoc = $db->readXML(sql => "SELECT name, value FROM test_tb WHERE value LIKE '%\"test\"%' ORDER BY name, value"); 364if ($db->error()) 365{ 366 $db->close(); 367 die $xmlDoc; 368} 369print $xmlDoc if ($verbose); 370if ($xmlDoc eq $resultDoc) 371{ 372 print "ok 21\n"; 373} 374else 375{ 376 print "failed 21\n"; 377} 378 379print "Testing error returned by readXML()...\n"; 380$resultDoc = <<"END_OF_DATA"; 381<?xml version="1.0" encoding="ISO-8859-1"?> 382<resultset version="1.2"> 383 <select sql="SELECT invalid FROM test_tb" plug="''"/> 384 <status result="Error" error="DBIWrapper->readXML() - Error!<br />\\n\\nDBIWrapper->getDataHashHeader() - Error!<br />\\n\\nDBIWrapper->read() - Error!<br />\\nEval of execute failed!<br />\\nError = 'DBD::Pg::st execute failed: ERROR: column "invalid" does not exist at character 8\\n'.<br />\\nsql='SELECT invalid FROM test_tb'.<br />\\nplug=''.<br />\\n\\n"/> 385 <rows numRows="0" columns="0"/> 386</resultset> 387END_OF_DATA 388 389$xmlDoc = $db->readXML(sql => "SELECT invalid FROM test_tb"); 390if ($db->error()) 391{ 392 $db->close(); 393 die $xmlDoc; 394} 395$xmlDoc =~ s/(\/usr\/lib\/perl5\/site_perl\/5\.[68]\.[01234]\/DBIWrapper.pm)/DBIWrapper.pm/; 396$xmlDoc =~ s/(blib\/lib\/DBIWrapper.pm)/DBIWrapper.pm/; 397print $xmlDoc if ($verbose); 398if ($xmlDoc eq $resultDoc) 399{ 400 print "ok 22\n"; 401} 402else 403{ 404 print "failed 22\n"; 405 print "resultDoc:\n$resultDoc\n\nxmlDoc:\n$xmlDoc\n\n"; 406} 407$db->resetError(); # make sure we cleanup for future error tests. 408 409print "Testing boolToDBI()...\n"; 410my $result = $db->boolToDBI("false"); 411if ($result == 0) 412{ 413 $result = $db->boolToDBI(string => "true"); 414 if ($result == 1) 415 { 416 $result = $db->boolToDBI(string => 0); 417 if ($result == 0) 418 { 419 $result = $db->boolToDBI("t"); 420 if ($result == 1) 421 { 422 $result = $db->boolToDBI(string => "fa"); 423 if ($result == 0) 424 { 425 $result = $db->boolToDBI("1"); 426 if ($result == 1) 427 { 428 print "ok 23\n"; 429 } 430 else 431 { 432 print "failed 23 - string = '1'\n"; 433 } 434 } 435 else 436 { 437 print "failed 23 - string = 'fa'\n"; 438 } 439 } 440 else 441 { 442 print "failed 23 - string = 't'\n"; 443 } 444 } 445 else 446 { 447 print "failed 23 - string = '0'\n"; 448 } 449 } 450 else 451 { 452 print "failed 23 - string = 'true'\n"; 453 } 454} 455else 456{ 457 print "failed 23 - string = 'false'\n"; 458} 459 460print "Testing dbiToBool()...\n"; 461my $result = $db->dbiToBool("0"); 462if ($result eq "false") 463{ 464 $result = $db->dbiToBool(string => "1"); 465 if ($result eq "true") 466 { 467 $result = $db->dbiToBool(string => 0); 468 if ($result eq "false") 469 { 470 $result = $db->dbiToBool("1"); 471 if ($result eq "true") 472 { 473 $result = $db->dbiToBool(string => "fa"); 474 if ($result eq "false") 475 { 476 print "ok 24\n"; 477 } 478 else 479 { 480 print "failed 24 - string = 'fa'\n"; 481 } 482 } 483 else 484 { 485 print "failed 24 - string = '1'\n"; 486 } 487 } 488 else 489 { 490 print "failed 24 - string = '0'\n"; 491 } 492 } 493 else 494 { 495 print "failed 24 - string = '1'\n"; 496 } 497} 498else 499{ 500 print "failed 24 - string = '0'\n"; 501} 502 503print "Committing transaction (to clear error condition)...\n"; 504$db->commit; 505if ($db->error()) 506{ 507 $db->close(); 508 die $db->errorMessage(); 509} 510print "ok 25\n"; 511 512print "Testing the readHTML method...\n"; 513my $resultDoc = <<"END_OF_CODE"; 514<div class="sqlNumRows"><span class="sqlNumRows"><b>2</b> rows returned.</span></div> 515<table class=""> 516 <thead> 517 <tr class="sqlHeader"> 518 <th class="name">name</th> 519 <th class="value">value</th> 520 </tr> 521 </thead> 522 <tbody> 523 <tr class="sqlRow sqlEven"> 524 <td class="name"><span class="name">dummy</span></td> 525 <td class="value"><span class="value">entry2</span></td> 526 </tr> 527 <tr class="sqlRow sqlOdd"> 528 <td class="name"><span class="name">test</span></td> 529 <td class="value"><span class="value">entry</span></td> 530 </tr> 531 </tbody> 532</table> 533END_OF_CODE 534 535my $htmlDoc = $db->readHTML(sql => "SELECT name, value FROM test_tb ORDER BY name, value"); 536if ($db->error()) 537{ 538 $db->close(); 539 die $htmlDoc; 540} 541print $htmlDoc if ($verbose); 542if ($htmlDoc eq $resultDoc) 543{ 544 print "ok 26\n"; 545} 546else 547{ 548 print "failed 26\n"; 549 print "resultDoc:\n$resultDoc\n\nhtmlDoc:\n$htmlDoc\n\n"; 550} 551 552print "Testing error returned by readHTML()...\n"; 553$resultDoc = <<"END_OF_DATA"; 554<div class="sqlNumRows"><span class="sqlNumRows"><b>0</b> rows returned.</span></div> 555<table class=""> 556 <tr class="sqlError"> 557 <td class="sqlError"><span class="sqlError">DBIWrapper->readHTML() - Error!<br />\n\nDBIWrapper->getDataHashHeader() - Error!<br />\n\nDBIWrapper->read() - Error!<br />\nEval of execute failed!<br />\nError = 'DBD::Pg::st execute failed: ERROR: column "invalid" does not exist at character 8\n'.<br />\nsql='SELECT invalid FROM test_tb'.<br />\nplug=''.<br />\n\n</span></td> 558 </tr> 559</table> 560END_OF_DATA 561 562$htmlDoc = $db->readHTML(sql => "SELECT invalid FROM test_tb"); 563if ($db->error()) 564{ 565 $db->close(); 566 die $htmlDoc; 567} 568$htmlDoc =~ s/(\/usr\/lib\/perl5\/site_perl\/5\.[68]\.[01234]\/DBIWrapper.pm)/DBIWrapper.pm/; 569$htmlDoc =~ s/(blib\/lib\/DBIWrapper.pm)/DBIWrapper.pm/; 570print $htmlDoc if ($verbose); 571if ($htmlDoc eq $resultDoc) 572{ 573 print "ok 27\n"; 574} 575else 576{ 577 print "failed 27\n"; 578 print "resultDoc:\n$resultDoc\n\nhtmlDoc:\n$htmlDoc\n\n"; 579} 580$db->resetError(); # make sure we cleanup for future error tests. 581$db->commit(); 582 583# now test longRunning sql handling. 584print "Testing longRunningRead sql handling...\n"; 585if ( -e $longRunningLog ) 586{ 587 print "Removing previous log file '$longRunningLog'...\n"; 588 print "-" x 30 . "\n"; 589 open F, "<$longRunningLog"; 590 my @lines = <F>; 591 close F; 592 print join("", @lines); 593 print "-" x 30 . "\n"; 594 unlink $longRunningLog; 595} 596my @result = $db->getDataHash(sql => "SELECT pg_sleep(2),\nnow() AS current_time, ? || ' Test' AS name", plug => [ 0 ]); 597print "RESULT: " . join(", ", map {"$_='$result[0]->{$_}'"} sort keys %{$result[0]}) . "\n"; 598if (-e $longRunningLog) 599{ 600 open F, "<$longRunningLog"; 601 my @lines = <F>; 602 close F; 603 print "After:\n"; 604 print "-" x 30 . "\n"; 605 print join("", @lines); 606 print "-" x 30 . "\n"; 607 print "ok 28\n"; 608} 609else 610{ 611 print "failed 28\n"; 612} 613 614print "Verifying $sqlStatementLog was populated...\n"; 615if (-e $sqlStatementLog) 616{ 617 print "ok 29\n"; 618} 619else 620{ 621 print "failed 29\n"; 622} 623 624print "Closing connection...\n"; 625$db->close(); 626if ($db->error()) 627{ 628 die $db->errorMessage(); 629} 630print "ok 30\n"; 631 632