Merge 'trunk' into 'multicreate_fixes'
[dbsrgits/DBIx-Class.git] / t / multi_create / multilev_might_have_PKeqFK.t
CommitLineData
68b92e84 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6use lib qw(t/lib);
7use DBICTest;
8
9sub mc_diag { diag (@_) if $ENV{DBIC_MULTICREATE_DEBUG} };
10
9aa34b29 11plan tests => 13;
68b92e84 12
13my $schema = DBICTest->init_schema();
14
15mc_diag (<<'DG');
16* Test a multilevel might-have with a PK == FK in the might_have/has_many table
17
18CD -> might have -> Artwork
19 \
20 \-> has_many \
21 --> Artwork_to_Artist
22 /-> has_many /
23 /
24 Artist
25DG
26
27lives_ok (sub {
68b92e84 28 my $cd = $schema->resultset('CD')->create ({
9aa34b29 29 artist => { name => 'the cincinnati kid' },
68b92e84 30 title => 'Music to code by until the cows come home',
31 year => 2008,
32 artwork => {
33 artwork_to_artist => [
34 { artist => { name => 'cowboy joe' } },
35 { artist => { name => 'billy the kid' } },
36 ],
37 },
38 });
39
40 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
41 is ($cd->title, 'Music to code by until the cows come home', 'Correct CD title');
9aa34b29 42 is ($cd->artist->name, 'the cincinnati kid', 'Correct artist created for CD');
68b92e84 43
44 my $art_obj = $cd->artwork;
45 ok ($art_obj->has_column_loaded ('cd_id'), 'PK/FK present on artwork object');
46 is ($art_obj->artists->count, 2, 'Correct artwork creator count via the new object');
47 is_deeply (
48 [ sort $art_obj->artists->get_column ('name')->all ],
49 [ 'billy the kid', 'cowboy joe' ],
50 'Artists named correctly when queried via object',
51 );
52
53 my $artwork = $schema->resultset('Artwork')->search (
54 { 'cd.title' => 'Music to code by until the cows come home' },
55 { join => 'cd' },
56 )->single;
57 is ($artwork->artists->count, 2, 'Correct artwork creator count via a new search');
58 is_deeply (
59 [ sort $artwork->artists->get_column ('name')->all ],
60 [ 'billy the kid', 'cowboy joe' ],
61 'Artists named correctly queried via a new search',
62 );
63}, 'multilevel might-have with a PK == FK in the might_have/has_many table ok');
64
9aa34b29 65
66mc_diag (<<'DG');
67* Try the same as above in a different direction
68
69Artist -> has_many -> Artwork_to_Artist -> belongs_to
70 /
71 belongs_to <- CD <- belongs_to <- Artwork <-/
72 \
73 \-> Artist2
74
75DG
76
77lives_ok (sub {
78 $schema->resultset ('Artist')->create ({
79 name => 'The wooled wolf',
d65f2cde 80 artwork_to_artist => [{
9aa34b29 81 artwork => {
82 cd => {
83 title => 'Wool explosive',
84 year => 1999,
85 artist => { name => 'The black exploding sheep' },
86 }
87 }
88 }],
89 });
90
91 my $art2 = $schema->resultset ('Artist')->find ({ name => 'The black exploding sheep' });
92 ok ($art2, 'Second artist exists');
93
94 my $cd = $art2->cds->single;
95 is ($cd->title, 'Wool explosive', 'correctly created CD');
96
97 is_deeply (
98 [ $cd->artwork->artists->get_column ('name')->all ],
99 [ 'The wooled wolf' ],
100 'Artist correctly attached to artwork',
101 );
102
103}, 'Diamond chain creation ok');
104
68b92e84 1051;