Deduplicate (and stabilize) the result of _collapse_cond
[dbsrgits/DBIx-Class.git] / t / sqlmaker / core.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6
7 use lib qw(t/lib);
8 use DBICTest ':DiffSQL';
9
10 my $schema = DBICTest->init_schema(no_deploy => 1);
11
12 my $sql_maker = $schema->storage->sql_maker;
13
14
15 {
16   my ($sql, @bind) = $sql_maker->insert(
17             'lottery',
18             {
19               'day' => '2008-11-16',
20               'numbers' => [13, 21, 34, 55, 89]
21             }
22   );
23
24   is_same_sql_bind(
25     $sql, \@bind,
26     q/INSERT INTO lottery (day, numbers) VALUES (?, ?)/,
27       [ ['day' => '2008-11-16'], ['numbers' => [13, 21, 34, 55, 89]] ],
28     'sql_maker passes arrayrefs in insert'
29   );
30
31
32   ($sql, @bind) = $sql_maker->update(
33             'lottery',
34             {
35               'day' => '2008-11-16',
36               'numbers' => [13, 21, 34, 55, 89]
37             }
38   );
39
40   is_same_sql_bind(
41     $sql, \@bind,
42     q/UPDATE lottery SET day = ?, numbers = ?/,
43       [ ['day' => '2008-11-16'], ['numbers' => [13, 21, 34, 55, 89]] ],
44     'sql_maker passes arrayrefs in update'
45   );
46 }
47
48 # make sure the cookbook caveat of { $op, \'...' } no longer applies
49 {
50   my ($sql, @bind) = $sql_maker->where({
51     last_attempt => \ '< now() - interval "12 hours"',
52     next_attempt => { '<', \ 'now() - interval "12 hours"' },
53     created => [
54       { '<=', \ '1969' },
55       \ '> 1984',
56     ],
57   });
58   is_same_sql_bind(
59     $sql,
60     \@bind,
61     'WHERE
62           (created <= 1969 OR created > 1984 )
63       AND last_attempt < now() - interval "12 hours"
64       AND next_attempt < now() - interval "12 hours"
65     ',
66     [],
67   );
68 }
69
70 # Tests base class for => \'FOO' actually generates proper query. for =>
71 # 'READ'|'SHARE' is tested in db-specific subclasses
72 # we have to instantiate base because SQLMaker::SQLite disables _lock_select
73 {
74   require DBIx::Class::SQLMaker;
75   my $sa = DBIx::Class::SQLMaker->new;
76   {
77     my ($sql, @bind) = $sa->select('foo', '*', {}, { for => 'update' } );
78     is_same_sql_bind(
79       $sql,
80       \@bind,
81       'SELECT * FROM foo FOR UPDATE',
82       [],
83     );
84   }
85
86   {
87     my ($sql, @bind) = $sa->select('bar', '*', {}, { for => \'baz' } );
88     is_same_sql_bind(
89       $sql,
90       \@bind,
91       'SELECT * FROM bar FOR baz',
92       [],
93     );
94   }
95
96 }
97
98
99
100 # Make sure the carp/croak override in SQLA works (via SQLMaker)
101 my $file = quotemeta (__FILE__);
102 throws_ok (sub {
103   $schema->resultset ('Artist')->search ({}, { order_by => { -asc => 'stuff', -desc => 'staff' } } )->as_query;
104 }, qr/$file/, 'Exception correctly croak()ed');
105
106 done_testing;