4 # Stolen from 76joins.t (a good test for this purpose)
10 use DBIC::SqlMakerTest;
15 local $SIG{__WARN__} = sub {};
16 require DBIx::Class::Storage::DBI;
18 my $ping = \&DBIx::Class::Storage::DBI::_ping;
20 *DBIx::Class::Storage::DBI::_ping = sub {
26 my $schema = DBICTest->init_schema();
28 my $orig_debug = $schema->storage->debug;
33 eval "use DBD::SQLite";
35 ? ( skip_all => 'needs DBD::SQLite for testing' )
39 my $rs = $schema->resultset("CD")->search(
40 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
41 { from => [ { 'me' => 'cd' },
43 { artist => 'artist' },
44 { 'me.artist' => 'artist.artistid' }
48 is( $rs + 0, 1, "Single record in resultset");
50 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
52 $rs = $schema->resultset("CD")->search(
53 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
54 { join => 'artist' });
56 is( $rs + 0, 1, "Single record in resultset");
58 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
60 $rs = $schema->resultset("CD")->search(
61 { 'artist.name' => 'We Are Goth',
62 'liner_notes.notes' => 'Kill Yourself!' },
63 { join => [ qw/artist liner_notes/ ] });
65 is( $rs + 0, 1, "Single record in resultset");
67 is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
69 # when using join attribute, make sure slice()ing all objects has same count as all()
70 $rs = $schema->resultset("CD")->search(
72 { join => [qw/artist/], order_by => 'artist.name' }
74 is( scalar $rs->all, scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
76 ok(!$rs->slice($rs->count+1000, $rs->count+1002)->count,
77 'Slicing beyond end of rs returns a zero count');
79 $rs = $schema->resultset("Artist")->search(
80 { 'liner_notes.notes' => 'Kill Yourself!' },
81 { join => { 'cds' => 'liner_notes' } });
83 is( $rs->count, 1, "Single record in resultset");
85 is($rs->first->name, 'We Are Goth', 'Correct record returned');
89 $schema->populate('Artist', [
90 [ qw/artistid name/ ],
91 [ 4, 'Another Boy Band' ],
93 $schema->populate('CD', [
94 [ qw/cdid artist title year/ ],
95 [ 6, 2, "Greatest Hits", 2001 ],
96 [ 7, 4, "Greatest Hits", 2005 ],
97 [ 8, 4, "BoyBandBlues", 2008 ],
99 $schema->populate('TwoKeys', [
108 return $schema->resultset("CD")->count;
111 return $schema->resultset("TwoKeys")->count;
114 is(cd_count(), 8, '8 rows in table cd');
115 is(tk_count(), 7, '7 rows in table twokeys');
118 return $schema->resultset("CD")->search(
119 { 'artist.name' => 'Caterwauler McCrae' },
120 { join => [qw/artist/]}
124 return $schema->resultset("CD")->search(
125 { 'artist.name' => 'Random Boy Band' },
126 { join => [qw/artist/]}
130 is( artist1()->count, 3, '3 Caterwauler McCrae CDs' );
131 ok( artist1()->delete, 'Successfully deleted 3 CDs' );
132 is( artist1()->count, 0, '0 Caterwauler McCrae CDs' );
133 is( artist2()->count, 2, '3 Random Boy Band CDs' );
134 ok( artist2()->update( { 'artist' => 1 } ) );
135 is( artist2()->count, 0, '0 Random Boy Band CDs' );
136 is( artist1()->count, 2, '2 Caterwauler McCrae CDs' );
138 # test update on multi-column-pk
140 return $schema->resultset("TwoKeys")->search(
142 'artist.name' => { like => '%Boy Band' },
143 'cd.title' => 'Greatest Hits',
145 { join => [qw/artist cd/] }
149 return $schema->resultset("TwoKeys")->search(
150 { 'artist.name' => 'Caterwauler McCrae' },
151 { join => [qw/artist/]}
154 is( tk2()->count, 2, 'TwoKeys count == 2' );
155 is( tk1()->count, 2, 'TwoKeys count == 2' );
156 ok( tk1()->update( { artist => 1 } ) );
157 is( tk1()->count, 0, 'TwoKeys count == 0' );
158 is( tk2()->count, 4, '2 Caterwauler McCrae CDs' );
159 ok( tk2()->delete, 'Successfully deleted 4 CDs' );
160 is(cd_count(), 5, '5 rows in table cd');
161 is(tk_count(), 3, '3 rows in table twokeys');
164 is $ping_count, 0, 'no _ping() calls';