dt test
[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 => 12;
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         
94         my $cd_result = $schema->resultset('Artist')->first->create_related('cds', {
95         
96                 title => 'TestOneCD1',
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 "TestOneCD1", "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 }
122
123 my $now = DateTime->now;
124
125 ok( $schema->resultset('Event')
126            ->create( { starts_at => $now, created_on => $now } ),
127   'object with inflated non-rels ok');