fix related resultsets and multi-create
[dbsrgits/DBIx-Class.git] / t / 93single_accessor_object.t
index 6423c03..287785f 100644 (file)
@@ -2,12 +2,13 @@ use strict;
 use warnings;  
 
 use Test::More;
+use Test::Exception;
 use lib qw(t/lib);
 use DBICTest;
 
 my $schema = DBICTest->init_schema();
 
-plan tests => 7;
+plan tests => 10;
 
 # Test various uses of passing an object to find, create, and update on a single
 # rel accessor
@@ -23,20 +24,41 @@ plan tests => 7;
   is($cd->get_column('artist'), $artist->id, 'artist matches CD');
 
   my $liner_notes = $schema->resultset("LinerNotes")->find_or_create({
-    liner_id => $cd,
-    notes    => "Creating using an object on a might_have is helpful.",
+    cd     => $cd,
+    notes  => "Creating using an object on a might_have is helpful.",
   });
   ok(defined $liner_notes, 'created liner notes');
   is($liner_notes->liner_id, $cd->cdid, 'liner notes matches CD');
   is($liner_notes->notes, "Creating using an object on a might_have is helpful.", 'liner notes are correct');
 
   my $track = $cd->tracks->find_or_create({
-    position => 1,
+    position => 127,
     title    => 'Single Accessor'
   });
   is($track->get_column('cd'), $cd->cdid, 'track matches CD before update');
 
   my $another_cd = $schema->resultset("CD")->find(5);
   $track->update({ disc => $another_cd });
-  is($track->cdid, $another_cd->cdid, 'track matches another CD after update');
+  is($track->get_column('cd'), $another_cd->cdid, 'track matches another CD after update');
 }
+
+$schema = DBICTest->init_schema();
+
+{
+       my $artist = $schema->resultset('Artist')->create({ artistid => 666, name => 'bad religion' });
+       my $cd = $schema->resultset('CD')->create({ cdid => 187, artist => 1, title => 'how could hell be any worse?', year => 1982, genreid => undef });
+
+       ok(!defined($cd->genreid), 'genreid is NULL');
+       ok(!defined($cd->genre), 'genre accessor returns undef');
+}
+
+$schema = DBICTest->init_schema();
+
+{
+       my $artist = $schema->resultset('Artist')->create({ artistid => 666, name => 'bad religion' });
+       my $genre = $schema->resultset('Genre')->create({ name => 'disco' });
+       my $cd = $schema->resultset('CD')->create({ cdid => 187, artist => 1, title => 'how could hell be any worse?', year => 1982 });
+
+       dies_ok { $cd->genre } 'genre accessor throws without column';
+}
+