Finally commit trully failing test
[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);
4086a325 8use DBIC::SqlMakerTest;
9use DBIC::DebugObj;
e9bd1473 10use DBICTest;
11use Data::Dumper;
12
13my $schema = DBICTest->init_schema();
14
4086a325 15plan tests => 22;
e9bd1473 16
e9bd1473 17# A search() with prefetch seems to pollute an already joined resultset
18# in a way that offsets future joins (adapted from a test case by Debolaz)
19{
20 my ($cd_rs, $attrs);
21
22 # test a real-life case - rs is obtained by an implicit m2m join
23 $cd_rs = $schema->resultset ('Producer')->first->cds;
24 $attrs = Dumper $cd_rs->{attrs};
25
26 $cd_rs->search ({})->all;
27 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search');
28
29 lives_ok (sub {
30 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
31 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch');
32 }, 'first prefetching search ok');
33
34 lives_ok (sub {
35 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
36 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch')
37 }, 'second prefetching search ok');
38
39
40 # test a regular rs with an empty seen_join injected - it should still work!
41 $cd_rs = $schema->resultset ('CD');
42 $cd_rs->{attrs}{seen_join} = {};
43 $attrs = Dumper $cd_rs->{attrs};
44
45 $cd_rs->search ({})->all;
46 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after a simple search');
47
48 lives_ok (sub {
49 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
50 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after search with prefetch');
51 }, 'first prefetching search ok');
52
53 lives_ok (sub {
54 $cd_rs->search ({'artist.artistid' => 1}, { prefetch => 'artist' })->all;
55 is (Dumper ($cd_rs->{attrs}), $attrs, 'Resultset attributes preserved after another search with prefetch')
56 }, 'second prefetching search ok');
57}
4086a325 58
59# Also test search_related, but now that we have as_query simply compare before and after
60my $artist = $schema->resultset ('Artist')->first;
61my %q;
62
63$q{a2a}{rs} = $artist->search_related ('artwork_to_artist');
64$q{a2a}{query} = $q{a2a}{rs}->as_query;
65
66$q{artw}{rs} = $q{a2a}{rs}->search_related ('artwork',
67 { },
68 { join => ['cd', 'artwork_to_artist'] },
69);
70$q{artw}{query} = $q{artw}{rs}->as_query;
71
72$q{cd}{rs} = $q{artw}{rs}->search_related ('cd', {}, { join => [ 'artist', 'tracks' ] } );
73$q{cd}{query} = $q{cd}{rs}->as_query;
74
75$q{artw_back}{rs} = $q{cd}{rs}->search_related ('artwork',
76 {}, { join => { artwork_to_artist => 'artist' } }
77)->search_related ('artwork_to_artist', {}, { join => 'artist' });
78$q{artw_back}{query} = $q{artw_back}{rs}->as_query;
79
80for my $s (qw/a2a artw cd artw_back/) {
81 my $rs = $q{$s}{rs};
82
83 lives_ok ( sub { $rs->first }, "first on $s does not throw an exception" );
84
85 lives_ok ( sub { $rs->count }, "count on $s does not throw an exception" );
86
87 is_same_sql_bind ($rs->as_query, $q{$s}{query}, "$s resultset unmodified (as_query matches)" );
88}
89