. # This file contains tests for functions in wfpl/format.php # # See wfpl/test.php for details on how to run or create tests require_once(__DIR__.'/../'.'db.php'); function test_db_reposition($table = 'reposition') { db_delete($table); db_insert($table, 'ord', '2'); $a = db_auto_id(); db_insert($table, 'ord', '3'); $b = db_auto_id(); db_insert($table, 'ord', '4'); $c = db_auto_id(); $before = array($a, $b, $c); $before_ords = array('2', '3', '4'); $args = array( # starting position # / destination (0: first, 1: after first, 2: after 2nd, 3: after third) #| / result (starting with 012 on each row) #| / / '00 012', '01 012', '02 102', '03 120', '10 102', '11 012', '12 012', '13 021', '20 201', '21 021', '22 012', '23 012'); # add tests for needing renumbering $thirtytwo = array( '10 102', # no more room at the start '02 102', # no more room in the middle (moving up) '21 021', # no more room in the middle (moving down) '03 120'); # no more room at the end foreach($thirtytwo as $test) { for($i = 0; $i < 32; ++$i) { $args[] = $test; } } foreach($args as $arg_str) { list($src, $dest, $space, $c1, $c2, $c3) = str_split($arg_str); $ord = db_reposition($table, $before[$src], $dest, 'ord', 'test record'); db_update($table, 'ord', $ord, 'where id=%i', $before[$src]); $after = db_get_column($table, 'id', 'order by ord'); $ords = db_get_column($table, 'ord', 'order by ord'); $message = "db_reposition($before[$src], $dest) did: ($before[0]:$before_ords[0], $before[1]:$before_ords[1], $before[2]:$before_ords[2]) -> ($after[0]:$ords[0]), $after[1]:$ords[1], $after[2]:$ords[2])"; if($before[$c1] == $after[0] && $before[$c2] == $after[1] && $before[$c3] == $after[2]) { unit_test_passed($message); } else { unit_test_failed($message); } $before = $after; $before_ords = $ords; } } function db_unit_tests_main() { db_connect('test'); test_db_reposition(); }