Yeah, committing the new tests would help ...
[dbsrgits/DBIx-Class.git] / t / run / 06relationship.tl
CommitLineData
0567538f 1sub run_tests {
2
3plan tests => 13;
4
5# has_a test
6my $cd = DBICTest::CD->find(4);
7my ($artist) = $cd->search_related('artist');
8is($artist->name, 'Random Boy Band', 'has_a search_related ok');
9
10# has_many test with an order_by clause defined
11$artist = DBICTest::Artist->find(1);
12is( ($artist->search_related('cds'))[1]->title, 'Spoonful of bees', 'has_many search_related with order_by ok' );
13
14# search_related with additional abstract query
15my @cds = $artist->search_related('cds', { title => { like => '%of%' } } );
16is( $cds[1]->title, 'Forkful of bees', 'search_related with abstract query ok' );
17
18# creating a related object
19$artist->create_related( 'cds', {
20 title => 'Big Flop',
21 year => 2005,
22} );
23is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' );
24
25# count_related
26is( $artist->count_related('cds'), 4, 'count_related ok' );
27
28# set_from_related
29my $track = DBICTest::Track->create( {
30 trackid => 1,
31 cd => 3,
32 position => 98,
33 title => 'Hidden Track'
34} );
35$track->set_from_related( cd => $cd );
36
37if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
38 is($track->cd->cdid, 4, 'set_from_related ok' );
39} else {
40 is( $track->cd, 4, 'set_from_related ok' );
41}
42
43# update_from_related, the same as set_from_related, but it calls update afterwards
44$track = DBICTest::Track->create( {
45 trackid => 2,
46 cd => 3,
47 position => 99,
48 title => 'Hidden Track'
49} );
50$track->update_from_related( cd => $cd );
51
52my $t_cd = (DBICTest::Track->search( cd => 4, position => 99 ))[0]->cd;
53
54if ($INC{'DBICTest/HelperRels.pm'}) { # except inflated object
55 is( $t_cd->cdid, 4, 'update_from_related ok' );
56} else {
57 is( $t_cd, 4, 'update_from_related ok' );
58}
59
60# find_or_create_related with an existing record
61$cd = $artist->find_or_create_related( 'cds', { title => 'Big Flop' } );
62is( $cd->year, 2005, 'find_or_create_related on existing record ok' );
63
64# find_or_create_related creating a new record
65$cd = $artist->find_or_create_related( 'cds', {
66 title => 'Greatest Hits',
67 year => 2006,
68} );
69is( $cd->title, 'Greatest Hits', 'find_or_create_related new record ok' );
70@cds = $artist->search_related('cds');
71is( ($artist->search_related('cds'))[4]->title, 'Greatest Hits', 'find_or_create_related new record search ok' );
72
73SKIP: {
74 #skip 'Need to add delete_related', 1;
75 # delete_related
76 $artist->delete_related( cds => { title => 'Greatest Hits' });
77 cmp_ok( DBICTest::CD->search( title => 'Greatest Hits' ), '==', 0, 'delete_related ok' );
78};
79
80# try to add a bogus relationship using the wrong cols
81eval {
82 $artist->add_relationship(
83 tracks => 'DBICTest::Track',
84 { 'foreign.cd' => 'self.cdid' }
85 );
86};
87like($@, qr/Unknown column/, 'failed when creating a rel with invalid key, ok');
88
89# another bogus relationship using no join condition
90eval {
91 $artist->add_relationship( tracks => 'DBICTest::Track' );
92};
93like($@, qr/join condition/, 'failed when creating a rel without join condition, ok');
94
95}
96
971;