Commit | Line | Data |
64acc2bc |
1 | sub run_tests { |
2 | my $schema = shift; |
3 | |
4 | eval "use DBD::SQLite"; |
5 | plan skip_all => 'needs DBD::SQLite for testing' if $@; |
62e87ea8 |
6 | plan tests => 15; |
64acc2bc |
7 | |
8 | my $rs = $schema->resultset("Artist")->search( |
9 | { artistid => 1 } |
10 | ); |
11 | |
12 | my $artist = $rs->first; |
13 | |
14 | is( scalar @{ $rs->get_cache }, 0, 'cache is not populated without cache attribute' ); |
15 | |
f109ee4a |
16 | my @a = $schema->resultset("Artist")->search( |
17 | { }, |
18 | { |
19 | join => [ qw/ cds /], |
20 | prefetch => [qw/ cds /], |
21 | } |
22 | ); |
23 | |
24 | is(scalar @a, 3, 'artist with cds: count parent objects'); |
25 | |
64acc2bc |
26 | $rs = $schema->resultset("Artist")->search( |
27 | { 'artistid' => 1 }, |
28 | { |
3c3c416e |
29 | join => [ qw/ cds /], |
64acc2bc |
30 | prefetch => [qw/ cds /], |
64acc2bc |
31 | } |
32 | ); |
33 | |
f9cc31dd |
34 | use Data::Dumper; $Data::Dumper::Deparse = 1; |
64acc2bc |
35 | |
36 | # start test for prefetch SELECT count |
37 | unlink 't/var/dbic.trace' if -e 't/var/dbic.trace'; |
38 | DBI->trace(1, 't/var/dbic.trace'); |
39 | |
40 | $artist = $rs->first; |
41 | $rs->reset(); |
42 | |
43 | # make sure artist contains a related resultset for cds |
44 | is( ref $artist->{related_resultsets}->{cds}, 'DBIx::Class::ResultSet', 'artist has a related_resultset for cds' ); |
45 | |
46 | # check if $artist->cds->get_cache is populated |
47 | is( scalar @{$artist->cds->get_cache}, 3, 'cache for artist->cds contains correct number of records'); |
48 | |
49 | # ensure that $artist->cds returns correct number of objects |
50 | is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' ); |
51 | |
52 | # ensure that $artist->cds->count returns correct value |
53 | is( $artist->cds->count, 3, 'artist->cds->count returns correct value' ); |
54 | |
55 | # ensure that $artist->count_related('cds') returns correct value |
56 | is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' ); |
57 | |
58 | # count the SELECTs |
59 | DBI->trace(0, undef); |
60 | my $selects = 0; |
61 | my $trace = IO::File->new('t/var/dbic.trace', '<') |
62 | or die "Unable to read trace file"; |
63 | while (<$trace>) { |
64 | $selects++ if /SELECT/; |
65 | } |
66 | $trace->close; |
67 | unlink 't/var/dbic.trace'; |
5a5bec6c |
68 | is($selects, 1, 'only one SQL statement executed'); |
64acc2bc |
69 | |
70 | # make sure related_resultset is deleted after object is updated |
71 | $artist->set_column('name', 'New Name'); |
72 | $artist->update(); |
73 | |
74 | is( scalar keys %{$artist->{related_resultsets}}, 0, 'related resultsets deleted after update' ); |
75 | |
76 | # todo: make sure caching works with nested prefetch e.g. $artist->cds->tracks |
77 | $rs = $schema->resultset("Artist")->search( |
78 | { artistid => 1 }, |
79 | { |
3c3c416e |
80 | join => { cds => 'tags' }, |
64acc2bc |
81 | prefetch => { |
82 | cds => 'tags' |
83 | }, |
64acc2bc |
84 | } |
85 | ); |
62e87ea8 |
86 | { |
87 | $schema->resultset("Artist")->create({artistid=>4,name=>qq{Humoungous Hamsters}}); |
88 | my $artist = $schema->resultset("Artist")->search( |
89 | { artistid => 4 },{prefetch=>[qw/cds/]} |
90 | )->first; |
91 | |
92 | is($artist->cds, 0, 'No cds for this artist'); |
93 | } |
64acc2bc |
94 | |
f9cc31dd |
95 | # SELECT count for nested has_many prefetch |
96 | unlink 't/var/dbic.trace' if -e 't/var/dbic.trace'; |
97 | DBI->trace(1, 't/var/dbic.trace'); |
98 | |
5a5bec6c |
99 | $artist = ($rs->all)[0]; |
f9cc31dd |
100 | |
101 | # count the SELECTs |
102 | DBI->trace(0, undef); |
6e3b590d |
103 | $selects = 0; |
104 | $trace = IO::File->new('t/var/dbic.trace', '<') |
f9cc31dd |
105 | or die "Unable to read trace file"; |
106 | while (<$trace>) { |
107 | $selects++ if /SELECT/; |
108 | } |
109 | $trace->close; |
110 | unlink 't/var/dbic.trace'; |
5a5bec6c |
111 | is($selects, 1, 'only one SQL statement executed'); |
f9cc31dd |
112 | |
113 | my @objs; |
5a5bec6c |
114 | #$artist = $rs->find(1); |
f9cc31dd |
115 | |
116 | unlink 't/var/dbic.trace' if -e 't/var/dbic.trace'; |
117 | DBI->trace(1, 't/var/dbic.trace'); |
118 | |
119 | my $cds = $artist->cds; |
120 | my $tags = $cds->next->tags; |
121 | while( my $tag = $tags->next ) { |
5a5bec6c |
122 | push @objs, $tag->tagid; #warn "tag:", $tag->ID, " => ", $tag->tag; |
f9cc31dd |
123 | } |
124 | |
5a5bec6c |
125 | is_deeply( \@objs, [ 3 ], 'first cd has correct tags' ); |
f9cc31dd |
126 | |
127 | $tags = $cds->next->tags; |
128 | @objs = (); |
129 | while( my $tag = $tags->next ) { |
130 | push @objs, $tag->id; #warn "tag: ", $tag->ID; |
131 | } |
132 | |
133 | is_deeply( \@objs, [ 2, 5, 8 ], 'second cd has correct tags' ); |
134 | |
135 | # count the SELECTs |
136 | DBI->trace(0, undef); |
6e3b590d |
137 | $selects = 0; |
138 | $trace = IO::File->new('t/var/dbic.trace', '<') |
f9cc31dd |
139 | or die "Unable to read trace file"; |
140 | while (<$trace>) { |
141 | $selects++ if /SELECT/; |
142 | } |
143 | $trace->close; |
144 | unlink 't/var/dbic.trace'; |
145 | |
146 | is( $selects, 0, 'no additional SQL statements while checking nested data' ); |
147 | |
d2b3ea14 |
148 | # start test for prefetch SELECT count |
149 | unlink 't/var/dbic.trace' if -e 't/var/dbic.trace'; |
150 | DBI->trace(1, 't/var/dbic.trace'); |
151 | |
152 | $artist = $schema->resultset('Artist')->find(1, { prefetch => [qw/cds/] }); |
153 | |
154 | # count the SELECTs |
155 | DBI->trace(0, undef); |
156 | $selects = 0; |
157 | $trace = IO::File->new('t/var/dbic.trace', '<') |
158 | or die "Unable to read trace file"; |
159 | while (<$trace>) { |
160 | $selects++ if /SELECT/; |
161 | } |
162 | $trace->close; |
163 | unlink 't/var/dbic.trace'; |
164 | |
165 | is( $selects, 1, 'only one select statement on find with inline has_many prefetch' ); |
166 | |
167 | # start test for prefetch SELECT count |
168 | unlink 't/var/dbic.trace' if -e 't/var/dbic.trace'; |
169 | DBI->trace(1, 't/var/dbic.trace'); |
170 | |
171 | $rs = $schema->resultset('Artist')->search(undef, { prefetch => [qw/cds/] }); |
172 | $artist = $rs->find(1); |
173 | |
174 | # count the SELECTs |
175 | DBI->trace(0, undef); |
176 | $selects = 0; |
177 | $trace = IO::File->new('t/var/dbic.trace', '<') |
178 | or die "Unable to read trace file"; |
179 | while (<$trace>) { |
180 | $selects++ if /SELECT/; |
181 | } |
182 | $trace->close; |
183 | unlink 't/var/dbic.trace'; |
184 | |
185 | is( $selects, 1, 'only one select statement on find with has_many prefetch on resultset' ); |
186 | |
64acc2bc |
187 | } |
188 | |
189 | 1; |