fixup for stringify that can be false in find_or_create_related
[dbsrgits/DBIx-Class.git] / t / run / 06relationship.tl
1 sub run_tests {
2 my $schema = shift;
3
4 use strict;
5 use warnings;  
6 plan tests => 26;
7
8 # has_a test
9 my $cd = $schema->resultset("CD")->find(4);
10 my ($artist) = ($INC{'DBICTest/HelperRels'}
11                   ? $cd->artist
12                   : $cd->search_related('artist'));
13 is($artist->name, 'Random Boy Band', 'has_a search_related ok');
14
15 # has_many test with an order_by clause defined
16 $artist = $schema->resultset("Artist")->find(1);
17 my @cds = ($INC{'DBICTest/HelperRels'}
18              ? $artist->cds
19              : $artist->search_related('cds'));
20 is( $cds[1]->title, 'Spoonful of bees', 'has_many search_related with order_by ok' );
21
22 # search_related with additional abstract query
23 @cds = ($INC{'DBICTest/HelperRels'}
24           ? $artist->cds({ title => { like => '%of%' } })
25           : $artist->search_related('cds', { title => { like => '%of%' } } )
26        );
27 is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' );
28
29 # creating a related object
30 if ($INC{'DBICTest/HelperRels.pm'}) {
31   $artist->add_to_cds({ title => 'Big Flop', year => 2005 });
32 } else {
33   $artist->create_related( 'cds', {
34       title => 'Big Flop',
35       year => 2005,
36   } );
37 }
38
39 is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' );
40
41 # count_related
42 is( $artist->count_related('cds'), 4, 'count_related ok' );
43
44 # set_from_related
45 my $track = $schema->resultset("Track")->create( {
46   trackid => 1,
47   cd => 3,
48   position => 98,
49   title => 'Hidden Track'
50 } );
51 $track->set_from_related( cd => $cd );
52
53 if ($INC{'DBICTest/HelperRels.pm'}) { # expect inflated object
54   is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' );
55 } else {
56   is( $track->cd, 4, 'set_from_related ok' );
57 }
58
59 $track->set_from_related( cd => undef );
60
61 ok( !defined($track->cd), 'set_from_related with undef ok');
62
63
64 # update_from_related, the same as set_from_related, but it calls update afterwards
65 $track = $schema->resultset("Track")->create( {
66   trackid => 2,
67   cd => 3,
68   position => 99,
69   title => 'Hidden Track'
70 } );
71 $track->update_from_related( cd => $cd );
72
73 my $t_cd = ($schema->resultset("Track")->search( cd => 4, position => 99 ))[0]->cd;
74
75 if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
76   is( $t_cd->cdid, 4, 'update_from_related ok' );
77 } else {
78   is( $t_cd, 4, 'update_from_related ok' );
79 }
80
81 # find_or_create_related with an existing record
82 $cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } );
83 is( $cd->year, 2005, 'find_or_create_related on existing record ok' );
84
85 # find_or_create_related creating a new record
86 $cd = $artist->find_or_create_related( 'cds', {
87   title => 'Greatest Hits',
88   year => 2006,
89 } );
90 is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' );
91 @cds = $artist->search_related('cds');
92 is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' );
93
94 $artist->delete_related( cds => { title => 'Greatest Hits' });
95 cmp_ok( $schema->resultset("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' );
96
97 SKIP: {
98   skip "relationship checking needs fixing", 1;
99   # try to add a bogus relationship using the wrong cols
100   eval {
101       DBICTest::Schema::Artist->add_relationship(
102           tracks => 'DBICTest::Schema::Track',
103           { 'foreign.cd' => 'self.cdid' }
104       );
105   };
106   like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok');
107 }
108   
109 # another bogus relationship using no join condition
110 eval {
111     DBICTest::Schema::Artist->add_relationship( tracks => 'DBICTest::Track' );
112 };
113 like($@, qr/join condition/, 'failed when creating a rel without join condition, ok');
114
115 # many_to_many helper test
116 $cd = $schema->resultset("CD")->find(1);
117 my @producers = $cd->producers();
118 is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' );
119 is( $cd->producers_sorted->next->name, 'Bob The Builder', 'sorted many_to_many ok' );
120 is( $cd->producers_sorted(producerid => 3)->next->name, 'Fred The Phenotype', 'sorted many_to_many with search condition ok' );
121
122 # test undirected many-to-many relationship (e.g. "related artists")
123 my $undir_maps = $schema->resultset("Artist")->find(1)->artist_undirected_maps;
124 is($undir_maps->count, 1, 'found 1 undirected map for artist 1');
125
126 $undir_maps = $schema->resultset("Artist")->find(2)->artist_undirected_maps;
127 is($undir_maps->count, 1, 'found 1 undirected map for artist 2');
128
129 my $mapped_rs = $undir_maps->search_related('mapped_artists');
130
131 my @art = $mapped_rs->all;
132
133 cmp_ok(@art, '==', 2, "Both artist returned from map");
134
135 my $searched = $mapped_rs->search({'mapped_artists.artistid' => {'!=', undef}});
136
137 cmp_ok($searched->count, '==', 2, "Both artist returned from map after adding another condition");
138
139 # check join through cascaded has_many relationships
140 $artist = $schema->resultset("Artist")->find(1);
141 my $trackset = $artist->cds->search_related('tracks');
142 # LEFT join means we also see the trackless additional album...
143 cmp_ok($trackset->count, '==', 11, "Correct number of tracks for artist");
144
145 # now see about updating eveything that belongs to artist 2 to artist 3
146 $artist = $schema->resultset("Artist")->find(2);
147 my $nartist = $schema->resultset("Artist")->find(3);
148 cmp_ok($artist->cds->count, '==', 1, "Correct orig #cds for artist");
149 cmp_ok($nartist->cds->count, '==', 1, "Correct orig #cds for artist");
150 $artist->cds->update({artist => $nartist->id});
151 cmp_ok($artist->cds->count, '==', 0, "Correct new #cds for artist");
152 cmp_ok($nartist->cds->count, '==', 2, "Correct new #cds for artist");
153
154 }
155
156 1;