JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
release public domain / CC0
[wfpl.git] / unit_tests / db.php
1 <?php
2
3 # This program is in the public domain within the United States. Additionally,
4 # we waive copyright and related rights in the work worldwide through the CC0
5 # 1.0 Universal public domain dedication, which can be found at
6 # http://creativecommons.org/publicdomain/zero/1.0/
7
8
9 # This file contains tests for functions in wfpl/format.php
10 #
11 # See wfpl/test.php for details on how to run or create tests
12
13
14 require_once(__DIR__.'/../'.'db.php');
15
16 function test_db_reposition($table = 'reposition') {
17         db_delete($table);
18         db_insert($table, 'ord', '2');
19         $a = db_auto_id();
20         db_insert($table, 'ord', '3');
21         $b = db_auto_id();
22         db_insert($table, 'ord', '4');
23         $c = db_auto_id();
24         $before = array($a, $b, $c);
25         $before_ords = array('2', '3', '4');
26
27         $args = array(
28                 #  starting position
29                 # /  destination (0: first, 1: after first, 2: after 2nd, 3: after third)
30                 #|  /  result (starting with 012 on each row)
31                 #| /  /
32                 '00 012',
33                 '01 012',
34                 '02 102',
35                 '03 120',
36                 '10 102',
37                 '11 012',
38                 '12 012',
39                 '13 021',
40                 '20 201',
41                 '21 021',
42                 '22 012',
43                 '23 012');
44         # add tests for needing renumbering
45         $thirtytwo = array(
46                 '10 102', # no more room at the start
47                 '02 102', # no more room in the middle (moving up)
48                 '21 021', # no more room in the middle (moving down)
49                 '03 120'); # no more room at the end
50         foreach($thirtytwo as $test) {
51                 for($i = 0; $i < 32; ++$i) {
52                         $args[] = $test;
53                 }
54         }
55
56         foreach($args as $arg_str) {
57                 list($src, $dest, $space, $c1, $c2, $c3) = str_split($arg_str);
58                 $ord = db_reposition($table, $before[$src], $dest, 'ord', 'test record');
59                 db_update($table, 'ord', $ord, 'where id=%i', $before[$src]);
60                 $after = db_get_column($table, 'id', 'order by ord');
61                 $ords = db_get_column($table, 'ord', 'order by ord');
62                 $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])";
63                 if($before[$c1] == $after[0] && $before[$c2] == $after[1] && $before[$c3] == $after[2]) {
64                         unit_test_passed($message);
65                 } else {
66                         unit_test_failed($message);
67                 }
68                 $before = $after;
69                 $before_ords = $ords;
70         }
71 }
72
73 function db_unit_tests_main() {
74         db_connect('test');
75         test_db_reposition();
76 }