Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / t / 76joins.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use DBICTest ':DiffSQL';
8
9 my $schema = DBICTest->init_schema();
10
11 my $rs = $schema->resultset("CD")->search(
12            { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
13            { from => [
14               { 'me' => 'cd' },
15               [
16                 { artist => 'artist' },
17                 { 'me.artist' => { -ident => 'artist.artistid' } },
18               ],
19            ] }
20          );
21
22 is( $rs + 0, 1, "Single record in resultset");
23
24 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
25
26 $rs = $schema->resultset("CD")->search(
27            { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
28            { join => 'artist' });
29
30 is( $rs + 0, 1, "Single record in resultset");
31
32 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
33
34 $rs = $schema->resultset("CD")->search(
35            { 'artist.name' => 'We Are Goth',
36              'liner_notes.notes' => 'Kill Yourself!' },
37            { join => [ qw/artist liner_notes/ ] });
38
39 is( $rs + 0, 1, "Single record in resultset");
40
41 is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
42
43 # when using join attribute, make sure slice()ing all objects has same count as all()
44 $rs = $schema->resultset("CD")->search(
45     { 'artist' => 1 },
46     { join => [qw/artist/], order_by => 'artist.name' }
47 );
48 is( scalar $rs->all, scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
49
50 ok(!$rs->slice($rs->count+1000, $rs->count+1002)->count,
51   'Slicing beyond end of rs returns a zero count');
52
53 $rs = $schema->resultset("Artist")->search(
54         { 'liner_notes.notes' => 'Kill Yourself!' },
55         { join => { 'cds' => 'liner_notes' } });
56
57 is( $rs->count, 1, "Single record in resultset");
58
59 is($rs->first->name, 'We Are Goth', 'Correct record returned');
60
61
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     ]);
80
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' );
104
105     # test update on multi-column-pk
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');
127 }
128
129 done_testing;