half unpicked has_many prefetch mess
[dbsrgits/DBIx-Class.git] / t / run / 23cache.tl
CommitLineData
64acc2bc 1sub run_tests {
2my $schema = shift;
3
4eval "use DBD::SQLite";
5plan skip_all => 'needs DBD::SQLite for testing' if $@;
f9cc31dd 6plan tests => 12;
64acc2bc 7
a86b1efe 8warn "
9This test WILL fail. That's because the has_many prefetch code is
10only half re-written. However, it was utterly borken before, so
11this is arguably an improvement. If you fancy having a go at making
12_construct_object in resultset collapse multiple results into
13appropriate nested structures for inflate_result, be my guest.
14 -- mst
15
16";
17
64acc2bc 18my $rs = $schema->resultset("Artist")->search(
19 { artistid => 1 }
20);
21
22my $artist = $rs->first;
23
24is( scalar @{ $rs->get_cache }, 0, 'cache is not populated without cache attribute' );
25
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 34use Data::Dumper; $Data::Dumper::Deparse = 1;
64acc2bc 35
36# start test for prefetch SELECT count
37unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
38DBI->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
44is( 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
47is( 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
50is( scalar ($artist->cds), 3, 'artist->cds returns correct number of objects' );
51
52# ensure that $artist->cds->count returns correct value
53is( $artist->cds->count, 3, 'artist->cds->count returns correct value' );
54
55# ensure that $artist->count_related('cds') returns correct value
56is( $artist->count_related('cds'), 3, 'artist->count_related returns correct value' );
57
58# count the SELECTs
59DBI->trace(0, undef);
60my $selects = 0;
61my $trace = IO::File->new('t/var/dbic.trace', '<')
62 or die "Unable to read trace file";
63while (<$trace>) {
64 $selects++ if /SELECT/;
65}
66$trace->close;
67unlink 't/var/dbic.trace';
68is($selects, 2, 'only one SQL statement for each cached table');
69
70# make sure related_resultset is deleted after object is updated
71$artist->set_column('name', 'New Name');
72$artist->update();
73
74is( 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);
86
f9cc31dd 87# SELECT count for nested has_many prefetch
88unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
89DBI->trace(1, 't/var/dbic.trace');
90
91$artist = $rs->first;
92
93# count the SELECTs
94DBI->trace(0, undef);
6e3b590d 95$selects = 0;
96$trace = IO::File->new('t/var/dbic.trace', '<')
f9cc31dd 97 or die "Unable to read trace file";
98while (<$trace>) {
99 $selects++ if /SELECT/;
100}
101$trace->close;
102unlink 't/var/dbic.trace';
103is($selects, 3, 'one SQL statement for each cached table with nested prefetch');
104
105my @objs;
6e3b590d 106$artist = $rs->find(1);
f9cc31dd 107
108unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
109DBI->trace(1, 't/var/dbic.trace');
110
111my $cds = $artist->cds;
112my $tags = $cds->next->tags;
113while( my $tag = $tags->next ) {
114 push @objs, $tag->tagid; #warn "tag:", $tag->ID;
115}
116
117is_deeply( \@objs, [ 1 ], 'first cd has correct tags' );
118
119$tags = $cds->next->tags;
120@objs = ();
121while( my $tag = $tags->next ) {
122 push @objs, $tag->id; #warn "tag: ", $tag->ID;
123}
124
125is_deeply( \@objs, [ 2, 5, 8 ], 'second cd has correct tags' );
126
127# count the SELECTs
128DBI->trace(0, undef);
6e3b590d 129$selects = 0;
130$trace = IO::File->new('t/var/dbic.trace', '<')
f9cc31dd 131 or die "Unable to read trace file";
132while (<$trace>) {
133 $selects++ if /SELECT/;
134}
135$trace->close;
136unlink 't/var/dbic.trace';
137
138is( $selects, 0, 'no additional SQL statements while checking nested data' );
139
64acc2bc 140}
141
1421;