new config option to DBICTest to let you set an alternative storage type, start on...
[dbsrgits/DBIx-Class.git] / t / 96multi_create.t
index 79571d3..a6394cf 100644 (file)
@@ -1,15 +1,12 @@
 use strict;
 use warnings;
 
-use Test::More;
+use Test::More qw(no_plan);
 use lib qw(t/lib);
 use DBICTest;
-use DateTime;
 
 my $schema = DBICTest->init_schema();
 
-plan tests => 12;
-
 my $cd2 = $schema->resultset('CD')->create({ artist => 
                                    { name => 'Fred Bloggs' },
                                    title => 'Some CD',
@@ -54,6 +51,23 @@ my $newartist2 = $schema->resultset('Artist')->find_or_create({ name => 'Fred 3'
 
 is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
 
+my $artist2 = $schema->resultset('Artist')->create({ artistid => 1000,
+                                                    name => 'Fred 3',
+                                                     cds => [
+                                                             { artist => 1000,
+                                                               title => 'Music to code by',
+                                                               year => 2007,
+                                                             },
+                                                             ],
+                                                    cds_unordered => [
+                                                             { artist => 1000,
+                                                               title => 'Music to code by',
+                                                               year => 2007,
+                                                             },
+                                                             ]
+                                                     });
+
+is($artist2->in_storage, 1, 'artist with duplicate rels inserted okay');
 
 CREATE_RELATED1 :{
 
@@ -90,10 +104,11 @@ CREATE_RELATED1 :{
 
 CREATE_RELATED2 :{
 
+       my $artist = $schema->resultset('Artist')->first;
        
-       my $cd_result = $schema->resultset('Artist')->first->create_related('cds', {
+       my $cd_result = $artist->create_related('cds', {
        
-               title => 'TestOneCD1',
+               title => 'TestOneCD2',
                year => 2007,
                tracks => [
                
@@ -105,10 +120,13 @@ CREATE_RELATED2 :{
                        }
                ],
 
+    liner_notes => { notes => 'I can haz liner notes?' },
+
        });
        
        ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
-       ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
+       ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
+  ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
        
        my $tracks = $cd_result->tracks;
        
@@ -120,8 +138,69 @@ CREATE_RELATED2 :{
        }
 }
 
-my $now = DateTime->now;
+my $cdp = $schema->resultset('CD_to_Producer')->create({
+            cd => { artist => 1, title => 'foo', year => 2000 },
+            producer => { name => 'jorge' }
+          });
+
+ok($cdp, 'join table record created ok');
+
+SPECIAL_CASE: {
+  my $kurt_cobain = { name => 'Kurt Cobain' };
+
+  my $in_utero = $schema->resultset('CD')->new({
+      title => 'In Utero',
+      year  => 1993
+    });
+
+  $kurt_cobain->{cds} = [ $in_utero ];
+
+
+  $schema->resultset('Artist')->populate([ $kurt_cobain ]); # %)
+  $a = $schema->resultset('Artist')->find({name => 'Kurt Cobain'});
+
+  is($a->name, 'Kurt Cobain', 'Artist insertion ok');
+  is($a->cds && $a->cds->first && $a->cds->first->title, 
+                 'In Utero', 'CD insertion ok');
+}
+
+SPECIAL_CASE2: {
+  my $pink_floyd = { name => 'Pink Floyd' };
+
+  my $the_wall = { title => 'The Wall', year  => 1979 };
+
+  $pink_floyd->{cds} = [ $the_wall ];
+
+
+  $schema->resultset('Artist')->populate([ $pink_floyd ]); # %)
+  $a = $schema->resultset('Artist')->find({name => 'Pink Floyd'});
+
+  is($a->name, 'Pink Floyd', 'Artist insertion ok');
+  is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok');
+}
+
+## Create foreign key col obj including PK
+## See test 20 in 66relationships.t
+my $new_cd_hashref = { 
+              cdid => 27, 
+               title => 'Boogie Woogie', 
+              year => '2007', 
+              artist => { artistid => 17, name => 'king luke' }
+             };
+
+my $cd = $schema->resultset("CD")->find(1);
+
+is($cd->artist->id, 1, 'rel okay');
+
+my $new_cd = $schema->resultset("CD")->create($new_cd_hashref);
+is($new_cd->artist->id, 17, 'new id retained okay');
+
 
-ok( $schema->resultset('Event')
-           ->create( { starts_at => $now, created_on => $now } ),
-  'object with inflated non-rels ok');
+# Make sure exceptions from errors in created rels propogate
+eval {
+    my $t = $schema->resultset("Track")->new({});
+    $t->cd($t->new_related('cd', { artist => undef } ) );
+    $t->{_rel_in_storage} = 0;
+    $t->insert;
+};
+like($@, qr/cd.artist may not be NULL/, "Exception propogated properly");