Add an explicit deduplication of identical condition in cond normalizer
[dbsrgits/DBIx-Class.git] / t / search / stack_cond.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
5268b1da 3use strict;
4use warnings;
5
6use Test::More;
c0329273 7
8fc4291e 8use DBIx::Class::_Util 'dump_value';
5268b1da 9use DBICTest ':DiffSQL';
10use SQL::Abstract qw(is_plain_value is_literal_value);
11use List::Util 'shuffle';
5268b1da 12$Data::Dumper::Indent = 0;
13
14my $schema = DBICTest->init_schema();
15
16for my $c (
17 { cond => undef, sql => 'IS NULL' },
18 { cond => { -value => undef }, sql => 'IS NULL' },
19 { cond => \'foo', sql => '= foo' },
20 { cond => 'foo', sql => '= ?', bind => [
21 [ { dbic_colname => "title", sqlt_datatype => "varchar", sqlt_size => 100 } => 'foo' ],
22 [ { dbic_colname => "year", sqlt_datatype => "varchar", sqlt_size => 100 } => 'foo' ],
23 ]},
24 { cond => { -value => 'foo' }, sql => '= ?', bind => [
25 [ { dbic_colname => "title", sqlt_datatype => "varchar", sqlt_size => 100 } => 'foo' ],
26 [ { dbic_colname => "year", sqlt_datatype => "varchar", sqlt_size => 100 } => 'foo' ],
27 ]},
28 { cond => \[ '?', "foo" ], sql => '= ?', bind => [
29 [ {} => 'foo' ],
30 [ {} => 'foo' ],
31 ]},
32) {
33 my $rs = $schema->resultset('CD')->search({}, { columns => 'title' });
34
35 my $bare_cond = is_literal_value($c->{cond}) ? { '=', $c->{cond} } : $c->{cond};
36
37 my @query_steps = (
135ac69d 38 # these are monkey-wrenches, always there
5268b1da 39 { title => { '!=', [ -and => \'bar' ] }, year => { '!=', [ -and => 'bar' ] } },
135ac69d 40 { -or => [ genreid => undef, genreid => { '!=' => \42 } ] },
41 { -or => [ genreid => undef, genreid => { '!=' => \42 } ] },
5268b1da 42
43 { title => $bare_cond, year => { '=', $c->{cond} } },
44 { -and => [ year => $bare_cond, { title => { '=', $c->{cond} } } ] },
45 [ year => $bare_cond ],
46 [ title => $bare_cond ],
47 { -and => [ { year => { '=', $c->{cond} } }, { title => { '=', $c->{cond} } } ] },
48 { -and => { -or => { year => { '=', $c->{cond} } } }, -or => { title => $bare_cond } },
49 );
50
51 if (my $v = is_plain_value($c->{cond})) {
52 push @query_steps,
5379386e 53 { year => $$v },
54 { title => $$v },
55 { -and => [ year => $$v, title => $$v ] },
5268b1da 56 ;
57 }
58
59 @query_steps = shuffle @query_steps;
60
61 $rs = $rs->search($_) for @query_steps;
62
63 my @bind = @{$c->{bind} || []};
64 {
65 no warnings 'misc';
66 splice @bind, 1, 0, [ { dbic_colname => "year", sqlt_datatype => "varchar", sqlt_size => 100 } => 'bar' ];
67 }
68
69 is_same_sql_bind (
70 $rs->as_query,
71 "(
72 SELECT me.title
73 FROM cd me
135ac69d 74 WHERE
d6c13bfd 75 ( genreid IS NULL OR genreid != 42 )
135ac69d 76 AND
77 title != bar
78 AND
79 title $c->{sql}
80 AND
81 year != ?
82 AND
83 year $c->{sql}
5268b1da 84 )",
85 \@bind,
d6c13bfd 86 'Double condition correctly collapsed for steps:' . join( '', map { "\n\t" . dump_value($_) } @query_steps ),
5268b1da 87 );
88}
89
90done_testing;