9 sub mc_diag { diag (@_) if $ENV{DBIC_MULTICREATE_DEBUG} };
11 my $schema = DBICTest->init_schema();
14 * Test a multilevel might-have/has_one with a PK == FK in the mid-table
16 CD -> might have -> Artwork
27 has_one => 'mandatory_artwork',
28 might_have => 'artwork',
31 for my $type (qw/has_one might_have/) {
35 my $rel = $rels->{$type};
36 my $cd_title = "Simple test $type cd";
38 my $cd = $schema->resultset('CD')->create ({
45 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
46 is ($cd->title, $cd_title, 'Correct CD title');
48 isa_ok ($cd->$rel, 'DBICTest::Artwork', 'Related artwork present');
49 ok ($cd->$rel->in_storage, 'And in storage');
51 }, "Simple $type creation");
54 my $artist_rs = $schema->resultset('Artist');
55 for my $type (qw/has_one might_have/) {
57 my $rel = $rels->{$type};
59 my $cd_title = "Test $type cd";
60 my $artist_names = [ map { "Artist via $type $_" } (1, 2) ];
62 my $someartist = $artist_rs->next;
65 my $cd = $schema->resultset('CD')->create ({
66 artist => $someartist,
70 artwork_to_artist => [ map {
71 { artist => { name => $_ } }
78 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
79 is ($cd->title, $cd_title, 'Correct CD title');
81 my $art_obj = $cd->$rel;
82 ok ($art_obj->has_column_loaded ('cd_id'), 'PK/FK present on artwork object');
83 is ($art_obj->artists->count, 2, 'Correct artwork creator count via the new object');
85 [ sort $art_obj->artists->get_column ('name')->all ],
87 'Artists named correctly when queried via object',
90 my $artwork = $schema->resultset('Artwork')->search (
91 { 'cd.title' => $cd_title },
94 is ($artwork->artists->count, 2, 'Correct artwork creator count via a new search');
96 [ sort $artwork->artists->get_column ('name')->all ],
98 'Artists named correctly queried via a new search',
100 }, "multilevel $type with a PK == FK in the $type/has_many table ok");