Make sure DBICTest is always loaded first (purely bookkeep)
[dbsrgits/DBIx-Class.git] / t / sqlmaker / core.t
CommitLineData
e5938571 1use strict;
2use warnings;
3
4use Test::More;
b2b22cd6 5use Test::Exception;
e5938571 6
c61a0748 7use lib qw(t/lib);
5e724964 8use DBICTest;
c61a0748 9use DBIC::SqlMakerTest;
e5938571 10
f6a14bd4 11my $schema = DBICTest->init_schema(no_deploy => 1);
e5938571 12
13my $sql_maker = $schema->storage->sql_maker;
14
15
20ea616f 16{
89479564 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}
b2b22cd6 48
f6a14bd4 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
8249c09b 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
d5dedbd6 101# Make sure the carp/croak override in SQLA works (via SQLMaker)
8637bb24 102my $file = quotemeta (__FILE__);
b2b22cd6 103throws_ok (sub {
104 $schema->resultset ('Artist')->search ({}, { order_by => { -asc => 'stuff', -desc => 'staff' } } )->as_query;
105}, qr/$file/, 'Exception correctly croak()ed');
f6a14bd4 106
107done_testing;