Fix some errors with using unique constraints with Ordered.
[dbsrgits/DBIx-Class.git] / t / run / 27ordered.tl
CommitLineData
93cec8c3 1# vim: filetype=perl
2
3sub run_tests {
4
b1c66eea 5 plan tests => 66;
93cec8c3 6 my $schema = shift;
93cec8c3 7
02f1c2b6 8 my $employees = $schema->resultset('Employee');
707cbb2d 9 $employees->delete();
93cec8c3 10
707cbb2d 11 foreach (1..5) {
12 $employees->create({ name=>'temp' });
13 }
14 $employees = $employees->search(undef,{order_by=>'position'});
169bb185 15 ok( check_rs($employees), "intial positions" );
80010e2b 16
169bb185 17 hammer_rs( $employees );
18
b1c66eea 19 return;
20
a733c37f 21 DBICTest::Employee->grouping_column('group_id');
169bb185 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
39sub hammer_rs {
40 my $rs = shift;
707cbb2d 41 my $employee;
169bb185 42 my $count = $rs->count();
43 my $position_column = $rs->result_class->position_column();
80010e2b 44
169bb185 45 foreach my $position (1..$count) {
80010e2b 46
169bb185 47 $row = $rs->find({ $position_column=>$position });
48 $row->move_previous();
49 ok( check_rs($rs), "move_previous( $position )" );
80010e2b 50
169bb185 51 $row = $rs->find({ $position_column=>$position });
52 $row->move_next();
53 ok( check_rs($rs), "move_next( $position )" );
80010e2b 54
169bb185 55 $row = $rs->find({ $position_column=>$position });
56 $row->move_first();
57 ok( check_rs($rs), "move_first( $position )" );
80010e2b 58
169bb185 59 $row = $rs->find({ $position_column=>$position });
60 $row->move_last();
61 ok( check_rs($rs), "move_last( $position )" );
80010e2b 62
169bb185 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 )" );
707cbb2d 67 }
80021def 68
169bb185 69 $row = $rs->find({ position=>$position });
707cbb2d 70 if ($position==1) {
169bb185 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' );
707cbb2d 77 }
169bb185 78 if ($position==$count) {
79 ok( !$row->next_sibling(), 'no next sibling' );
80 ok( !$row->last_sibling(), 'no last sibling' );
80021def 81 }
707cbb2d 82 else {
169bb185 83 ok( $row->next_sibling(), 'next sibling' );
84 ok( $row->last_sibling(), 'last sibling' );
707cbb2d 85 }
86
80010e2b 87 }
80010e2b 88}
89
169bb185 90sub check_rs {
91 my( $rs ) = @_;
92 $rs->reset();
93 my $position_column = $rs->result_class->position_column();
93cec8c3 94 my $expected_position = 0;
169bb185 95 while (my $row = $rs->next()) {
93cec8c3 96 $expected_position ++;
169bb185 97 if ($row->get_column($position_column)!=$expected_position) {
133dd22a 98 return 0;
80021def 99 }
93cec8c3 100 }
133dd22a 101 return 1;
93cec8c3 102}
103
1041;