Institute a central "load this first in testing" package
[dbsrgits/DBIx-Class.git] / t / 76joins.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
70350518 3use strict;
8273e845 4use warnings;
70350518 5
6use Test::More;
a5a7bb73 7use DBICTest ':DiffSQL';
70350518 8
a47e1233 9my $schema = DBICTest->init_schema();
0567538f 10
f9db5527 11my $rs = $schema->resultset("CD")->search(
0567538f 12 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
638cd950 13 { from => [
14 { 'me' => 'cd' },
15 [
16 { artist => 'artist' },
17 { 'me.artist' => { -ident => 'artist.artistid' } },
18 ],
19 ] }
0567538f 20 );
21
cb9b5b23 22is( $rs + 0, 1, "Single record in resultset");
0567538f 23
24is($rs->first->title, 'Forkful of bees', 'Correct record returned');
25
f9db5527 26$rs = $schema->resultset("CD")->search(
0567538f 27 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
28 { join => 'artist' });
29
cb9b5b23 30is( $rs + 0, 1, "Single record in resultset");
0567538f 31
32is($rs->first->title, 'Forkful of bees', 'Correct record returned');
33
f9db5527 34$rs = $schema->resultset("CD")->search(
0567538f 35 { 'artist.name' => 'We Are Goth',
36 'liner_notes.notes' => 'Kill Yourself!' },
37 { join => [ qw/artist liner_notes/ ] });
38
cb9b5b23 39is( $rs + 0, 1, "Single record in resultset");
0567538f 40
41is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
42
8fe164b9 43# when using join attribute, make sure slice()ing all objects has same count as all()
f9db5527 44$rs = $schema->resultset("CD")->search(
8fe164b9 45 { 'artist' => 1 },
46 { join => [qw/artist/], order_by => 'artist.name' }
47);
cb9b5b23 48is( scalar $rs->all, scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
8fe164b9 49
2bd9c7c0 50ok(!$rs->slice($rs->count+1000, $rs->count+1002)->count,
51 'Slicing beyond end of rs returns a zero count');
52
f9db5527 53$rs = $schema->resultset("Artist")->search(
0567538f 54 { 'liner_notes.notes' => 'Kill Yourself!' },
55 { join => { 'cds' => 'liner_notes' } });
56
cb9b5b23 57is( $rs->count, 1, "Single record in resultset");
0567538f 58
59is($rs->first->name, 'We Are Goth', 'Correct record returned');
60
9f2d17e9 61
0f6fc705 62{
63 $schema->populate('Artist', [
64 [ qw/artistid name/ ],
65 [ 4, 'Another Boy Band' ],
66 ]);
67 $schema->populate('CD', [
68 [ qw/cdid artist title year/ ],
69 [ 6, 2, "Greatest Hits", 2001 ],
70 [ 7, 4, "Greatest Hits", 2005 ],
71 [ 8, 4, "BoyBandBlues", 2008 ],
72 ]);
73 $schema->populate('TwoKeys', [
74 [ qw/artist cd/ ],
75 [ 2, 4 ],
76 [ 2, 6 ],
77 [ 4, 7 ],
78 [ 4, 8 ],
79 ]);
8273e845 80
65d35121 81 my $cd_count = sub { $schema->resultset("CD")->count };
82 my $tk_count = sub { $schema->resultset("TwoKeys")->count };
83
84 is($cd_count->(), 8, '8 rows in table cd');
85 is($tk_count->(), 7, '7 rows in table twokeys');
86
87 my $artist1_rs = $schema->resultset("CD")->search(
88 { 'artist.name' => 'Caterwauler McCrae' },
89 { join => [qw/artist/]}
90 );
91
92 my $artist2_rs = $schema->resultset("CD")->search(
93 { 'artist.name' => 'Random Boy Band' },
94 { join => [qw/artist/]}
95 );
96
97 is( $artist1_rs->count, 3, '3 Caterwauler McCrae CDs' );
98 ok( $artist1_rs->delete, 'Successfully deleted 3 CDs' );
99 is( $artist1_rs->count, 0, '0 Caterwauler McCrae CDs' );
100 is( $artist2_rs->count, 2, '3 Random Boy Band CDs' );
101 ok( $artist2_rs->update( { 'artist' => 1 } ) );
102 is( $artist2_rs->count, 0, '0 Random Boy Band CDs' );
103 is( $artist1_rs->count, 2, '2 Caterwauler McCrae CDs' );
0f6fc705 104
105 # test update on multi-column-pk
65d35121 106 my $tk1_rs = $schema->resultset("TwoKeys")->search(
107 {
108 'artist.name' => { like => '%Boy Band' },
109 'cd.title' => 'Greatest Hits',
110 },
111 { join => [qw/artist cd/] }
112 );
113
114 my $tk2_rs = $schema->resultset("TwoKeys")->search(
115 { 'artist.name' => 'Caterwauler McCrae' },
116 { join => [qw/artist/]}
117 );
118
119 is( $tk2_rs->count, 2, 'TwoKeys count == 2' );
120 is( $tk1_rs->count, 2, 'TwoKeys count == 2' );
121 ok( $tk1_rs->update( { artist => 1 } ) );
122 is( $tk1_rs->count, 0, 'TwoKeys count == 0' );
123 is( $tk2_rs->count, 4, '2 Caterwauler McCrae CDs' );
124 ok( $tk2_rs->delete, 'Successfully deleted 4 CDs' );
125 is($cd_count->(), 5, '5 rows in table cd');
126 is($tk_count->(), 3, '3 rows in table twokeys');
0f6fc705 127}
56166f36 128
129done_testing;