Add an explicit deduplication of identical condition in cond normalizer
[dbsrgits/DBIx-Class.git] / t / search / stack_cond.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 use DBIx::Class::_Util 'dump_value';
9 use DBICTest ':DiffSQL';
10 use SQL::Abstract qw(is_plain_value is_literal_value);
11 use List::Util 'shuffle';
12 $Data::Dumper::Indent = 0;
13
14 my $schema = DBICTest->init_schema();
15
16 for 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 = (
38     # these are monkey-wrenches, always there
39     { title => { '!=', [ -and => \'bar' ] }, year => { '!=', [ -and => 'bar' ] } },
40     { -or => [ genreid => undef, genreid => { '!=' => \42 } ] },
41     { -or => [ genreid => undef, genreid => { '!=' => \42 } ] },
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,
53       { year => $$v },
54       { title => $$v },
55       { -and => [ year => $$v, title => $$v ] },
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
74       WHERE
75         ( genreid IS NULL OR genreid != 42 )
76           AND
77         title != bar
78           AND
79         title $c->{sql}
80           AND
81         year != ?
82           AND
83         year $c->{sql}
84     )",
85     \@bind,
86     'Double condition correctly collapsed for steps:' . join( '', map { "\n\t" . dump_value($_) } @query_steps ),
87   );
88 }
89
90 done_testing;