Retire DBIC/SqlMakerTest.pm now that SQLA::Test provides the same function
[dbsrgits/DBIx-Class.git] / t / sqlmaker / dbihacks_internals.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use Test::Warn;
5
6 use lib qw(t/lib);
7 use DBICTest ':DiffSQL';
8
9 use Data::Dumper;
10
11 my $schema = DBICTest->init_schema( no_deploy => 1);
12 my $sm = $schema->storage->sql_maker;
13
14 for my $t (
15   {
16     where => { artistid => 1, charfield => undef },
17     cc_result => { artistid => 1, charfield => undef },
18     sql => 'WHERE artistid = ? AND charfield IS NULL',
19     efcc_result => [qw( artistid )],
20   },
21   {
22     where => { -and => [ artistid => 1, charfield => undef, { rank => 13 } ] },
23     cc_result => { artistid => 1, charfield => undef, rank => 13 },
24     sql => 'WHERE artistid = ?  AND charfield IS NULL AND rank = ?',
25     efcc_result => [qw( artistid rank )],
26   },
27   {
28     where => { -and => [ { artistid => 1, charfield => undef}, { rank => 13 } ] },
29     cc_result => { artistid => 1, charfield => undef, rank => 13 },
30     sql => 'WHERE artistid = ?  AND charfield IS NULL AND rank = ?',
31     efcc_result => [qw( artistid rank )],
32   },
33   {
34     where => { -and => [ -or => { name => 'Caterwauler McCrae' }, 'rank' ] },
35     cc_result => { name => 'Caterwauler McCrae', rank => undef },
36     sql => 'WHERE name = ? AND rank IS NULL',
37     efcc_result => [qw( name )],
38   },
39   {
40     where => { -and => [ [ [ artist => {'=' => \'foo' } ] ], { name => \[ '= ?', 'bar' ] } ] },
41     cc_result => { artist => {'=' => \'foo' }, name => \[ '= ?', 'bar' ] },
42     sql => 'WHERE artist = foo AND name = ?',
43     efcc_result => [qw( artist )],
44   },
45   {
46     where => { -and => [ -or => { name => 'Caterwauler McCrae', artistid => 2 } ] },
47     cc_result => { -or => [ artistid => 2, name => 'Caterwauler McCrae' ] },
48     sql => 'WHERE artistid = ? OR name = ?',
49     efcc_result => [],
50   },
51   {
52     where => { -and => [ \'foo=bar',  [ { artistid => { '=', 3 } } ], { name => 'Caterwauler McCrae'} ] },
53     cc_result => { '' => \'foo=bar', name => 'Caterwauler McCrae', artistid => 3 },
54     sql => 'WHERE foo=bar AND artistid = ? AND name = ?',
55     efcc_result => [qw( artistid name )],
56   },
57   {
58     where => { artistid => [ 1 ], rank => [ 13, 2, 3 ], charfield => [ undef ] },
59     cc_result => { artistid => 1, charfield => undef, rank => [13, 2, 3] },
60     sql => 'WHERE artistid = ? AND charfield IS NULL AND ( rank = ? OR rank = ? OR rank = ? )',
61     efcc_result => [qw( artistid )],
62   },
63   {
64     where => { artistid => { '=' => 1 }, rank => { '>' => 12 }, charfield => { '=' => undef } },
65     cc_result => { artistid => 1, charfield => undef, rank => { '>' => 12 } },
66     sql => 'WHERE artistid = ? AND charfield IS NULL AND rank > ?',
67     efcc_result => [qw( artistid )],
68   },
69   {
70     where => { artistid => { '=' => [ 1 ], }, charfield => { '=' => [-and => \'1', \['?',2] ] }, rank => { '=' => [ 1, 2 ] } },
71     cc_result => { artistid => 1, charfield => [-and => { '=' => \'1' }, { '=' => \['?',2] } ], rank => { '=' => [1, 2] } },
72     sql => 'WHERE artistid = ? AND charfield = 1 AND charfield = ? AND ( rank = ? OR rank = ? )',
73     efcc_result => [qw( artistid charfield )],
74   },
75   {
76     where => { -and => [ artistid => 1, artistid => 2 ], name => [ -and => { '!=', 1 }, 2 ], charfield => [ -or => { '=', 2 } ], rank => [-and => undef, { '=', undef }, { '!=', 2 } ] },
77     cc_result => { artistid => [ -and => 1, 2 ], name => [ -and => { '!=', 1 }, 2 ], charfield => 2, rank => [ -and => undef, undef, { '!=', 2 } ] },
78     sql => 'WHERE artistid = ? AND artistid = ? AND charfield = ? AND name != ? AND name = ? AND rank IS NULL AND rank IS NULL AND rank != ?',
79     efcc_result => [qw( artistid charfield name )],
80   },
81   {
82     where => { -and => [
83       [ '_macro.to' => { -like => '%correct%' }, '_wc_macros.to' => { -like => '%correct%' } ],
84       { -and => [ { 'group.is_active' => 1 }, { 'me.is_active' => 1 } ] }
85     ] },
86     cc_result => {
87       'group.is_active' => 1,
88       'me.is_active' => 1,
89       -or => [
90         '_macro.to' => { -like => '%correct%' },
91         '_wc_macros.to' => { -like => '%correct%' },
92       ],
93     },
94     sql => 'WHERE ( _macro.to LIKE ? OR _wc_macros.to LIKE ? ) AND group.is_active = ? AND me.is_active = ?',
95     efcc_result => [qw( group.is_active me.is_active )],
96   },
97   {
98     where => { artistid => [] },
99     cc_result => { artistid => [] },
100     efcc_result => [],
101   },
102   (map {
103     {
104       where => { -and => $_ },
105       cc_result => undef,
106       efcc_result => [],
107       sql => '',
108     },
109     {
110       where => { -or => $_ },
111       cc_result => undef,
112       efcc_result => [],
113       sql => '',
114     },
115   } (
116     # bare
117     [], {},
118     # singles
119     [ {} ], [ [] ],
120     # doubles
121     [ [], [] ], [ {}, {} ], [ [], {} ], [ {}, [] ],
122     # tripples
123     [ {}, [], {} ], [ [], {}, [] ]
124   )),
125
126   # FIXME legacy compat crap, possibly worth undef/dieing in SQLMaker
127   { where => { artistid => {} }, sql => '', cc_result => undef, efcc_result => [] },
128
129   # batshit insanity, just to be thorough
130   {
131     where => { -and => [ [ 'artistid' ], [ -and => [ artistid => { '!=', 69 }, artistid => undef, artistid => { '=' => 200 } ]], artistid => [], { -or => [] }, { -and => [] }, [ 'charfield' ], { name => [] }, 'rank' ] },
132     cc_result => { artistid => [ -and => undef, { '!=', 69 }, undef, 200, [] ], charfield => undef, name => [], rank => undef },
133     sql => 'WHERE artistid IS NULL AND artistid != ? AND artistid IS NULL AND artistid = ? AND 0=1 AND charfield IS NULL AND 0=1 AND rank IS NULL',
134     efcc_result => [qw( artistid )],
135   },
136
137   # original test from RT#93244
138   {
139     where => {
140       -and => [
141         \[
142           "LOWER(me.title) LIKE ?",
143           '%spoon%',
144         ],
145         [ { 'me.title' => 'Spoonful of bees' } ],
146     ]},
147     cc_result => {
148       '' => \[
149         "LOWER(me.title) LIKE ?",
150         '%spoon%',
151       ],
152       'me.title' => 'Spoonful of bees',
153     },
154     sql => 'WHERE LOWER(me.title) LIKE ? AND me.title = ?',
155     efcc_result => [qw( me.title )],
156   }
157 ) {
158
159   for my $w (
160     $t->{where},
161     [ -and => $t->{where} ],
162     ( keys %{$t->{where}} <= 1 ) ? [ %{$t->{where}} ] : ()
163   ) {
164     my $name = do { local ($Data::Dumper::Indent, $Data::Dumper::Terse, $Data::Dumper::Sortkeys) = (0, 1, 1); Dumper $w };
165
166     my @orig_sql_bind = $sm->where($w);
167
168     is_same_sql ( $orig_sql_bind[0], $t->{sql}, "Expected SQL from $name" )
169       if exists $t->{sql};
170
171     my $collapsed_cond = $schema->storage->_collapse_cond($w);
172
173     is_same_sql_bind(
174       \[ $sm->where($collapsed_cond) ],
175       \\@orig_sql_bind,
176       "Collapse did not alter final SQL based on $name",
177     );
178
179     is_deeply(
180       $collapsed_cond,
181       $t->{cc_result},
182       "Expected collapsed condition produced on $name",
183     );
184
185     is_deeply(
186       $schema->storage->_extract_fixed_condition_columns($w),
187       $t->{efcc_result},
188       "Expected fixed_condition produced on $name",
189     );
190   }
191 }
192
193 done_testing;