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