Extra test to demonstrate has_one working, and a POD clarification of multicreate
[dbsrgits/DBIx-Class.git] / t / multi_create / multilev_single_PKeqFK.t
CommitLineData
cc9d96d0 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
9c3018b6 11plan tests => 26;
cc9d96d0 12
13my $schema = DBICTest->init_schema();
14
15mc_diag (<<'DG');
16* Test a multilevel might-have/has_one with a PK == FK in the mid-table
17
18CD -> might have -> Artwork
19 \- has_one -/ \
20 \
21 \-> has_many \
22 --> Artwork_to_Artist
23 /-> has_many /
24 /
25 Artist
26DG
27
28my $rels = {
29 has_one => 'mandatory_artwork',
30 might_have => 'artwork',
31};
32
9c3018b6 33for my $type (qw/has_one might_have/) {
34
35 lives_ok (sub {
cc9d96d0 36
9c3018b6 37 my $rel = $rels->{$type};
38 my $cd_title = "Simple test $type cd";
39
40 my $cd = $schema->resultset('CD')->create ({
41 artist => 1,
42 title => $cd_title,
43 year => 2008,
44 $rel => {},
45 });
46
47 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
48 is ($cd->title, $cd_title, 'Correct CD title');
49
50 isa_ok ($cd->$rel, 'DBICTest::Artwork', 'Related artwork present');
51 ok ($cd->$rel->in_storage, 'And in storage');
52
53 }, "Simple $type creation");
54}
55
56my $artist_rs = $schema->resultset('Artist');
cc9d96d0 57for my $type (qw/has_one might_have/) {
58
59 my $rel = $rels->{$type};
60
61 my $cd_title = "Test $type cd";
62 my $artist_names = [ map { "Artist via $type $_" } (1, 2) ];
63
64 my $someartist = $artist_rs->next;
65
66 lives_ok (sub {
67 my $cd = $schema->resultset('CD')->create ({
68 artist => $someartist,
69 title => $cd_title,
70 year => 2008,
71 $rel => {
72 artwork_to_artist => [ map {
73 { artist => { name => $_ } }
74 } (@$artist_names)
75 ]
76 },
77 });
78
79
80 isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
81 is ($cd->title, $cd_title, 'Correct CD title');
82
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');
86 is_deeply (
87 [ sort $art_obj->artists->get_column ('name')->all ],
88 $artist_names,
89 'Artists named correctly when queried via object',
90 );
91
92 my $artwork = $schema->resultset('Artwork')->search (
93 { 'cd.title' => $cd_title },
94 { join => 'cd' },
95 )->single;
96 is ($artwork->artists->count, 2, 'Correct artwork creator count via a new search');
97 is_deeply (
98 [ sort $artwork->artists->get_column ('name')->all ],
99 $artist_names,
100 'Artists named correctly queried via a new search',
101 );
102 }, "multilevel $type with a PK == FK in the $type/has_many table ok");
103}
104
1051;