Fixes and tests for next/previous _sibling.
Aran Deltac [Thu, 23 Mar 2006 17:37:05 +0000 (17:37 +0000)]
lib/DBIx/Class/Positioned.pm
t/run/26positioned.tl

index 8fe2d36..7619d5a 100644 (file)
@@ -155,12 +155,13 @@ is returned if the current object is the first one.
 sub previous_sibling {
     my( $self ) = @_;
     my $position_column = $self->position_column;
+    my $position = $self->get_column( $position_column );
+    return 0 if ($position==1);
     return ($self->search(
         {
-            $position_column => { '<' => $self->get_column($position_column) },
+            $position_column => $position - 1,
             $self->_collection_clause(),
-        },
-        { rows=>1, order_by => $position_column.' DESC' },
+        }
     )->all())[0];
 }
 
@@ -176,12 +177,14 @@ is returned if the current object is the last one.
 sub next_sibling {
     my( $self ) = @_;
     my $position_column = $self->position_column;
+    my $position = $self->get_column( $position_column );
+    my $count = $self->result_source->resultset->search({$self->_collection_clause()})->count();
+    return 0 if ($position==$count);
     return ($self->result_source->resultset->search(
         {
-            $position_column => { '>' => $self->get_column($position_column) },
+            $position_column => $position + 1,
             $self->_collection_clause(),
         },
-        { rows=>1, order_by => $position_column },
     )->all())[0];
 }
 
index ad6a919..c73e257 100644 (file)
@@ -2,48 +2,58 @@
 
 sub run_tests {
 
-    plan tests => 96;
+    plan tests => 56;
     my $schema = shift;
 
     my $employees = $schema->resultset('Employee::Positioned');
+    $employees->delete();
 
-    if ($employees->result_class->can('position_column')) {
-
-        $employees->delete();
-        foreach (1..5) {
-            $employees->create({ name=>'temp' });
-        }
+    foreach (1..5) {
+        $employees->create({ name=>'temp' });
+    }
+    $employees = $employees->search(undef,{order_by=>'position'});
+    ok( check_positions($employees), "$class: intial positions" );
 
-        $employees = $employees->search(undef,{order_by=>'position'});
-        ok( check_positions($employees), "$class: intial positions" );
+    my $employee;
 
-        my $employee;
+    foreach my $position (1..$employees->count()) {
 
-        foreach my $position (1..$employees->count()) {
+        $employee = $employees->find({ position=>$position });
+        $employee->move_previous();
+        ok( check_positions($employees), "$class: move_previous( $position )" );
 
-            $employee = $employees->find({ position=>$position });
-            $employee->move_previous();
-            ok( check_positions($employees), "$class: move_previous( $position )" );
+        $employee = $employees->find({ position=>$position });
+        $employee->move_next();
+        ok( check_positions($employees), "$class: move_next( $position )" );
 
-            $employee = $employees->find({ position=>$position });
-            $employee->move_next();
-            ok( check_positions($employees), "$class: move_next( $position )" );
+        $employee = $employees->find({ position=>$position });
+        $employee->move_first();
+        ok( check_positions($employees), "$class: move_first( $position )" );
 
-            $employee = $employees->find({ position=>$position });
-            $employee->move_first();
-            ok( check_positions($employees), "$class: move_first( $position )" );
+        $employee = $employees->find({ position=>$position });
+        $employee->move_last();
+        ok( check_positions($employees), "$class: move_last( $position )" );
 
+        foreach my $to_position (1..$employees->count()) {
             $employee = $employees->find({ position=>$position });
-            $employee->move_last();
-            ok( check_positions($employees), "$class: move_last( $position )" );
-
-            foreach my $to_position (1..$employees->count()) {
-                $employee = $employees->find({ position=>$position });
-                $employee->move_to($to_position);
-                ok( check_positions($employees), "$class: move_to( $position => $to_position )" );
-            }
+            $employee->move_to($to_position);
+            ok( check_positions($employees), "$class: move_to( $position => $to_position )" );
+        }
 
+        $employee = $employees->find({ position=>$position });
+        if ($position==1) {
+            ok( !$employee->previous_sibling(), 'no previous sibling' );
+            ok( $employee->next_sibling(), 'next sibling' );
+        }
+        elsif ($position==$employees->count()) {
+            ok( $employee->previous_sibling(), 'previous sibling' );
+            ok( !$employee->next_sibling(), 'no next sibling' );
         }
+        else {
+            ok( $employee->previous_sibling(), 'previous sibling' );
+            ok( $employee->next_sibling(), 'next sibling' );
+        }
+
     }
 }