Merge 'trunk' into 'reduce_pings'
[dbsrgits/DBIx-Class.git] / t / 92storage_ping_count.t
1 use strict;
2 use warnings;  
3
4 # Stolen from 76joins.t (a good test for this purpose)
5
6 use Test::More;
7 use lib qw(t/lib);
8 use DBICTest;
9 use Data::Dumper;
10 use DBIC::SqlMakerTest;
11
12 my $ping_count = 0;
13
14 {
15   local $SIG{__WARN__} = sub {};
16   require DBIx::Class::Storage::DBI;
17
18   my $ping = \&DBIx::Class::Storage::DBI::_ping;
19
20   *DBIx::Class::Storage::DBI::_ping = sub {
21     $ping_count++;
22     goto &$ping;
23   };
24 }
25
26 my $schema = DBICTest->init_schema();
27
28 my $orig_debug = $schema->storage->debug;
29
30 use IO::File;
31
32 BEGIN {
33     eval "use DBD::SQLite";
34     plan $@
35         ? ( skip_all => 'needs DBD::SQLite for testing' )
36         : ( tests => 28 );
37 }
38
39 my $rs = $schema->resultset("CD")->search(
40            { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
41            { from => [ { 'me' => 'cd' },
42                          [
43                            { artist => 'artist' },
44                            { 'me.artist' => 'artist.artistid' }
45                          ] ] }
46          );
47
48 is( $rs + 0, 1, "Single record in resultset");
49
50 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
51
52 $rs = $schema->resultset("CD")->search(
53            { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
54            { join => 'artist' });
55
56 is( $rs + 0, 1, "Single record in resultset");
57
58 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
59
60 $rs = $schema->resultset("CD")->search(
61            { 'artist.name' => 'We Are Goth',
62              'liner_notes.notes' => 'Kill Yourself!' },
63            { join => [ qw/artist liner_notes/ ] });
64
65 is( $rs + 0, 1, "Single record in resultset");
66
67 is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
68
69 # when using join attribute, make sure slice()ing all objects has same count as all()
70 $rs = $schema->resultset("CD")->search(
71     { 'artist' => 1 },
72     { join => [qw/artist/], order_by => 'artist.name' }
73 );
74 is( scalar $rs->all, scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
75
76 ok(!$rs->slice($rs->count+1000, $rs->count+1002)->count,
77   'Slicing beyond end of rs returns a zero count');
78
79 $rs = $schema->resultset("Artist")->search(
80         { 'liner_notes.notes' => 'Kill Yourself!' },
81         { join => { 'cds' => 'liner_notes' } });
82
83 is( $rs->count, 1, "Single record in resultset");
84
85 is($rs->first->name, 'We Are Goth', 'Correct record returned');
86
87
88 {
89     $schema->populate('Artist', [
90         [ qw/artistid name/ ],
91         [ 4, 'Another Boy Band' ],
92     ]);
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 ],
98     ]);
99     $schema->populate('TwoKeys', [
100         [ qw/artist cd/ ],
101         [ 2, 4 ],
102         [ 2, 6 ],
103         [ 4, 7 ],
104         [ 4, 8 ],
105     ]);
106     
107     sub cd_count {
108         return $schema->resultset("CD")->count;
109     }
110     sub tk_count {
111         return $schema->resultset("TwoKeys")->count;
112     }
113
114     is(cd_count(), 8, '8 rows in table cd');
115     is(tk_count(), 7, '7 rows in table twokeys');
116  
117     sub artist1 {
118         return $schema->resultset("CD")->search(
119             { 'artist.name' => 'Caterwauler McCrae' },
120             { join => [qw/artist/]}
121         );
122     }
123     sub artist2 {
124         return $schema->resultset("CD")->search(
125             { 'artist.name' => 'Random Boy Band' },
126             { join => [qw/artist/]}
127         );
128     }
129
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' );
137
138     # test update on multi-column-pk
139     sub tk1 {
140         return $schema->resultset("TwoKeys")->search(
141             {
142                 'artist.name' => { like => '%Boy Band' },
143                 'cd.title'    => 'Greatest Hits',
144             },
145             { join => [qw/artist cd/] }
146         );
147     }
148     sub tk2 {
149         return $schema->resultset("TwoKeys")->search(
150             { 'artist.name' => 'Caterwauler McCrae' },
151             { join => [qw/artist/]}
152         );
153     }
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');
162 }
163
164 is $ping_count, 0, 'no _ping() calls';