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