1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
7 use DBICTest ':DiffSQL';
9 my $schema = DBICTest->init_schema();
11 my $rs = $schema->resultset("CD")->search(
12 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
16 { artist => 'artist' },
17 { 'me.artist' => { -ident => 'artist.artistid' } },
22 is( $rs + 0, 1, "Single record in resultset");
24 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
26 $rs = $schema->resultset("CD")->search(
27 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
28 { join => 'artist' });
30 is( $rs + 0, 1, "Single record in resultset");
32 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
34 $rs = $schema->resultset("CD")->search(
35 { 'artist.name' => 'We Are Goth',
36 'liner_notes.notes' => 'Kill Yourself!' },
37 { join => [ qw/artist liner_notes/ ] });
39 is( $rs + 0, 1, "Single record in resultset");
41 is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
43 # when using join attribute, make sure slice()ing all objects has same count as all()
44 $rs = $schema->resultset("CD")->search(
46 { join => [qw/artist/], order_by => 'artist.name' }
48 is( scalar $rs->all, scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
50 ok(!$rs->slice($rs->count+1000, $rs->count+1002)->count,
51 'Slicing beyond end of rs returns a zero count');
53 $rs = $schema->resultset("Artist")->search(
54 { 'liner_notes.notes' => 'Kill Yourself!' },
55 { join => { 'cds' => 'liner_notes' } });
57 is( $rs->count, 1, "Single record in resultset");
59 is($rs->first->name, 'We Are Goth', 'Correct record returned');
63 $schema->populate('Artist', [
64 [ qw/artistid name/ ],
65 [ 4, 'Another Boy Band' ],
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 ],
73 $schema->populate('TwoKeys', [
81 my $cd_count = sub { $schema->resultset("CD")->count };
82 my $tk_count = sub { $schema->resultset("TwoKeys")->count };
84 is($cd_count->(), 8, '8 rows in table cd');
85 is($tk_count->(), 7, '7 rows in table twokeys');
87 my $artist1_rs = $schema->resultset("CD")->search(
88 { 'artist.name' => 'Caterwauler McCrae' },
89 { join => [qw/artist/]}
92 my $artist2_rs = $schema->resultset("CD")->search(
93 { 'artist.name' => 'Random Boy Band' },
94 { join => [qw/artist/]}
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' );
105 # test update on multi-column-pk
106 my $tk1_rs = $schema->resultset("TwoKeys")->search(
108 'artist.name' => { like => '%Boy Band' },
109 'cd.title' => 'Greatest Hits',
111 { join => [qw/artist cd/] }
114 my $tk2_rs = $schema->resultset("TwoKeys")->search(
115 { 'artist.name' => 'Caterwauler McCrae' },
116 { join => [qw/artist/]}
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');