Retire DBIC/SqlMakerTest.pm now that SQLA::Test provides the same function
[dbsrgits/DBIx-Class.git] / t / search / preserve_original_rs.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6
7 use lib qw(t/lib);
8 use DBICTest ':DiffSQL';
9
10 use Storable 'dclone';
11
12 my $schema = DBICTest->init_schema();
13
14 # A search() with prefetch seems to pollute an already joined resultset
15 # in a way that offsets future joins (adapted from a test case by Debolaz)
16 {
17   my ($cd_rs, $attrs);
18
19   # test a real-life case - rs is obtained by an implicit m2m join
20   $cd_rs = $schema->resultset ('Producer')->first->cds;
21   $attrs = dclone( $cd_rs->{attrs} );
22
23   $cd_rs->search ({})->all;
24   is_deeply (dclone($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search');
25
26   lives_ok (sub {
27     $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
28     is_deeply (dclone($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch');
29   }, 'first prefetching search ok');
30
31   lives_ok (sub {
32     $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
33     is_deeply (dclone($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch')
34   }, 'second prefetching search ok');
35
36
37   # test a regular rs with an empty seen_join injected - it should still work!
38   $cd_rs = $schema->resultset ('CD');
39   $cd_rs->{attrs}{seen_join}  = {};
40   $attrs = dclone( $cd_rs->{attrs} );
41
42   $cd_rs->search ({})->all;
43   is_deeply (dclone($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search');
44
45   lives_ok (sub {
46     $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
47     is_deeply (dclone($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch');
48   }, 'first prefetching search ok');
49
50   lives_ok (sub {
51     $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
52     is_deeply (dclone($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch')
53   }, 'second prefetching search ok');
54 }
55
56 # Also test search_related, but now that we have as_query simply compare before and after
57 my $artist = $schema->resultset ('Artist')->first;
58 my %q;
59
60 $q{a2a}{rs} = $artist->search_related ('artwork_to_artist');
61 $q{a2a}{query} = $q{a2a}{rs}->as_query;
62
63 $q{artw}{rs} = $q{a2a}{rs}->search_related ('artwork',
64   { },
65   { join => ['cd', 'artwork_to_artist'] },
66 );
67 $q{artw}{query} = $q{artw}{rs}->as_query;
68
69 $q{cd}{rs} = $q{artw}{rs}->search_related ('cd', {}, { join => [ 'artist', 'tracks' ] } );
70 $q{cd}{query} = $q{cd}{rs}->as_query;
71
72 $q{artw_back}{rs} = $q{cd}{rs}->search_related ('artwork',
73   {}, { join => { artwork_to_artist => 'artist' } }
74 )->search_related ('artwork_to_artist', {}, { join => 'artist' });
75 $q{artw_back}{query} = $q{artw_back}{rs}->as_query;
76
77 for my $s (qw/a2a artw cd artw_back/) {
78   my $rs = $q{$s}{rs};
79
80   lives_ok ( sub { $rs->first }, "first() on $s does not throw an exception" );
81
82   lives_ok ( sub { $rs->count }, "count() on $s does not throw an exception" );
83
84   is_same_sql_bind ($rs->as_query, $q{$s}{query}, "$s resultset unmodified (as_query matches)" );
85 }
86
87 # ensure nothing pollutes the attrs of an existing rs
88 {
89   my $fresh = $schema->resultset('CD');
90
91   isa_ok ($fresh->find(1), 'DBICTest::CD' );
92   isa_ok ($fresh->single({ cdid => 1}), 'DBICTest::CD' );
93   isa_ok ($fresh->search({ cdid => 1})->next, 'DBICTest::CD' );
94   is ($fresh->count({ cdid => 1}), 1 );
95   is ($fresh->count_rs({ cdid => 1})->next, 1 );
96
97   ok (! exists $fresh->{cursor}, 'Still no cursor on fresh rs');
98   ok (! exists $fresh->{_attrs}{_last_sqlmaker_alias_map}, 'aliasmap did not leak through' );
99
100   my $n = $fresh->next;
101
102   # check that we are not testing for deprecated slotnames
103   ok ($fresh->{cursor}, 'Cursor at expected slot after fire');
104   ok (exists $fresh->{_attrs}{_last_sqlmaker_alias_map}, 'aliasmap at expected slot after fire' );
105 }
106
107 done_testing;