Fix default insert on Oracle ( $rs->create({}) with empty hashref )
[dbsrgits/DBIx-Class.git] / t / search / preserve_original_rs.t
CommitLineData
e9bd1473 1use strict;
4086a325 2use warnings;
e9bd1473 3
4use Test::More;
5use Test::Exception;
4086a325 6
e9bd1473 7use lib qw(t/lib);
a5a7bb73 8use DBICTest ':DiffSQL';
bfa46eb5 9
2cfc22dd 10use Storable 'dclone';
e9bd1473 11
12my $schema = DBICTest->init_schema();
13
e9bd1473 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;
09d763c8 21 $attrs = dclone( $cd_rs->{attrs} );
e9bd1473 22
23 $cd_rs->search ({})->all;
09d763c8 24 is_deeply (dclone($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search');
e9bd1473 25
26 lives_ok (sub {
27 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
09d763c8 28 is_deeply (dclone($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch');
e9bd1473 29 }, 'first prefetching search ok');
30
31 lives_ok (sub {
32 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
09d763c8 33 is_deeply (dclone($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch')
e9bd1473 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} = {};
09d763c8 40 $attrs = dclone( $cd_rs->{attrs} );
e9bd1473 41
42 $cd_rs->search ({})->all;
09d763c8 43 is_deeply (dclone($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search');
e9bd1473 44
45 lives_ok (sub {
46 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
09d763c8 47 is_deeply (dclone($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch');
e9bd1473 48 }, 'first prefetching search ok');
49
50 lives_ok (sub {
51 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
09d763c8 52 is_deeply (dclone($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch')
e9bd1473 53 }, 'second prefetching search ok');
54}
4086a325 55
56# Also test search_related, but now that we have as_query simply compare before and after
57my $artist = $schema->resultset ('Artist')->first;
58my %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
77for my $s (qw/a2a artw cd artw_back/) {
78 my $rs = $q{$s}{rs};
79
7025db7c 80 lives_ok ( sub { $rs->first }, "first() on $s does not throw an exception" );
4086a325 81
7025db7c 82 lives_ok ( sub { $rs->count }, "count() on $s does not throw an exception" );
4086a325 83
84 is_same_sql_bind ($rs->as_query, $q{$s}{query}, "$s resultset unmodified (as_query matches)" );
85}
09d763c8 86
1887cd9f 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
735f190c 97 ok (! exists $fresh->{cursor}, 'Still no cursor on fresh rs');
6ae62c5c 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' );
1887cd9f 105}
106
09d763c8 107done_testing;