From 0e820ee53af5142bd6774ee415ccdb9627bcdbb6 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Mon, 29 Mar 2010 15:41:34 -0400 Subject: [PATCH] added unit test for db_reposition() --- misc.php | 2 +- unit_tests.php | 2 +- unit_tests/db.php | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 unit_tests/db.php diff --git a/misc.php b/misc.php index 1e9a2b7..8f46014 100644 --- a/misc.php +++ b/misc.php @@ -105,7 +105,7 @@ function get_text_between($text, $start_text, $end_text) { # Make it easy to insert an array into the template data structure so that each # element of the array gets its own row. # -# passed this: columnate(array('a', 'b', 'c'), 'k'); +# passed this: columnize(array('a', 'b', 'c'), 'k'); # it returns: array(array('k' => 'a'), # array('k' => 'b'), # array('k' => 'c')); diff --git a/unit_tests.php b/unit_tests.php index c63e4cd..0808fa4 100644 --- a/unit_tests.php +++ b/unit_tests.php @@ -49,7 +49,7 @@ function run_unit_tests($directory, $basenames) { # Call this to unit test wfpl. By default it tests everything, or you can pass # a space-separated list of the basenames of the files in code/wfpl/unit_tests/ # that you'd like to run. -function unit_test_wfpl($basenames = 'format') { +function unit_test_wfpl($basenames = 'format db') { tem_load('code/wfpl/unit_tests/template.html'); run_unit_tests('code/wfpl/unit_tests', $basenames); display_messages(); diff --git a/unit_tests/db.php b/unit_tests/db.php new file mode 100644 index 0000000..a2aeb1b --- /dev/null +++ b/unit_tests/db.php @@ -0,0 +1,92 @@ +. + + +# This file contains tests for functions in code/wfpl/format.php +# +# See code/wfpl/test.php for details on how to run or create tests + + +require_once('code/wfpl/db.php'); + +# returns number of errors +function test_db_reposition() { + $table = 'reposition'; + $errors = 0; + 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); + 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'); + if($before[$c1] != $after[0] || $before[$c2] != $after[1] || $before[$c3] != $after[2]) { + 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])"); + $errors += 1; + } + $before = $after; + $before_ords = $ords; + } + + return $errors; +} + +# returns number of errors +function db_unit_tests_main() { + db_connect('test'); + $errors = 0; + $errors += test_db_reposition(); + return $errors; +} -- 1.7.10.4