6 use DBICTest ':DiffSQL';
8 my $schema = DBICTest->init_schema();
10 my $rs = $schema->resultset("CD")->search(
11 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
15 { artist => 'artist' },
16 { 'me.artist' => { -ident => 'artist.artistid' } },
21 is( $rs + 0, 1, "Single record in resultset");
23 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
25 $rs = $schema->resultset("CD")->search(
26 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
27 { join => 'artist' });
29 is( $rs + 0, 1, "Single record in resultset");
31 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
33 $rs = $schema->resultset("CD")->search(
34 { 'artist.name' => 'We Are Goth',
35 'liner_notes.notes' => 'Kill Yourself!' },
36 { join => [ qw/artist liner_notes/ ] });
38 is( $rs + 0, 1, "Single record in resultset");
40 is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
42 # when using join attribute, make sure slice()ing all objects has same count as all()
43 $rs = $schema->resultset("CD")->search(
45 { join => [qw/artist/], order_by => 'artist.name' }
47 is( scalar $rs->all, scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
49 ok(!$rs->slice($rs->count+1000, $rs->count+1002)->count,
50 'Slicing beyond end of rs returns a zero count');
52 $rs = $schema->resultset("Artist")->search(
53 { 'liner_notes.notes' => 'Kill Yourself!' },
54 { join => { 'cds' => 'liner_notes' } });
56 is( $rs->count, 1, "Single record in resultset");
58 is($rs->first->name, 'We Are Goth', 'Correct record returned');
62 $schema->populate('Artist', [
63 [ qw/artistid name/ ],
64 [ 4, 'Another Boy Band' ],
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 ],
72 $schema->populate('TwoKeys', [
80 my $cd_count = sub { $schema->resultset("CD")->count };
81 my $tk_count = sub { $schema->resultset("TwoKeys")->count };
83 is($cd_count->(), 8, '8 rows in table cd');
84 is($tk_count->(), 7, '7 rows in table twokeys');
86 my $artist1_rs = $schema->resultset("CD")->search(
87 { 'artist.name' => 'Caterwauler McCrae' },
88 { join => [qw/artist/]}
91 my $artist2_rs = $schema->resultset("CD")->search(
92 { 'artist.name' => 'Random Boy Band' },
93 { join => [qw/artist/]}
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' );
104 # test update on multi-column-pk
105 my $tk1_rs = $schema->resultset("TwoKeys")->search(
107 'artist.name' => { like => '%Boy Band' },
108 'cd.title' => 'Greatest Hits',
110 { join => [qw/artist cd/] }
113 my $tk2_rs = $schema->resultset("TwoKeys")->search(
114 { 'artist.name' => 'Caterwauler McCrae' },
115 { join => [qw/artist/]}
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');