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