JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
clean up include paths and docs
[wfpl.git] / unit_tests / db.php
1 <?php
2
3 #  Copyright (C) 2010 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 wfpl/format.php
20 #
21 # See wfpl/test.php for details on how to run or create tests
22
23
24 require_once(__DIR__.'/../'.'db.php');
25
26 function test_db_reposition($table = 'reposition') {
27         db_delete($table);
28         db_insert($table, 'ord', '2');
29         $a = db_auto_id();
30         db_insert($table, 'ord', '3');
31         $b = db_auto_id();
32         db_insert($table, 'ord', '4');
33         $c = db_auto_id();
34         $before = array($a, $b, $c);
35         $before_ords = array('2', '3', '4');
36
37         $args = array(
38                 #  starting position
39                 # /  destination (0: first, 1: after first, 2: after 2nd, 3: after third)
40                 #|  /  result (starting with 012 on each row)
41                 #| /  /
42                 '00 012',
43                 '01 012',
44                 '02 102',
45                 '03 120',
46                 '10 102',
47                 '11 012',
48                 '12 012',
49                 '13 021',
50                 '20 201',
51                 '21 021',
52                 '22 012',
53                 '23 012');
54         # add tests for needing renumbering
55         $thirtytwo = array(
56                 '10 102', # no more room at the start
57                 '02 102', # no more room in the middle (moving up)
58                 '21 021', # no more room in the middle (moving down)
59                 '03 120'); # no more room at the end
60         foreach($thirtytwo as $test) {
61                 for($i = 0; $i < 32; ++$i) {
62                         $args[] = $test;
63                 }
64         }
65
66         foreach($args as $arg_str) {
67                 list($src, $dest, $space, $c1, $c2, $c3) = str_split($arg_str);
68                 $ord = db_reposition($table, $before[$src], $dest, 'ord', 'test record');
69                 db_update($table, 'ord', $ord, 'where id=%i', $before[$src]);
70                 $after = db_get_column($table, 'id', 'order by ord');
71                 $ords = db_get_column($table, 'ord', 'order by ord');
72                 $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])";
73                 if($before[$c1] == $after[0] && $before[$c2] == $after[1] && $before[$c3] == $after[2]) {
74                         unit_test_passed($message);
75                 } else {
76                         unit_test_failed($message);
77                 }
78                 $before = $after;
79                 $before_ords = $ords;
80         }
81 }
82
83 function db_unit_tests_main() {
84         db_connect('test');
85         test_db_reposition();
86 }