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