Reshape initial tests
Peter Rabbitson [Sat, 12 Sep 2009 10:11:41 +0000 (10:11 +0000)]
t/lib/DBICTest/Schema/Artist.pm
t/lib/DBICTest/Schema/Track.pm
t/relationship/core.t
t/relationship/custom.t [new file with mode: 0644]

index da590ff..4f2bde5 100644 (file)
@@ -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" }
         };
     }
 );
index 7a738a1..10fd396 100644 (file)
@@ -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;
index 59be451..d6cb3a3 100644 (file)
@@ -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 (file)
index 0000000..e356733
--- /dev/null
@@ -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;