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