Added support for unique constraints to SQLT::Parser::DBIx::Class
[dbsrgits/DBIx-Class.git] / t / run / 26ordered.tl
1 # vim: filetype=perl
2
3 sub run_tests {
4
5     plan tests => 321;
6     my $schema = shift;
7
8     my $employees = $schema->resultset('Employee');
9     $employees->delete();
10
11     foreach (1..5) {
12         $employees->create({ name=>'temp' });
13     }
14     $employees = $employees->search(undef,{order_by=>'position'});
15     ok( check_rs($employees), "intial positions" );
16
17     hammer_rs( $employees );
18
19     DBICTest::Employee->grouping_column('group_id');
20     $employees->delete();
21     foreach my $group_id (1..3) {
22         foreach (1..6) {
23             $employees->create({ name=>'temp', group_id=>$group_id });
24         }
25     }
26     $employees = $employees->search(undef,{order_by=>'group_id,position'});
27
28     foreach my $group_id (1..3) {
29         my $group_employees = $employees->search({group_id=>$group_id});
30         $group_employees->all();
31         ok( check_rs($group_employees), "group intial positions" );
32         hammer_rs( $group_employees );
33     }
34
35 }
36
37 sub hammer_rs {
38     my $rs = shift;
39     my $employee;
40     my $count = $rs->count();
41     my $position_column = $rs->result_class->position_column();
42
43     foreach my $position (1..$count) {
44
45         $row = $rs->find({ $position_column=>$position });
46         $row->move_previous();
47         ok( check_rs($rs), "move_previous( $position )" );
48
49         $row = $rs->find({ $position_column=>$position });
50         $row->move_next();
51         ok( check_rs($rs), "move_next( $position )" );
52
53         $row = $rs->find({ $position_column=>$position });
54         $row->move_first();
55         ok( check_rs($rs), "move_first( $position )" );
56
57         $row = $rs->find({ $position_column=>$position });
58         $row->move_last();
59         ok( check_rs($rs), "move_last( $position )" );
60
61         foreach my $to_position (1..$count) {
62             $row = $rs->find({ $position_column=>$position });
63             $row->move_to($to_position);
64             ok( check_rs($rs), "move_to( $position => $to_position )" );
65         }
66
67         $row = $rs->find({ position=>$position });
68         if ($position==1) {
69             ok( !$row->previous_sibling(), 'no previous sibling' );
70             ok( !$row->first_sibling(), 'no first sibling' );
71         }
72         else {
73             ok( $row->previous_sibling(), 'previous sibling' );
74             ok( $row->first_sibling(), 'first sibling' );
75         }
76         if ($position==$count) {
77             ok( !$row->next_sibling(), 'no next sibling' );
78             ok( !$row->last_sibling(), 'no last sibling' );
79         }
80         else {
81             ok( $row->next_sibling(), 'next sibling' );
82             ok( $row->last_sibling(), 'last sibling' );
83         }
84
85     }
86 }
87
88 sub check_rs {
89     my( $rs ) = @_;
90     $rs->reset();
91     my $position_column = $rs->result_class->position_column();
92     my $expected_position = 0;
93     while (my $row = $rs->next()) {
94         $expected_position ++;
95         if ($row->get_column($position_column)!=$expected_position) {
96             return 0;
97         }
98     }
99     return 1;
100 }
101
102 1;