Commit | Line | Data |
68b92e84 |
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 | plan tests => 8; |
12 | |
13 | my $schema = DBICTest->init_schema(); |
14 | |
15 | mc_diag (<<'DG'); |
16 | * Test a multilevel might-have with a PK == FK in the might_have/has_many table |
17 | |
18 | CD -> might have -> Artwork |
19 | \ |
20 | \-> has_many \ |
21 | --> Artwork_to_Artist |
22 | /-> has_many / |
23 | / |
24 | Artist |
25 | DG |
26 | |
27 | lives_ok (sub { |
28 | my $someartist = $schema->resultset('Artist')->first; |
29 | my $cd = $schema->resultset('CD')->create ({ |
30 | artist => $someartist, |
31 | title => 'Music to code by until the cows come home', |
32 | year => 2008, |
33 | artwork => { |
34 | artwork_to_artist => [ |
35 | { artist => { name => 'cowboy joe' } }, |
36 | { artist => { name => 'billy the kid' } }, |
37 | ], |
38 | }, |
39 | }); |
40 | |
41 | isa_ok ($cd, 'DBICTest::CD', 'Main CD object created'); |
42 | is ($cd->title, 'Music to code by until the cows come home', 'Correct CD title'); |
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 | |
65 | 1; |