PHP Classes

File: dbff_tests.php

Recommend this page to a friend!
  Classes of jerry mattsson  >  dbff  >  dbff_tests.php  >  Download  
File: dbff_tests.php
Role: Unit test script
Content type: text/plain
Description: Tests and samples
Class: dbff
Read and write flat file databases in CSV format
Author: By
Last change: Changed include path
Date: 16 years ago
Size: 15,438 bytes
 

Contents

Class file image Download
<?php /* dbff test suite and simple samples :) / 0.98 jm */ require_once 'dbff.php'; print '<html><body>'; /////////////////////////// if (file_exists('test')) { print '<br>File test exist !!!!! Will not overwrite existing file! delete it manually before test run!'; exit; } /////////////////////////// $test_def = array ( 'TABLE_NAME'=>'T', 'FILE_NAME'=>'test', 'cfld' =>array('size'=>40, 'chkNN'=>true, 'chkMinLen'=>15), 'ifld' =>array('size'=>10, 'type'=>'INT', 'chkNN'=>true, 'chkMin'=>9), 'nfld' =>array('size'=>10, 'type'=>'NUMBER', 'chkNN'=>true, 'chkMax'=>999999), 'lfld' =>array('size'=>10, 'type'=>'NUMBER', 'chkLIST'=>array (1,2,3)), 'em' =>array('size'=>50, 'type'=>'EMAIL'), 'dfld' =>array('size'=>8, 'type'=>'DATE','default'=>'now'), 'dtfld' =>array('size'=>14, 'type'=>'DATETIME')); $db = new dbff; $db->recdef = $test_def; $db->read(); $d = array ('now', '20060305', '20050812', '23-May-03', '5/jan/2007', '1-aug-2005', '1-jan-1902', '31-december-2000', '11 january 1906', '6 feb 1992', '2006-07-22', '2005/03/2', '1991-mar-31', '31-mar-1991', '1991-march-31', '25-JAN-2007', '10.Aug.2005', '23/10/2005', '31-dec-2037', '3-Jun-97', '93-Jun-5', '20010100', '1-JAN-2039', '32-december-2000', '1899.dec.22', '19990001', '32-May-2002' ); /* 20 valid 7 invalid */ print '<br>String to date conversion tests, 20 valid 7 invalid'; $i=0; while ($x = current($d)) { $i++; $res = $db->str2ts($x); if (is_null($res)) print "<br>$i) $x ...... invalid"; else print "<br>$i) $x =>".date('Y-M-d H:i:s',$res); next($d); } $d = array ('now', '20060305192233', '11-january-1906 12:23:34', '23/10/2005 11:21:31', '15-Mar-2001 11:12:03', '5-jan-2007 05:15:01', '1-aug-2005 1:01:31', '12:53 15-Mar-2001', '11:33:44 11-january-1906', '31-december-2000 23:23', '2006-07-22 12:23:34', '2005/03/2 11:21:31', '25-JAN-2007 05:15:01pm', '10-Aug-2005 1:01AM', '12:53 29-feb-2001', '1999-dec-22 51:21:31', '20060305116733', '20060305252233', '20060305192299' ); /* 14 val 5 inv */ print '<br><br>String to datetime conversion tests, 14 valid 5 invalid'; $i=0; while ($x = current($d)) { $i++; $res = $db->str2ts($x); if (is_null($res)) print "<br>$i) $x ...... invalid"; else print "<br>$i) $x =>".date('Y-M-d H:i:s',$res); next($d); } print '<br><br>Validation tests set 1, should all be ok'; $dr = array ('1 Correct record', 12345, 234.56, 1, 'abc@cde.com', '10-aug-2000', '20-sep-2001 12:22:33'); if ($db->validateRec($dr)) print '<br>Test record '.$dr[0]. ' Passed ok'; else print '<br>Test record '.$dr[0]. " did NOT validate: $dbfferr"; $dr = array ('2 Ok email test record', 123, 36, NULL, 'a@abc.com', '10-aug-2000', '20-10-2001 12:22:33'); if ($db->validateRec($dr)) print '<br>Test record '.$dr[0]. ' Passed ok'; else print '<br>Test record '.$dr[0]. " did NOT validate: $dbfferr"; $dr = array ('3 Ok List test record', 123, 1234, 3, NULL, NULL, NULL); if ($db->validateRec($dr)) print '<br>Test record '.$dr[0]. ' Passed ok'; else print '<br>Test record '.$dr[0]. " did NOT validate: $dbfferr"; print '<br><br>Validation tests set 1, these should fail'; $dr = array ('10 Missing field record', 234.56, '10-aug-2000', '20-sep-2001 12:22:33'); if ($db->validateRec($dr)) print '<br>Test record '.$dr[0]. ' Passed ok'; else print '<br>Test record '.$dr[0]. " did NOT validate: $dbfferr"; $dr = array ('11 Int test record', '12a45', 234.56, NULL, NULL, NULL, NULL); if ($db->validateRec($dr)) print '<br>Test record '.$dr[0]. ' Passed ok'; else print '<br>Test record '.$dr[0]. " did NOT validate: $dbfferr"; $dr = array ('12 Int test record', 12.45, 234.56, NULL, NULL, NULL, NULL); if ($db->validateRec($dr)) print '<br>Test record '.$dr[0]. ' Passed ok'; else print '<br>Test record '.$dr[0]. " did NOT validate: $dbfferr"; $dr = array ('13 Number test record', 12345, '234:56', NULL, NULL, NULL, NULL); if ($db->validateRec($dr)) print '<br>Test record '.$dr[0]. ' Passed ok'; else print '<br>Test record '.$dr[0]. " did NOT validate: $dbfferr"; $dr = array ('14 Number test record', 12345, 'a2356', NULL, NULL, NULL, NULL); if ($db->validateRec($dr)) print '<br>Test record '.$dr[0]. ' Passed ok'; else print '<br>Test record '.$dr[0]. " did NOT validate: $dbfferr"; $dr = array ('15 char length test record 123456789012345', 123, 36, NULL, NULL, NULL, NULL); if ($db->validateRec($dr)) print '<br>Test record '.$dr[0]. ' Passed ok'; else print '<br>Test record '.$dr[0]. " did NOT validate: $dbfferr"; $dr = array ('16 err email test record', 123, 36, NULL, 'aabc.com', '10-aug-2000', '20-10-2001 12:22:33'); if ($db->validateRec($dr)) print '<br>Test record '.$dr[0]. ' Passed ok'; else print '<br>Test record '.$dr[0]. " did NOT validate: $dbfferr"; $dr = array ('17 min test record', 3, 36, NULL, NULL, NULL, NULL); if ($db->validateRec($dr)) print '<br>Test record '.$dr[0]. ' Passed ok'; else print '<br>Test record '.$dr[0]. " did NOT validate: $dbfferr"; $dr = array ('18 max test record', 123, 1000000, NULL, NULL, NULL, NULL); if ($db->validateRec($dr)) print '<br>Test record '.$dr[0]. ' Passed ok'; else print '<br>Test record '.$dr[0]. " did NOT validate: $dbfferr"; $dr = array ('19 minlen test', 123, 1234, NULL, NULL, NULL, NULL); if ($db->validateRec($dr)) print '<br>Test record '.$dr[0]. ' Passed ok'; else print '<br>Test record '.$dr[0]. " did NOT validate: $dbfferr"; $dr = array ('20 Err List test record', 123, 1234, 5, NULL, NULL, NULL); if ($db->validateRec($dr)) print '<br>Test record '.$dr[0]. ' Passed ok'; else print '<br>Test record '.$dr[0]. " did NOT validate: $dbfferr"; unset($db); print '<br><br>Validation tests set 2, should be ok'; $test_def = array ( 'TABLE_NAME'=>'T', 'FILE_NAME'=>'test', 'cfld' =>array('size'=>40, 'chkNN'=>true, 'chkMinLen'=>15), 'ifld' =>array('size'=>10, 'type'=>'INT', 'chkNN'=>true), 'nfld' =>array('size'=>10, 'type'=>'NUMBER', 'chkNN'=>true), 'lfld' =>array('size'=>10, 'type'=>'NUMBER'), 'em' =>array('size'=>50, 'type'=>'EMAIL'), 'dfld' =>array('size'=>8, 'type'=>'DATE','default'=>'now'), 'dtfld' =>array('size'=>14, 'type'=>'DATETIME')); /* if (isset($atts['CHKMOD10']) && !chkMod10($atts['CHKMOD10'])) { if (isset($atts['CHKFMT']) && sprintf($vrec[$i],$atts['CHKFMT'])<>$vrec[$i]) { */ $db = new dbff; $db->recdef = $test_def; $db->read(); $dr = array ('30 Int zero test record', 0, 234.56, NULL, NULL, NULL, NULL); if ($db->validateRec($dr)) print '<br>Test record '.$dr[0]. ' Passed ok'; else print '<br>Test record '.$dr[0]. " did NOT validate: $dbfferr"; $dr = array ('31 Int zero test record', '0', 234.56, NULL, NULL, NULL, NULL); // string zero ?? from forms if ($db->validateRec($dr)) print '<br>Test record '.$dr[0]. ' Passed ok'; else print '<br>Test record '.$dr[0]. " did NOT validate: $dbfferr"; $dr = array ('32 Int zero test record', '0000', 234.56, NULL, NULL, NULL, NULL); // string zero ?? from forms if ($db->validateRec($dr)) print '<br>Test record '.$dr[0]. ' Passed ok'; else print '<br>Test record '.$dr[0]. " did NOT validate: $dbfferr"; $dr = array ('33 Number zero test record', 12345, 0.0, NULL, NULL, NULL, NULL); if ($db->validateRec($dr)) print '<br>Test record '.$dr[0]. ' Passed ok'; else print '<br>Test record '.$dr[0]. " did NOT validate: $dbfferr"; print '<br><br>Validation tests set 2, should fail'; $dr = array ('34 Int zero test record', '0.0', 234.56, NULL, NULL, NULL, NULL); if ($db->validateRec($dr)) print '<br>Test record '.$dr[0]. ' Passed ok'; else print '<br>Test record '.$dr[0]. " did NOT validate: $dbfferr"; $dr = array ('35 Int zero test record', '0.0000001', 234.56, NULL, NULL, NULL, NULL); if ($db->validateRec($dr)) print '<br>Test record '.$dr[0]. ' Passed ok'; else print '<br>Test record '.$dr[0]. " did NOT validate: $dbfferr"; unset($db); unset ($dr); print '<br><br>Insert tests set 3, should be ok'; $test_def = array ( 'TABLE_NAME'=>'T', 'FILE_NAME'=>'test', 'cfld' =>array('size'=>40, 'chkNN'=>true), 'ifld' =>array('size'=>10, 'type'=>'INT', 'chkNN'=>true), 'nfld' =>array('size'=>10, 'type'=>'NUMBER', 'chkNN'=>true), 'pkfld' =>array('size'=>10, 'pk'=>'sequence'), 'em' =>array('size'=>50, 'type'=>'EMAIL'), 'dfld' =>array('size'=>8, 'type'=>'DATE','default'=>'now'), 'dtfld' =>array('size'=>14, 'type'=>'DATETIME')); $db = new dbff; $db->recdef = $test_def; $db->read(); $tr = array ('This', 1, 2.5, 1, 'abc@cde.com', '10-aug-2000', '20-sep-2001 12:22:33'); print '<br>'; for ($i=1; $i<12; $i++) { $y = 2000+$i; $tr[0] = 'Insert record number '.$i; $tr[1] += $i; $tr[2] +=$i; $tr[5] = $i.'-'.$i.'-'.$y; if ($db->insert($tr)) print " Insert rec $i ok "; else print "<br>Insert rec $i failed $dbfferr"; } print "<br>The following tests should be error free"; $hits = $db->select(); print "<br>$hits records selected"; while ($rec = $db->selectGet(FALSE)) print "<br>$rec[3] $rec[0] $rec[1] $rec[2] $rec[5]"; print '<br><br>Select test set 5 date between 31-dec-2002 and 1-jan-2007'; $hits = $db->select('dfld','31-dec-2002','GT'); $hits = $db->reselect('dfld','1-jan-2007','LT'); print "<br>$hits records selected"; while ($rec = $db->selectGet(FALSE)) print "<br>$rec[3] $rec[0] $rec[1] $rec[2] $rec[5]"; print '<br><br>Update test set 6, set ifld to zero'; $hits = $db->updateSelected('ifld',0); print "<br>$hits records updated"; while ($rec = $db->selectGet(FALSE)) print "<br>$rec[3] $rec[0] $rec[1] $rec[2] $rec[5]"; print '<br><br>print with field names test set 7, same data as before'; while ($rec = $db->selectGet()) print "<br>".$rec['T.CFLD'].' '.$rec['T.IFLD'].' '. $rec['T.NFLD'].' '.$rec['T.DFLD']; print '<br><br>Delete test set 8, print the rest'; $hits = $db->deleteSelected(); print "<br>$hits records deleted"; $hits = $db->select(); print "<br>$hits records selected"; while ($rec = $db->selectGet(FALSE)) print "<br>$rec[3] $rec[0] $rec[1] $rec[2] $rec[5]"; if (!empty($db->errstk)) print_r($db->errstk); unset($db); print '<br><br>Primary/Unique key tests set 9, should fail at duplicate keys and generated dates'; $test_def = array ( 'TABLE_NAME'=>'T', 'FILE_NAME'=>'test', 'cfld' =>array('size'=>40, 'chkNN'=>true), 'ukfld' =>array('size'=>10, 'type'=>'INT', 'uk'=>true), 'nfld' =>array('size'=>10, 'type'=>'NUMBER', 'chkNN'=>true), 'pkfld' =>array('size'=>10, 'type'=>'INT', 'pk'=>true), 'em' =>array('size'=>50, 'type'=>'EMAIL'), 'dfld' =>array('size'=>8, 'type'=>'DATE','default'=>'now'), 'dtfld' =>array('size'=>14, 'type'=>'DATETIME')); $db = new dbff; $db->recdef = $test_def; $db->read(); $dr = array ('Pk/uk test', 1, 2.5, 1, 'abc@cde.com', '10-aug-2000', '20-sep-2001 12:22:33'); print '<br>'; for ($i=1; $i<12; $i++) { $y = 2000+$i; $dr[0] = 'Insert record number '.$i; $dr[1] += $i; $dr[2] =$i; $dr[3] =$i; $dr[5] = $i.'-'.$i.'-'.$y; if ($db->insert($dr)) print " Insert rec $i ok "; else print "<br>Insert rec $i failed $dbfferr"; } for ($i=1; $i<20; $i+=2) { $y = 2000+$i; $dr[0] = 'Insert record number '.$i; $dr[1] = $i+1; $dr[2] +=$i; $dr[3] =$i; $dr[5] = $i.'-'.$i.'-'.$y; if ($db->insert($dr)) print " Insert rec $i ok "; else print "<br>Insert rec $i failed $dbfferr"; } $hits = $db->select(); print "<br>$hits records selected"; while ($rec = $db->selectGet(FALSE)) print "<br>$rec[3] $rec[0] $rec[1] $rec[5]"; unset($db); print '<br><br>Write tests 10000 record, test set 10'; $test_def = array ( 'TABLE_NAME'=>'T', 'FILE_NAME'=>'test', 'pkfld' =>array('size'=>10, 'type'=>'INT', 'pk'=>'sequence'), 'ukfld' =>array('size'=>10, 'type'=>'INT', 'uk'=>true), 'nfld' =>array('size'=>16, 'type'=>'NUMBER', 'chkNN'=>true), 'dtfld' =>array('size'=>14, 'type'=>'DATETIME','default'=>'now')); $db = new dbff; $db->recdef = $test_def; $db->changelog = FALSE; $dr = array (0,0,0,NULL); print '<br>Started: '.$db->str2datetime('now'); for ($i=1; $i<10000; $i++) { $j +=$i; $dr[1] = $i; $dr[2] =$j/($i*3); if (!$db->insert($dr)) print "<br>Insert rec $i failed $dbfferr"; } if (!$db->commit()) print $dbfferr; print ' Written at: '.$db->str2datetime('now'); if (!empty($db->errstk)) print_r($db->errstk); unset($db); print '<br><br>Read tests 10000 record, test set 11'; $test_def = array ( 'TABLE_NAME'=>'T', 'FILE_NAME'=>'test', 'pkfld' =>array('size'=>10, 'type'=>'INT', 'pk'=>'sequence'), 'ukfld' =>array('size'=>10, 'type'=>'INT', 'uk'=>true), 'nfld' =>array('size'=>16, 'type'=>'NUMBER', 'chkNN'=>true), 'dtfld' =>array('size'=>14, 'type'=>'DATETIME','default'=>'now')); $db = new dbff; $db->recdef = $test_def; $db->changelog = FALSE; print '<br>Started: '.$db->str2datetime('now'); $hits = $db->select(); print " All $hits records selected"; print ' Read at: '.$db->str2datetime('now'); print "<br>Search records between 1000 and 2000 in fld 2 "; print '<br>Started: '.$db->str2datetime('now'); $hits = $db->select('ukfld',1000,'ge'); $hits = $db->reselect('ukfld',2000,'le'); print " $hits records selected"; print ' Selected at: '.$db->str2datetime('now'); print "<br>Search one record 1111 in fld 2 "; print '<br>Started: '.$db->str2datetime('now'); $hits = $db->select('ukfld',1111); print " $hits records selected"; print ' Selected at: '.$db->str2datetime('now'); print "<br>Search records >555.6 and < 558.4 in num fld 3 and show sort result"; print '<br>Started: '.$db->str2datetime('now'); $hits = $db->select('nfld',555.6,'gt'); $hits = $db->reselect('nfld',558.4,'lt'); print " $hits records selected"; print ' Selected at: '.$db->str2datetime('now'); $hits = $db->selectSort('nfld'); while ($rec = $db->selectGet(FALSE)) print "<br>$rec[0] $rec[2]"; print "<br><br>Update all records one at the time with new datetime in num fld 4"; print '<br>Started: '.$db->str2datetime('now'); $hits = $db->select(); $ts = time(); while ($rec = $db->selectGet(FALSE)) { $ts -= ($rec[1] * 10); $d = date('YmdHis',$ts); $newrec = array($rec[0], $rec[1], $rec[2], $d); if (!$db->updByRn($db->getSelectedRn(),$newrec)) print "<br>Update failed $dbfferr"; } print ' Updated at: '.$db->str2datetime('now'); print "<br>Write update"; $db->commit(); print ' at: '.$db->str2datetime('now'); print "<br><br>Search records with dates in jan 1991 and show sort result"; print '<br>Started: '.$db->str2datetime('now'); $hits = $db->select('dtfld','1-jan-1991','gt'); $hits = $db->reselect('dtfld','31-jan-1991','lt'); print " $hits records selected"; print ' Selected at: '.$db->str2datetime('now'); $hits = $db->selectSort('dtfld'); while ($rec = $db->selectGet(FALSE)) print "<br>$rec[0] $rec[3]"; if (!empty($db->errstk)) print_r($db->errstk); $db->rollback(); if (file_exists('test')) unlink('test'); print '<br>End test, check errors<br>'; print '</body></html>'; exit; ?>