Fix missing true return lost during e5a62c46f
[dbsrgits/DBIx-Class.git] / t / 93autocast.t
CommitLineData
d047d650 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7use DBIC::SqlMakerTest;
5e782048 8use DBIC::DebugObj;
d047d650 9
10{ # Fake storage driver for sqlite with autocast
11 package DBICTest::SQLite::AutoCast;
12 use base qw/
13 DBIx::Class::Storage::DBI::AutoCast
14 DBIx::Class::Storage::DBI::SQLite
15 /;
16 use mro 'c3';
17
18 my $type_map = {
19 datetime => 'DateTime',
20 integer => 'INT',
21 int => undef, # no conversion
22 };
23
0bd2c1cd 24 sub _native_data_type {
d047d650 25 return $type_map->{$_[1]};
26 }
27}
28
29my $schema = DBICTest->init_schema (storage_type => 'DBICTest::SQLite::AutoCast');
30
31# 'me.id' will be cast unlike the unqualified 'id'
32my $rs = $schema->resultset ('CD')->search ({
33 cdid => { '>', 5 },
34 'tracks.last_updated_at' => { '!=', undef },
35 'tracks.last_updated_on' => { '<', 2009 },
36 'tracks.position' => 4,
5e782048 37 'me.single_track' => \[ '= ?', [ single_track => 1 ] ],
d047d650 38}, { join => 'tracks' });
39
5e782048 40my ($sql, @bind);
41my $debugobj = DBIC::DebugObj->new (\$sql, \@bind);
42my $storage = $schema->storage;
43my ($orig_debug, $orig_debugobj) = ($storage->debug, $storage->debugobj);
44$storage->debugobj ($debugobj);
45$storage->debug (1);
d047d650 46
5e782048 47# the quoting is a debugobj thing, not dbic-internals
48my $bind = [ map { "'$_'" } qw/
49 5 1 2009 4
50/];
51
52$rs->all;
d047d650 53is_same_sql_bind (
5e782048 54 $sql,
55 \@bind,
56 '
d047d650 57 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
58 FROM cd me
59 LEFT JOIN track tracks ON tracks.cd = me.cdid
60 WHERE
61 cdid > ?
0e773352 62 AND me.single_track = ?
d047d650 63 AND tracks.last_updated_at IS NOT NULL
64 AND tracks.last_updated_on < ?
65 AND tracks.position = ?
5e782048 66 ',
d047d650 67 $bind,
68 'expected sql with casting off',
69);
70
71$schema->storage->auto_cast (1);
72
5e782048 73$rs->all;
d047d650 74is_same_sql_bind (
5e782048 75 $sql,
76 \@bind,
77 '
d047d650 78 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
79 FROM cd me
80 LEFT JOIN track tracks ON tracks.cd = me.cdid
81 WHERE
82 cdid > CAST(? AS INT)
0e773352 83 AND me.single_track = CAST(? AS INT)
d047d650 84 AND tracks.last_updated_at IS NOT NULL
d0e58487 85 AND tracks.last_updated_on < CAST (? AS DateTime)
d047d650 86 AND tracks.position = ?
5e782048 87 ',
d047d650 88 $bind,
89 'expected sql with casting on',
90);
91
5e782048 92$storage->debugobj ($orig_debugobj);
93$storage->debug ($orig_debug);
94
d047d650 95done_testing;