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