c4464a810763e250f2f00d2d3ba87ecac20019ca
[dbsrgits/DBIx-Class.git] / t / multi_create / existing_in_chain.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8
9 plan 'no_plan';
10
11 my $schema = DBICTest->init_schema();
12
13 {
14   my $counts;
15   $counts->{$_} = $schema->resultset($_)->count for qw/Track CD Genre/;
16
17   lives_ok (sub {
18     my $existing_nogen_cd = $schema->resultset('CD')->search (
19       { 'genre.genreid' => undef },
20       { join => 'genre' },
21     )->first;
22
23     $schema->resultset('Track')->create ({
24       title => 'Sugar-coated',
25       cd => {
26         title => $existing_nogen_cd->title,
27         genre => {
28           name => 'sugar genre',
29         }
30       }
31     });
32
33     is ($schema->resultset('Track')->count, $counts->{Track} + 1, '1 new track');
34     is ($schema->resultset('CD')->count, $counts->{CD}, 'No new cds');
35     is ($schema->resultset('Genre')->count, $counts->{Genre} + 1, '1 new genre');
36
37     is ($existing_nogen_cd->genre->title,  'sugar genre', 'Correct genre assigned to CD');
38   });
39 }
40
41 {
42   my $counts;
43   $counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Producer/;
44
45   lives_ok (sub {
46     my $artist = $schema->resultset('Artist')->first;
47     my $producer = $schema->resultset('Producer')->create ({ name => 'the queen of england' });
48
49     $schema->resultset('CD')->create ({
50       artist => $artist,
51       title => 'queen1',
52       year => 2007,
53       cd_to_producer => [
54         {
55           producer => {
56           name => $producer->name,
57             producer_to_cd => [
58               {
59                 cd => {
60                   title => 'queen2',
61                   year => 2008,
62                   artist => $artist,
63                 },
64               },
65             ],
66           },
67         },
68       ],
69     });
70
71     is ($schema->resultset('Artist')->count, $counts->{Artist}, 'No new artists');
72     is ($schema->resultset('Producer')->count, $counts->{Producer} + 1, '1 new producers');
73     is ($schema->resultset('CD')->count, $counts->{CD} + 2, '2 new cds');
74
75     is ($producer->cds->count, 2, 'CDs assigned to correct producer');
76     is_deeply (
77       [ $producer->cds->search ({}, { order_by => 'title' })->get_column('title')->all],
78       [ qw/queen1 queen2/ ],
79       'Correct cd names',
80     );
81   });
82 }
83
84 1;