From: Aran Deltac Date: Thu, 23 Mar 2006 17:37:05 +0000 (+0000) Subject: Fixes and tests for next/previous _sibling. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=707cbb2d80461bd6592f290374fe655fe1041fb8;p=dbsrgits%2FDBIx-Class-Historic.git Fixes and tests for next/previous _sibling. --- diff --git a/lib/DBIx/Class/Positioned.pm b/lib/DBIx/Class/Positioned.pm index 8fe2d36..7619d5a 100644 --- a/lib/DBIx/Class/Positioned.pm +++ b/lib/DBIx/Class/Positioned.pm @@ -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]; } diff --git a/t/run/26positioned.tl b/t/run/26positioned.tl index ad6a919..c73e257 100644 --- a/t/run/26positioned.tl +++ b/t/run/26positioned.tl @@ -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' ); + } + } }