JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added unit test for db_reposition()
[wfpl.git] / unit_tests / db.php
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;
+}