Improved join condition possiblities - arrayrefs of hashrefs now work for OR
[dbsrgits/DBIx-Class.git] / t / run / 06relationship.tl
CommitLineData
0567538f 1sub run_tests {
5efe4c79 2
3use strict;
4use warnings;
5plan tests => 17;
0567538f 6
7# has_a test
3712e4f4 8my $cd = DBICTest->class("CD")->find(4);
07037f89 9my ($artist) = ($INC{'DBICTest/HelperRels'}
10 ? $cd->artist
11 : $cd->search_related('artist'));
0567538f 12is($artist->name, 'Random Boy Band', 'has_a search_related ok');
13
14# has_many test with an order_by clause defined
3712e4f4 15$artist = DBICTest->class("Artist")->find(1);
07037f89 16my @cds = ($INC{'DBICTest/HelperRels'}
17 ? $artist->cds
18 : $artist->search_related('cds'));
19is( $cds[1]->title, 'Spoonful of bees', 'has_many search_related with order_by ok' );
0567538f 20
21# search_related with additional abstract query
07037f89 22@cds = ($INC{'DBICTest/HelperRels'}
23 ? $artist->cds({ title => { like => '%of%' } })
24 : $artist->search_related('cds', { title => { like => '%of%' } } )
25 );
0567538f 26is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' );
27
28# creating a related object
07037f89 29if ($INC{'DBICTest/HelperRels.pm'}) {
30 $artist->add_to_cds({ title => 'Big Flop', year => 2005 });
31} else {
32 $artist->create_related( 'cds', {
33 title => 'Big Flop',
34 year => 2005,
35 } );
36}
37
0567538f 38is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' );
39
40# count_related
41is( $artist->count_related('cds'), 4, 'count_related ok' );
42
43# set_from_related
3712e4f4 44my $track = DBICTest->class("Track")->create( {
0567538f 45 trackid => 1,
46 cd => 3,
47 position => 98,
48 title => 'Hidden Track'
49} );
50$track->set_from_related( cd => $cd );
51
52if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
1e3bc087 53 is($track->disc->cdid, 4, 'set_from_related ok, including alternative accessor' );
0567538f 54} else {
55 is( $track->cd, 4, 'set_from_related ok' );
56}
57
58# update_from_related, the same as set_from_related, but it calls update afterwards
3712e4f4 59$track = DBICTest->class("Track")->create( {
0567538f 60 trackid => 2,
61 cd => 3,
62 position => 99,
63 title => 'Hidden Track'
64} );
65$track->update_from_related( cd => $cd );
66
3712e4f4 67my $t_cd = (DBICTest->class("Track")->search( cd => 4, position => 99 ))[0]->cd;
0567538f 68
69if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
70 is( $t_cd->cdid, 4, 'update_from_related ok' );
71} else {
72 is( $t_cd, 4, 'update_from_related ok' );
73}
74
75# find_or_create_related with an existing record
76$cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } );
77is( $cd->year, 2005, 'find_or_create_related on existing record ok' );
78
79# find_or_create_related creating a new record
80$cd = $artist->find_or_create_related( 'cds', {
81 title => 'Greatest Hits',
82 year => 2006,
83} );
84is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' );
85@cds = $artist->search_related('cds');
86is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' );
87
87772e46 88$artist->delete_related( cds => { title => 'Greatest Hits' });
89cmp_ok( DBICTest->class("CD")->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' );
0567538f 90
87772e46 91SKIP: {
92 skip "relationship checking needs fixing", 1;
93 # try to add a bogus relationship using the wrong cols
94 eval {
95 DBICTest::Schema::Artist->add_relationship(
96 tracks => 'DBICTest::Schema::Track',
97 { 'foreign.cd' => 'self.cdid' }
98 );
99 };
100 like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok');
101}
102
0567538f 103# another bogus relationship using no join condition
104eval {
3712e4f4 105 DBICTest::Schema::Artist->add_relationship( tracks => 'DBICTest::Track' );
0567538f 106};
107like($@, qr/join condition/, 'failed when creating a rel without join condition, ok');
108
7411204b 109# many_to_many helper test
110$cd = DBICTest->class("CD")->find(1);
111my @producers = $cd->producers();
112is( $producers[0]->name, 'Matt S Trout', 'many_to_many ok' );
113
5efe4c79 114# test undirected many-to-many relationship (e.g. "related artists")
115my $undir_maps = DBICTest->class("Artist")->find(1)->artist_undirected_maps;
116is($undir_maps->count, 1, 'found 1 undirected map for artist 1');
117
118$undir_maps = DBICTest->class("Artist")->find(2)->artist_undirected_maps;
119is($undir_maps->count, 1, 'found 1 undirected map for artist 2');
120
121my @art = $undir_maps->search_related('mapped_artists')->all;
122
123cmp_ok(@art, '==', 2, "Both artist returned from map");
124
0567538f 125}
126
1271;