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