0.08001 because I'm an idiot
[dbsrgits/DBIx-Class.git] / t / 96multi_create.t
index 205ba4f..23badf9 100644 (file)
@@ -1,5 +1,5 @@
 use strict;
-use warnings;  
+use warnings;
 
 use Test::More;
 use lib qw(t/lib);
@@ -7,14 +7,7 @@ use DBICTest;
 
 my $schema = DBICTest->init_schema();
 
-plan tests => 1;
-
-my $artist = $schema->resultset('Artist')->create({ name => 'Fred 1'});
-
-my $cd = $schema->resultset('CD')->create({ artist => $artist,
-                                            title => 'Some CD',
-                                            year => 1996
-                                          });
+plan tests => 17;
 
 my $cd2 = $schema->resultset('CD')->create({ artist => 
                                    { name => 'Fred Bloggs' },
@@ -22,4 +15,107 @@ my $cd2 = $schema->resultset('CD')->create({ artist =>
                                    year => 1996
                                  });
 
-is(ref $cd->artist, 'DBICTest::Artist', 'Created CD and Artist object');
+is(ref $cd2->artist, 'DBICTest::Artist', 'Created CD and Artist object');
+is($cd2->artist->name, 'Fred Bloggs', 'Artist created correctly');
+
+my $artist = $schema->resultset('Artist')->create({ name => 'Fred 2',
+                                                     cds => [
+                                                             { title => 'Music to code by',
+                                                               year => 2007,
+                                                             },
+                                                             ],
+                                                     });
+is(ref $artist->cds->first, 'DBICTest::CD', 'Created Artist with CDs');
+is($artist->cds->first->title, 'Music to code by', 'CD created correctly');
+
+# Add a new CD
+$artist->update({cds => [ $artist->cds, 
+                          { title => 'Yet another CD',
+                            year => 2006,
+                          },
+                        ],
+                });
+is(($artist->cds->search({}, { order_by => 'year' }))[0]->title, 'Yet another CD', 'Updated and added another CD');
+
+my $newartist = $schema->resultset('Artist')->find_or_create({ name => 'Fred 2'});
+
+is($newartist->name, 'Fred 2', 'Retrieved the artist');
+
+
+my $newartist2 = $schema->resultset('Artist')->find_or_create({ name => 'Fred 3',
+                                                                cds => [
+                                                                        { title => 'Noah Act',
+                                                                          year => 2007,
+                                                                        },
+                                                                       ],
+
+                                                              });
+
+is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
+
+
+CREATE_RELATED1 :{
+
+       my $artist = $schema->resultset('Artist')->first;
+       
+       my $cd_result = $artist->create_related('cds', {
+       
+               title => 'TestOneCD1',
+               year => 2007,
+               tracks => [
+               
+                       { position=>111,
+                         title => 'TrackOne',
+                       },
+                       { position=>112,
+                         title => 'TrackTwo',
+                       }
+               ],
+
+       });
+       
+       ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
+       ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
+       
+       my $tracks = $cd_result->tracks;
+       
+       ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
+       
+       foreach my $track ($tracks->all)
+       {
+               ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
+       }
+}
+
+CREATE_RELATED2 :{
+
+       my $artist = $schema->resultset('Artist')->first;
+       
+       my $cd_result = $artist->create_related('cds', {
+       
+               title => 'TestOneCD2',
+               year => 2007,
+               tracks => [
+               
+                       { position=>111,
+                         title => 'TrackOne',
+                       },
+                       { position=>112,
+                         title => 'TrackTwo',
+                       }
+               ],
+
+       });
+       
+       ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
+       ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
+       
+       my $tracks = $cd_result->tracks;
+       
+       ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
+       
+       foreach my $track ($tracks->all)
+       {
+               ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
+       }
+}