From: Peter Rabbitson Date: Sat, 12 Sep 2009 10:11:41 +0000 (+0000) Subject: Reshape initial tests X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b5c8410c7296e6d5128292b3d21a6a2c261774cc;p=dbsrgits%2FDBIx-Class-Historic.git Reshape initial tests --- diff --git a/t/lib/DBICTest/Schema/Artist.pm b/t/lib/DBICTest/Schema/Artist.pm index da590ff..4f2bde5 100644 --- a/t/lib/DBICTest/Schema/Artist.pm +++ b/t/lib/DBICTest/Schema/Artist.pm @@ -49,11 +49,11 @@ __PACKAGE__->has_many( __PACKAGE__->has_many( cds_80s => 'DBICTest::Schema::CD', sub { - my ( $rs, $self, $foreign ) = @_; + my ( $self_alias, $rel_alias, $self_rsrc, $rel_name ) = @_; return { - "${foreign}.artist" => "${self}.artistid", - "${foreign}.year" => { '>', "1979" }, - "${foreign}.year" => { '<', "1990" } + "${rel_alias}.artist" => \ "${self_alias}.artistid", + "${rel_alias}.year" => { '>', "1979" }, + "${rel_alias}.year" => { '<', "1990" } }; } ); diff --git a/t/lib/DBICTest/Schema/Track.pm b/t/lib/DBICTest/Schema/Track.pm index 7a738a1..10fd396 100644 --- a/t/lib/DBICTest/Schema/Track.pm +++ b/t/lib/DBICTest/Schema/Track.pm @@ -1,4 +1,4 @@ -package # hide from PAUSE +package # hide from PAUSE DBICTest::Schema::Track; use base qw/DBICTest::BaseResult/; @@ -63,4 +63,16 @@ __PACKAGE__->belongs_to( { join_type => 'left' }, ); +__PACKAGE__->might_have ( + 'next_track', + __PACKAGE__, + sub { + my ( $self_alias, $rel_alias, $self_rsrc, $rel_name ) = @_; + return { + "${self_alias}.cd" => \ "${rel_alias}.cd", + "${self_alias}.position" => { '<', \ "${rel_alias}.position" }, + }; + }, +); + 1; diff --git a/t/relationship/core.t b/t/relationship/core.t index 59be451..d6cb3a3 100644 --- a/t/relationship/core.t +++ b/t/relationship/core.t @@ -323,15 +323,4 @@ is($cds->count, 1, "subjoins under left joins force_left (arrayref)"); $cds = $schema->resultset("CD")->search({ 'me.cdid' => 5 }, { join => { single_track => { cd => {} } } }); is($cds->count, 1, "subjoins under left joins force_left (hashref)"); -$artist = $schema->resultset("Artist")->create({ name => 'Michael Jackson' }); -foreach my $year (1975..1985) { - $artist->create_related('cds', { year => $year, title => 'Compilation from ' . $year }); -} - -my @cds_80s = $artist->cds_80s; - -is(@cds_80s, 6, '6 80s cds found'); - -map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s; - done_testing; diff --git a/t/relationship/custom.t b/t/relationship/custom.t new file mode 100644 index 0000000..e356733 --- /dev/null +++ b/t/relationship/custom.t @@ -0,0 +1,45 @@ +use strict; +use warnings; + +use Test::More; +use Test::Exception; +use lib qw(t/lib); +use DBICTest; + +my $schema = DBICTest->init_schema(); + + +my $artist = $schema->resultset("Artist")->create({ name => 'Michael Jackson' }); +foreach my $year (1975..1985) { + $artist->create_related('cds', { year => $year, title => 'Compilation from ' . $year }); +} + +my @cds_80s = $artist->cds_80s; + +is(@cds_80s, 6, '6 80s cds found'); + +map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s; + + + + +my @last_track_ids; +for my $cd ($schema->resultset('CD')->search ({}, { order_by => 'cdid'})->all) { + push @last_track_ids, $cd->tracks + ->search ({}, { order_by => { -desc => 'position'} }) + ->get_column ('trackid') + ->next; +} + +my $last_tracks = $schema->resultset('Track')->search ( + {'next_track.trackid' => undef}, + { join => 'next_track', order_by => 'me.cd' }, +); + +is_deeply ( + [$last_tracks->get_column ('trackid')->all], + \@last_track_ids, + 'last group-entry via self-join works', +); + +done_testing;