Delete basicrels tests. Modify run tests to use new syntax. Remove helperrels test...
[dbsrgits/DBIx-Class-Historic.git] / t / run / 06relationship.tl
index bc84c2e..45ed343 100644 (file)
@@ -1,9 +1,13 @@
-sub run_tests {
-my $schema = shift;
-
 use strict;
 use warnings;  
-plan tests => 26;
+
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+
+my $schema = DBICTest::init_schema();
+
+plan tests => 32;
 
 # has_a test
 my $cd = $schema->resultset("CD")->find(4);
@@ -38,6 +42,12 @@ if ($INC{'DBICTest/HelperRels.pm'}) {
 
 is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' );
 
+my( $rs_from_list ) = $artist->search_related_rs('cds');
+is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'search_related_rs in list context returns rs' );
+
+( $rs_from_list ) = $artist->cds_rs();
+is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'relation_rs in list context returns rs' );
+
 # count_related
 is( $artist->count_related('cds'), 4, 'count_related ok' );
 
@@ -50,11 +60,7 @@ my $track = $schema->resultset("Track")->create( {
 } );
 $track->set_from_related( cd => $cd );
 
-if ($INC{'DBICTest/HelperRels.pm'}) { # expect inflated object
-  is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' );
-} else {
-  is( $track->cd, 4, 'set_from_related ok' );
-}
+is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' );
 
 $track->set_from_related( cd => undef );
 
@@ -72,11 +78,7 @@ $track->update_from_related( cd => $cd );
 
 my $t_cd = ($schema->resultset("Track")->search( cd => 4, position => 99 ))[0]->cd;
 
-if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
-  is( $t_cd->cdid, 4, 'update_from_related ok' );
-} else {
-  is( $t_cd, 4, 'update_from_related ok' );
-}
+is( $t_cd->cdid, 4, 'update_from_related ok' );
 
 # find_or_create_related with an existing record
 $cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } );
@@ -94,6 +96,19 @@ is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create
 $artist->delete_related( cds => { title => 'Greatest Hits' });
 cmp_ok( $schema->resultset("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' );
 
+# find_or_new_related with an existing record
+$cd = $artist->find_or_new_related( 'cds', { title => 'Big Flop' } );
+is( $cd->year, 2005, 'find_or_new_related on existing record ok' );
+ok( $cd->in_storage, 'find_or_new_related on existing record: is in_storage' );
+
+# find_or_new_related instantiating a new record
+$cd = $artist->find_or_new_related( 'cds', {
+  title => 'Greatest Hits 2: Louder Than Ever',
+  year => 2007,
+} );
+is( $cd->title, 'Greatest Hits 2: Louder Than Ever', 'find_or_new_related new record ok' );
+ok( ! $cd->in_storage, 'find_or_new_related on a new record: not in_storage' );
+
 SKIP: {
   skip "relationship checking needs fixing", 1;
   # try to add a bogus relationship using the wrong cols
@@ -151,6 +166,3 @@ $artist->cds->update({artist => $nartist->id});
 cmp_ok($artist->cds->count, '==', 0, "Correct new #cds for artist");
 cmp_ok($nartist->cds->count, '==', 2, "Correct new #cds for artist");
 
-}
-
-1;