From: <chr...@us...> - 2006-05-24 23:33:44
|
Revision: 884 Author: chromatic Date: 2006-05-24 16:32:30 -0700 (Wed, 24 May 2006) ViewCVS: http://svn.sourceforge.net/everydevel/?rev=884&view=rev Log Message: ----------- r17483@windwheel: chromatic | 2006-05-24 16:32:21 -0700 Finished tests for Everything::Node::Workspace. Modified Paths: -------------- trunk/ebase/lib/Everything/NodeBase/Workspace.pm trunk/ebase/lib/Everything/Test/NodeBase/Workspace.pm Property Changed: ---------------- trunk/ebase/ Property changes on: trunk/ebase ___________________________________________________________________ Name: svk:merge - a6810612-c0f9-0310-9d3e-a9e4af8c5745:/ebase/offline:17450 + a6810612-c0f9-0310-9d3e-a9e4af8c5745:/ebase/offline:17483 Modified: trunk/ebase/lib/Everything/NodeBase/Workspace.pm =================================================================== --- trunk/ebase/lib/Everything/NodeBase/Workspace.pm 2006-05-24 00:27:31 UTC (rev 883) +++ trunk/ebase/lib/Everything/NodeBase/Workspace.pm 2006-05-24 23:32:30 UTC (rev 884) @@ -13,8 +13,6 @@ use base 'Everything::NodeBase'; -use File::Spec; - =head2 C<joinWorkspace> create the $DB-E<gt>{workspace} object if a workspace is specified. If the @@ -33,9 +31,6 @@ sub joinWorkspace { my ( $this, $WORKSPACE ) = @_; - - delete $this->{workspace} if exists $this->{workspace}; - return 1 unless $WORKSPACE; $this->getRef($WORKSPACE); @@ -51,9 +46,9 @@ =head2 C<getNodeWorkspace> -Helper funciton for getNode's workspace functionality. Given a $WHERE hash ( -field =E<gt> value, or field =E<gt> [value1, value2, value3]) return a list of -nodes in the workspace which fullfill this query +Helper function for getNode's workspace functionality. Given a $WHERE hash +(field =E<gt> value, or field =E<gt> [value1, value2, value3]) return a list of +nodes in the workspace which fulfill this query. =over 4 @@ -75,7 +70,9 @@ my @results; $TYPE = $this->getType($TYPE) if $TYPE; - my $cmpval = sub { + # compare node ids + my $cmpval = sub + { my ( $val1, $val2 ) = @_; $val1 = $val1->{node_id} if eval { $val1->isa( 'Everything::Node' ) }; @@ -84,33 +81,36 @@ $val1 eq $val2; }; - #we need to iterate through our workspace - foreach my $node ( keys %{ $this->{workspace}{nodes} } ) + # iterate through the workspace + for my $node ( keys %{ $this->{workspace}{nodes} } ) { my $N = $this->getNode($node); - next if $TYPE and $$N{type}{node_id} != $$TYPE{node_id}; + next if $TYPE and $N->{type}{node_id} != $TYPE->{node_id}; my $match = 1; - foreach ( keys %$WHERE ) + + for my $where ( keys %$WHERE ) { - if ( ref $$WHERE{$_} eq 'ARRAY' ) + if ( ref $WHERE->{$where} eq 'ARRAY' ) { - my $matchor = 0; - foreach my $orval ( @{ $$WHERE{$_} } ) + $match = 0; + + for my $orval ( @{ $WHERE->{$where} } ) { - $matchor = 1 if $cmpval->( $$N{$_}, $orval ); + next unless $cmpval->( $N->{$where}, $orval ); + $match = 1; + last; } - $match = 0 unless $matchor; } else { - $match = 0 unless $cmpval->( $$N{$_}, $$WHERE{$_} ); + $match = 0 unless $cmpval->( $N->{$where}, $WHERE->{$where} ); } } push @results, $N if $match; } - \@results; + return \@results; } =head2 C<getNode> @@ -181,7 +181,7 @@ return unless @results; my $orderby = $ext2 || 'node_id'; - my $position = ( $orderby =~ /\s+desc/i ) ? -1 : 0; + my $position = ( $orderby =~ s/\s+desc//i ) ? -1 : 0; @results = sort { $a->{$orderby} cmp $b->{$orderby} } @results; return $results[$position]; } Modified: trunk/ebase/lib/Everything/Test/NodeBase/Workspace.pm =================================================================== --- trunk/ebase/lib/Everything/Test/NodeBase/Workspace.pm 2006-05-24 00:27:31 UTC (rev 883) +++ trunk/ebase/lib/Everything/Test/NodeBase/Workspace.pm 2006-05-24 23:32:30 UTC (rev 884) @@ -1,5 +1,145 @@ package Everything::Test::NodeBase::Workspace; +use strict; +use warnings; + use base 'Everything::Test::NodeBase'; +use SUPER; +use Test::More; + +sub test_join_workspace :Test( 7 ) +{ + my $self = shift; + my $nb = $self->{nb}; + my $storage = $self->{storage}; + + is( $nb->joinWorkspace(), 1, + 'joinWorkspace() should return 1 without workspace to join' ); + + $nb->mock( getRef => sub { $_[1] = 0 } ); + is( $nb->joinWorkspace( 'foo' ), -1, + '... or -1 unless workspace is a valid node' ); + + $nb->set_true( 'getRef' ); + $storage->set_series( getVars => 'vars' ); + + is( $nb->joinWorkspace( $storage ), 1, + '... or 1 if joining workspace works' ); + + is( $nb->{workspace}, $storage, '... setting workspace attribute' ); + is( $storage->{nodes}, 'vars', '... setting workspace nodes' ); + is_deeply( $storage->{cached_nodes}, {}, '... and cache' ); + + $nb->joinWorkspace( $storage ); + is_deeply( $storage->{nodes}, {}, + '... using default nodes unless present' ); +} + +sub test_get_node_workspace :Test( 5 ) +{ + my $self = shift; + my $nb = $self->{nb}; + my $storage = $self->{storage}; + + my $nodes = + { + 2 => { node_id => 2, title => 'foo', type => { node_id => 1 } }, + 3 => { node_id => 3, title => 'bar', type => { node_id => 1 } }, + 4 => { node_id => 4, title => 'baz', type => { node_id => 2 } }, + }; + + $nb->{workspace}{nodes} = $nodes; + $nb->mock( getType => sub { return { node_id => $_[1] } } ) + ->mock( getNode => sub { return $nodes->{$_[1]} } ); + + my $result = [ sort @{ $nb->getNodeWorkspace() } ]; + is_deeply( $result, [ map { $nodes->{$_} } 2 .. 4 ], + 'getNodeWorkspace() should return all nodes without criteria' ); + + $result = [ sort @{ $nb->getNodeWorkspace( {}, 1 ) } ]; + is_deeply( $result, [ map { $nodes->{$_} } 2, 3 ], + '... or only nodes of the specific type' ); + + $nodes->{5} = { node_id => 5, title => 'foo', type => { node_id => 2 } }; + $result = [ sort @{ $nb->getNodeWorkspace( { title => 'foo' } ) } ]; + is_deeply( $result, [ map { $nodes->{$_} } 2, 5 ], + '... or only nodes matching a single criterion' ); + + $result = [sort @{ $nb->getNodeWorkspace({ title => [qw( bar baz )]} )} ]; + is_deeply( $result, [ map { $nodes->{$_} } 3, 4 ], + '... or only nodes matching a multi-value criterion' ); + + $nodes->{6} = bless { node_id => 6, type => { node_id => 3 } }, + 'Everything::Node'; + $nb->mock( getNode => sub { { node => bless { + node_id => $_[1], type => { node_id => 3 } }, 'Everything::Node' + }}); + + my $selector = bless { node_id => 6 }, 'Everything::Node'; + + $result = [sort @{ $nb->getNodeWorkspace( { node => $selector } ) } ]; + is_deeply( $result, [ { node => $nodes->{6} } ], + '... or blessed nodes with matching node ids' ); +} + +sub test_get_node :Test( +5 ) +{ + my $self = shift; + my $nb = $self->{nb}; + my $storage = $self->{storage}; + + $self->SUPER(); + + $nb->set_false( 'SUPER' ); + is( $nb->getNode( 100 ), undef, + 'getNode() should return false unless SUPER() call returns a node' ); + + $nb->set_always( SUPER => $storage ); + $storage->{node_id} = 100; + $storage->set_series( getWorkspaced => 0, 'workspaced' ); + is( $nb->getNode( 102 ), $storage, + '... and should return non-workspaced node, if not in workspace' ); + + $nb->{workspace}{nodes}{100} = 0; + is( $nb->getNode( 102 ), $storage, + '... or if workspaced node has no value' ); + + $nb->{workspace}{nodes}{100} = 1; + is( $nb->getNode( 102 ), $storage, + '... even when fetched from workspace' ); + + is( $nb->getNode( 102 ), 'workspaced', + '... but should return it if it does exist' ); +} + +sub test_get_node_with_where :Test( 4 ) +{ + my $self = shift; + my $nb = $self->{nb}; + my $storage = $self->{storage}; + + $nb->{workspace}{nodes} = { map { $_ => { node_id => $_, w => $_ } } 1..3 }; + + $nb->set_series( getNodeWhere => 0, + [ map { { node_id => $_, w => $_ } } 1 .. 3 ] ) + ->set_always( getNodeWorkspace => [] ); + + my $result = $nb->getNode( { node_id => 10 } ); + + is( $result, undef, + 'getNode() with where should return nothing with no node to find' ); + + is( $nb->getNode( { node_id => 10 } ), undef, + '... or nothing with no nodes in workspace' ); + + $nb->set_always( getNodeWorkspace => [values %{ $nb->{workspace}{nodes} }]); + is_deeply( $nb->getNode( { node_id => 11 } ), { node_id => 1, w => 1 }, + '... or the workspaced node, if there is a match' ); + + is_deeply( $nb->getNode( { node_id => 11 }, '', 'w desc' ), + { node_id => 3, w => 3 }, + '... ordered by secondary field, if provided' ); +} + 1; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |