X-Git-Url: http://mop.ddnsfree.com/gitweb/?a=blobdiff_plain;f=webtest%2Ftest_db.php;h=21a77ad0206559def4bdcde0084146ed5fe78616;hb=e941e33787f5686885b88070a68005aa1eab5273;hp=1c0332b7c31c6c378a97819c32269b278a05e465;hpb=31691c02c1802a699c4595448919cce161834c77;p=brisk.git diff --git a/webtest/test_db.php b/webtest/test_db.php index 1c0332b..21a77ad 100644 --- a/webtest/test_db.php +++ b/webtest/test_db.php @@ -2,7 +2,7 @@ /* * brisk - test_db.php * - * Copyright (C) 2014 Matteo Nastasi + * Copyright (C) 2014-2015 Matteo Nastasi * mailto: nastasi@alternativeoutput.it * matteo.nastasi@milug.org * web: http://www.alternativeoutput.it @@ -27,30 +27,124 @@ $G_base = "./"; require_once($G_base."Obj/brisk.phh"); require_once($G_base."Obj/dbase_${G_dbasetype}.phh"); +function succ($s) +{ + printf("%s ... SUCCESS
\n", $s); +} + +function fail($s) +{ + printf("%s ... FAILED
\n", $s); +} + function main() { do { if (($bdb = BriskDB::create()) == FALSE) { - printf("DB creation failed
\n"); + fail("DB Creation"); break; } - printf("DB creation success
\n"); + succ("DB Creation"); if ($bdb->transaction("BEGIN") == FALSE) { - printf("BEGIN failed
\n"); + fail("BEGIN"); break; } - printf("BEGIN success
\n"); + succ("BEGIN"); if ($bdb->transaction("COMMIT") == FALSE) { - printf("COMMIT failed
\n"); + fail("COMMIT"); + break; + } + succ("COMMIT"); + + $arr_ip = array("1.1.1.1", "127.0.0.1", "255.255.255.255", "255.255.0.0", "201.102.123.111", "5.9.11.13" ); + + if ($bdb->query("DROP TABLE IF EXISTS test_ip;") == FALSE) { + fail("DROP TABLE test_ip"); break; } - printf("COMMIT success
\n"); + succ("DROP TABLE test_ip"); + + if ($bdb->query("CREATE TABLE test_ip ( + code integer, -- insertion code + ip integer, -- ip v4 address + atime timestamp DEFAULT to_timestamp(0) -- access time + ); ") == FALSE) { + fail("CREATE TABLE test_ip"); + break; + } + succ("CREATE TABLE test_ip"); + + foreach ($arr_ip as $i) { + $v = ip2four($i); + + $msg = sprintf("  INSERT INTO test_ip [%s] val: [%d (%x)]", $i, $v, $v); + if ($bdb->query(sprintf("INSERT INTO test_ip (ip, atime) VALUES (%d, '1999-01-08 04:05:06');", $v)) == FALSE) { + printf("%s
\n", $bdb->last_error()); + fail($msg); + break; + } + succ($msg); + } + printf("
\n"); + foreach ($arr_ip as $i) { + $cmp = int2four(ip2int($i) & 0xffffff00); + $msk = int2four(0xffffff00); + $cmp_que = sprintf("SELECT * FROM test_ip WHERE (ip & %d = %d);", $msk, $cmp); + if (($cmp_pg = $bdb->query($cmp_que)) == FALSE) { + printf("%s
\n", $bdb->last_error()); + fail("SELECT * FROM test_ip"); + break; + } + succ($cmp_que); + + for ($r = 0 ; $r < pg_numrows($cmp_pg) ; $r++) { + $cmp_obj = pg_fetch_object($cmp_pg, $r); + + if ($ip_obj->ip & $msk != $cmp) { + fail(sprintf("  Expected: %s, retrieved: %s", int2ip($cmp), int2ip($ip_obj->ip & $msk))); + } + else { + succ(sprintf("  Expected: %s (%s)", int2ip($cmp), int2ip($cmp_obj->ip))); + } + // printf("RET IP: %d IP: %s
\n", $ip_obj->ip, $v)); + } + } + printf("
\n"); + + if (($ip_pg = $bdb->query(sprintf("SELECT * FROM test_ip ORDER BY code;"))) == FALSE) { + printf("%s
\n", $bdb->last_error()); + fail("SELECT * FROM test_ip"); + break; + } + succ("SELECT * FROM test_ip"); + + for ($r = 0 ; $r < pg_numrows($ip_pg) ; $r++) { + $ip_obj = pg_fetch_object($ip_pg, $r); + + $v = int2ip($ip_obj->ip); + + if ($arr_ip[$r] != $v) { + fail(sprintf("  Expected: %s, retrieved: %s", $arr_ip[$r], $v)); + } + else { + succ(sprintf("  Expected: %s", $arr_ip[$r])); + } + // printf("RET IP: %d IP: %s
\n", $ip_obj->ip, $v)); + + } + + if ($bdb->query("DROP TABLE IF EXISTS test_ip;") == FALSE) { + fail("DROP TABLE test_ip"); + break; + } + succ("DROP TABLE test_ip"); + if ($bdb->transaction("BEgIN") != FALSE) { - printf("BEgIN missed fail
\n"); + fail("BEgIN missed fail"); break; } - printf("BEgIN fail correctly
\n"); + succ("BEgIN missed fail"); } while (FALSE); }