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