9 sub mc_diag { diag (@_) if $ENV{DBIC_MULTICREATE_DEBUG} };
13 my $schema = DBICTest->init_schema();
16 * Test a multilevel might-have/has_one with a PK == FK in the mid-table
18 CD -> might have -> Artwork
29 has_one => 'mandatory_artwork',
30 might_have => 'artwork',
33 for my $type (qw/has_one might_have/) {
37 my $rel = $rels->{$type};
38 my $cd_title = "Simple test $type cd";
40 my $cd = $schema->resultset('CD')->create ({
47 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
48 is ($cd->title, $cd_title, 'Correct CD title');
50 isa_ok ($cd->$rel, 'DBICTest::Artwork', 'Related artwork present');
51 ok ($cd->$rel->in_storage, 'And in storage');
53 }, "Simple $type creation");
56 my $artist_rs = $schema->resultset('Artist');
57 for my $type (qw/has_one might_have/) {
59 my $rel = $rels->{$type};
61 my $cd_title = "Test $type cd";
62 my $artist_names = [ map { "Artist via $type $_" } (1, 2) ];
64 my $someartist = $artist_rs->next;
67 my $cd = $schema->resultset('CD')->create ({
68 artist => $someartist,
72 artwork_to_artist => [ map {
73 { artist => { name => $_ } }
80 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
81 is ($cd->title, $cd_title, 'Correct CD title');
83 my $art_obj = $cd->$rel;
84 ok ($art_obj->has_column_loaded ('cd_id'), 'PK/FK present on artwork object');
85 is ($art_obj->artists->count, 2, 'Correct artwork creator count via the new object');
87 [ sort $art_obj->artists->get_column ('name')->all ],
89 'Artists named correctly when queried via object',
92 my $artwork = $schema->resultset('Artwork')->search (
93 { 'cd.title' => $cd_title },
96 is ($artwork->artists->count, 2, 'Correct artwork creator count via a new search');
98 [ sort $artwork->artists->get_column ('name')->all ],
100 'Artists named correctly queried via a new search',
102 }, "multilevel $type with a PK == FK in the $type/has_many table ok");
107 * Try a diamond multicreate
109 Artist -> has_many -> Artwork_to_Artist -> belongs_to
111 belongs_to <- CD <- belongs_to <- Artwork <-/
118 $schema->resultset ('Artist')->create ({
119 name => 'The wooled wolf',
120 artwork_to_artist => [{
123 title => 'Wool explosive',
125 artist => { name => 'The black exploding sheep' },
131 my $art2 = $schema->resultset ('Artist')->find ({ name => 'The black exploding sheep' });
132 ok ($art2, 'Second artist exists');
134 my $cd = $art2->cds->single;
135 is ($cd->title, 'Wool explosive', 'correctly created CD');
138 [ $cd->artwork->artists->get_column ('name')->all ],
139 [ 'The wooled wolf' ],
140 'Artist correctly attached to artwork',
143 }, 'Diamond chain creation ok');