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