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