499f7a13bd9a94829d698840eea06b22b8284051
[dbsrgits/DBIx-Class.git] / t / multi_create / diamond.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 sub mc_diag { diag (@_) if $ENV{DBIC_MULTICREATE_DEBUG} };
10
11 my $schema = DBICTest->init_schema();
12
13 mc_diag (<<'DG');
14 * Try a diamond multicreate
15
16 Artist -> has_many -> Artwork_to_Artist -> belongs_to
17                                                /
18   belongs_to <- CD <- belongs_to <- Artwork <-/
19     \
20      \-> Artist2
21
22 DG
23
24 lives_ok (sub {
25   $schema->resultset ('Artist')->create ({
26     name => 'The wooled wolf',
27     artwork_to_artist => [{
28       artwork => {
29         cd => {
30           title => 'Wool explosive',
31           year => 1999,
32           artist => { name => 'The black exploding sheep' },
33         }
34       }
35     }],
36   });
37
38   my $art2 = $schema->resultset ('Artist')->find ({ name => 'The black exploding sheep' });
39   ok ($art2, 'Second artist exists');
40
41   my $cd = $art2->cds->single;
42   is ($cd->title, 'Wool explosive', 'correctly created CD');
43
44   is_deeply (
45     [ $cd->artwork->artists->get_column ('name')->all ],
46     [ 'The wooled wolf' ],
47     'Artist correctly attached to artwork',
48   );
49
50 }, 'Diamond chain creation ok');
51
52 done_testing;