Actual autocast code
[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;
8
9{ # Fake storage driver for sqlite with autocast
10 package DBICTest::SQLite::AutoCast;
11 use base qw/
12 DBIx::Class::Storage::DBI::AutoCast
13 DBIx::Class::Storage::DBI::SQLite
14 /;
15 use mro 'c3';
16
17 my $type_map = {
18 datetime => 'DateTime',
19 integer => 'INT',
20 int => undef, # no conversion
21 };
22
23 sub _dbi_data_type {
24 return $type_map->{$_[1]};
25 }
26}
27
28my $schema = DBICTest->init_schema (storage_type => 'DBICTest::SQLite::AutoCast');
29
30# 'me.id' will be cast unlike the unqualified 'id'
31my $rs = $schema->resultset ('CD')->search ({
32 cdid => { '>', 5 },
33 'tracks.last_updated_at' => { '!=', undef },
34 'tracks.last_updated_on' => { '<', 2009 },
35 'tracks.position' => 4,
36}, { join => 'tracks' });
37
38my $bind = [ [ cdid => 5 ], [ 'tracks.last_updated_on' => 2009 ], [ 'tracks.position' => 4 ] ];
39
40is_same_sql_bind (
41 $rs->as_query,
42 '(
43 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
44 FROM cd me
45 LEFT JOIN track tracks ON tracks.cd = me.cdid
46 WHERE
47 cdid > ?
48 AND tracks.last_updated_at IS NOT NULL
49 AND tracks.last_updated_on < ?
50 AND tracks.position = ?
51 )',
52 $bind,
53 'expected sql with casting off',
54);
55
56$schema->storage->auto_cast (1);
57
58is_same_sql_bind (
59 $rs->as_query,
60 '(
61 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
62 FROM cd me
63 LEFT JOIN track tracks ON tracks.cd = me.cdid
64 WHERE
65 cdid > CAST(? AS INT)
66 AND tracks.last_updated_at IS NOT NULL
67 AND tracks.last_updated_on < CAST (? AS yyy)
68 AND tracks.position = ?
69 )',
70 $bind,
71 'expected sql with casting on',
72);
73
74done_testing;