JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
db_reposition(): fixed error messages
[wfpl.git] / unit_tests / db.php
1 <?php
2
3 #  Copyright (C) 2009 Jason Woofenden
4 #
5 #  This program is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #  
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #  
15 #  You should have received a copy of the GNU General Public License
16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 # This file contains tests for functions in code/wfpl/format.php
20 #
21 # See code/wfpl/test.php for details on how to run or create tests
22
23
24 require_once('code/wfpl/db.php');
25
26 # returns number of errors
27 function test_db_reposition() {
28         $table = 'reposition';
29         $errors = 0;
30         db_delete($table);
31         db_insert($table, 'ord', '2');
32         $a = db_auto_id();
33         db_insert($table, 'ord', '3');
34         $b = db_auto_id();
35         db_insert($table, 'ord', '4');
36         $c = db_auto_id();
37         $before = array($a, $b, $c);
38         $before_ords = array('2', '3', '4');
39
40         $args = array(
41                 #  starting position
42                 # /  destination (0: first, 1: after first, 2: after 2nd, 3: after third)
43                 #|  /  result (starting with 012 on each row)
44                 #| /  /
45                 '00 012',
46                 '01 012',
47                 '02 102',
48                 '03 120',
49                 '10 102',
50                 '11 012',
51                 '12 012',
52                 '13 021',
53                 '20 201',
54                 '21 021',
55                 '22 012',
56                 '23 012');
57         # add tests for needing renumbering
58         $thirtytwo = array(
59                 '10 102', # no more room at the start
60                 '02 102', # no more room in the middle (moving up)
61                 '21 021', # no more room in the middle (moving down)
62                 '03 120'); # no more room at the end
63         foreach($thirtytwo as $test) {
64                 for($i = 0; $i < 32; ++$i) {
65                         $args[] = $test;
66                 }
67         }
68
69         foreach($args as $arg_str) {
70                 list($src, $dest, $space, $c1, $c2, $c3) = str_split($arg_str);
71                 $ord = db_reposition($table, $before[$src], $dest, 'ord', 'test record');
72                 db_update($table, 'ord', $ord, 'where id=%i', $before[$src]);
73                 $after = db_get_column($table, 'id', 'order by ord');
74                 $ords = db_get_column($table, 'ord', 'order by ord');
75                 if($before[$c1] != $after[0] || $before[$c2] != $after[1] || $before[$c3] != $after[2]) {
76                         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])");
77                         $errors += 1;
78                 }
79                 $before = $after;
80                 $before_ords = $ords;
81         }
82
83         return $errors;
84 }
85
86 # returns number of errors
87 function db_unit_tests_main() {
88         db_connect('test');
89         $errors = 0;
90         $errors += test_db_reposition();
91         return $errors;
92 }