New collection_column feature with working tests.
[dbsrgits/DBIx-Class-Historic.git] / t / run / 26positioned.tl
index 997f970..224e658 100644 (file)
 
 sub run_tests {
 
+    plan tests => 321;
     my $schema = shift;
-    my $artists = $schema->resultset("Artist");
 
-    $artists->delete();
-    $artists->create({ artistid=>1, name=>"Joe" });
-    $artists->create({ artistid=>2, name=>"Bob" });
-    $artists->create({ artistid=>3, name=>"Ted" });
-    $artists->create({ artistid=>4, name=>"Ned" });
-    $artists->create({ artistid=>5, name=>"Don" });
+    my $employees = $schema->resultset('Employee::Positioned');
+    $employees->delete();
 
-    $artists = $artists->search(undef,{order_by=>'position'});
+    foreach (1..5) {
+        $employees->create({ name=>'temp' });
+    }
+    $employees = $employees->search(undef,{order_by=>'position'});
+    ok( check_rs($employees), "intial positions" );
 
-    plan tests => 230;
+    hammer_rs( $employees );
 
-    check_positions($schema);
+    DBICTest::Employee::Positioned->collection_column('group_id');
+    $employees->delete();
+    foreach my $group_id (1..3) {
+        foreach (1..6) {
+            $employees->create({ name=>'temp', group_id=>$group_id });
+        }
+    }
+    $employees = $employees->search(undef,{order_by=>'group_id,position'});
 
-    my $artist;
+    foreach my $group_id (1..3) {
+        my $group_employees = $employees->search({group_id=>$group_id});
+        $group_employees->all();
+        ok( check_rs($group_employees), "group intial positions" );
+        hammer_rs( $group_employees );
+    }
 
-    foreach my $position (1..5) {
+}
 
-        $artist = $artists->find({ position=>$position });
-        $artist->move_previous();
-        check_positions($schema);
+sub hammer_rs {
+    my $rs = shift;
+    my $employee;
+    my $count = $rs->count();
+    my $position_column = $rs->result_class->position_column();
 
-        $artist = $artists->find({ position=>$position });
-        $artist->move_next();
-        check_positions($schema);
+    foreach my $position (1..$count) {
 
-        $artist = $artists->find({ position=>$position });
-        $artist->move_first();
-        check_positions($schema);
+        $row = $rs->find({ $position_column=>$position });
+        $row->move_previous();
+        ok( check_rs($rs), "move_previous( $position )" );
 
-        $artist = $artists->find({ position=>$position });
-        $artist->move_last();
-        check_positions($schema);
+        $row = $rs->find({ $position_column=>$position });
+        $row->move_next();
+        ok( check_rs($rs), "move_next( $position )" );
 
-        foreach my $to_position (1..5) {
+        $row = $rs->find({ $position_column=>$position });
+        $row->move_first();
+        ok( check_rs($rs), "move_first( $position )" );
 
-            $artist = $artists->find({ position=>$position });
-            $artist->move_to($to_position);
-            check_positions($schema);
+        $row = $rs->find({ $position_column=>$position });
+        $row->move_last();
+        ok( check_rs($rs), "move_last( $position )" );
 
+        foreach my $to_position (1..$count) {
+            $row = $rs->find({ $position_column=>$position });
+            $row->move_to($to_position);
+            ok( check_rs($rs), "move_to( $position => $to_position )" );
         }
 
-    }
+        $row = $rs->find({ position=>$position });
+        if ($position==1) {
+            ok( !$row->previous_sibling(), 'no previous sibling' );
+            ok( !$row->first_sibling(), 'no first sibling' );
+        }
+        else {
+            ok( $row->previous_sibling(), 'previous sibling' );
+            ok( $row->first_sibling(), 'first sibling' );
+        }
+        if ($position==$count) {
+            ok( !$row->next_sibling(), 'no next sibling' );
+            ok( !$row->last_sibling(), 'no last sibling' );
+        }
+        else {
+            ok( $row->next_sibling(), 'next sibling' );
+            ok( $row->last_sibling(), 'last sibling' );
+        }
 
+    }
 }
 
-sub check_positions {
-    my $schema = shift;
-    my $artists = $schema->resultset("Artist")->search(undef,{order_by=>'position'});
+sub check_rs {
+    my( $rs ) = @_;
+    $rs->reset();
+    my $position_column = $rs->result_class->position_column();
     my $expected_position = 0;
-    while (my $artist = $artists->next()) {
+    while (my $row = $rs->next()) {
         $expected_position ++;
-        ok( ($artist->position()==$expected_position), 'default positions set as expected' );
+        if ($row->get_column($position_column)!=$expected_position) {
+            return 0;
+        }
     }
+    return 1;
 }
 
 1;