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