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