Retire DBIC/SqlMakerTest.pm now that SQLA::Test provides the same function
[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);
a5a7bb73 8use DBICTest ':DiffSQL';
e5938571 9
f6a14bd4 10my $schema = DBICTest->init_schema(no_deploy => 1);
e5938571 11
12my $sql_maker = $schema->storage->sql_maker;
13
14
20ea616f 15{
89479564 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}
b2b22cd6 47
f6a14bd4 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
8249c09b 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
d5dedbd6 100# Make sure the carp/croak override in SQLA works (via SQLMaker)
8637bb24 101my $file = quotemeta (__FILE__);
b2b22cd6 102throws_ok (sub {
103 $schema->resultset ('Artist')->search ({}, { order_by => { -asc => 'stuff', -desc => 'staff' } } )->as_query;
104}, qr/$file/, 'Exception correctly croak()ed');
f6a14bd4 105
106done_testing;