JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added unit test for db_reposition()
authorJason Woofenden <jason@jasonwoof.com>
Mon, 29 Mar 2010 19:41:34 +0000 (15:41 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Mon, 29 Mar 2010 19:41:34 +0000 (15:41 -0400)
misc.php
unit_tests.php
unit_tests/db.php [new file with mode: 0644]

index 1e9a2b7..8f46014 100644 (file)
--- 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'));
index c63e4cd..0808fa4 100644 (file)
@@ -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 (file)
index 0000000..a2aeb1b
--- /dev/null
@@ -0,0 +1,92 @@
+<?php
+
+#  Copyright (C) 2009 Jason Woofenden
+#
+#  This program is free software: you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation, either version 3 of the License, or
+#  (at your option) any later version.
+#  
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#  
+#  You should have received a copy of the GNU General Public License
+#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+# 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;
+}