Fix updates with multi-create syntax
[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 => 6;
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 my $newartist2 = $schema->resultset('Artist')->find_or_create({ name => 'Fred 3',
45                                                                 cds => [
46                                                                         { title => 'Noah Act',
47                                                                           year => 2007,
48                                                                         },
49                                                                        ],
50
51                                                               });
52
53 is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');