ac5f2195b708b7b6f09811fd36fb3abe3b985e4e
[dbsrgits/DBIx-Class.git] / t / 96multi_create.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7 use DateTime;
8
9 my $schema = DBICTest->init_schema();
10
11 plan tests => 17;
12
13 my $cd2 = $schema->resultset('CD')->create({ artist => 
14                                    { name => 'Fred Bloggs' },
15                                    title => 'Some CD',
16                                    year => 1996
17                                  });
18
19 is(ref $cd2->artist, 'DBICTest::Artist', 'Created CD and Artist object');
20 is($cd2->artist->name, 'Fred Bloggs', 'Artist created correctly');
21
22 my $artist = $schema->resultset('Artist')->create({ name => 'Fred 2',
23                                                      cds => [
24                                                              { title => 'Music to code by',
25                                                                year => 2007,
26                                                              },
27                                                              ],
28                                                      });
29 is(ref $artist->cds->first, 'DBICTest::CD', 'Created Artist with CDs');
30 is($artist->cds->first->title, 'Music to code by', 'CD created correctly');
31
32 # Add a new CD
33 $artist->update({cds => [ $artist->cds, 
34                           { title => 'Yet another CD',
35                             year => 2006,
36                           },
37                         ],
38                 });
39 is(($artist->cds->search({}, { order_by => 'year' }))[0]->title, 'Yet another CD', 'Updated and added another CD');
40
41 my $newartist = $schema->resultset('Artist')->find_or_create({ name => 'Fred 2'});
42
43 is($newartist->name, 'Fred 2', 'Retrieved the artist');
44
45
46 my $newartist2 = $schema->resultset('Artist')->find_or_create({ name => 'Fred 3',
47                                                                 cds => [
48                                                                         { title => 'Noah Act',
49                                                                           year => 2007,
50                                                                         },
51                                                                        ],
52
53                                                               });
54
55 is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
56
57
58 CREATE_RELATED1 :{
59
60         my $artist = $schema->resultset('Artist')->first;
61         
62         my $cd_result = $artist->create_related('cds', {
63         
64                 title => 'TestOneCD1',
65                 year => 2007,
66                 tracks => [
67                 
68                         { position=>111,
69                           title => 'TrackOne',
70                         },
71                         { position=>112,
72                           title => 'TrackTwo',
73                         }
74                 ],
75
76         });
77         
78         ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
79         ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
80         
81         my $tracks = $cd_result->tracks;
82         
83         ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
84         
85         foreach my $track ($tracks->all)
86         {
87                 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
88         }
89 }
90
91 CREATE_RELATED2 :{
92
93         my $artist = $schema->resultset('Artist')->first;
94         
95         my $cd_result = $artist->create_related('cds', {
96         
97                 title => 'TestOneCD2',
98                 year => 2007,
99                 tracks => [
100                 
101                         { position=>111,
102                           title => 'TrackOne',
103                         },
104                         { position=>112,
105                           title => 'TrackTwo',
106                         }
107                 ],
108
109         });
110         
111         ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
112         ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
113         
114         my $tracks = $cd_result->tracks;
115         
116         ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
117         
118         foreach my $track ($tracks->all)
119         {
120                 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
121         }
122 }