Only normalize conditions during resolution time, instead on every ->search
[dbsrgits/DBIx-Class.git] / t / 93autocast.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
d047d650 3use strict;
4use warnings;
5
6use Test::More;
c0329273 7
d047d650 8use DBICTest;
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
2cfc22dd 40my @bind = (
41 [ { dbic_colname => "cdid", sqlt_datatype => "integer" }
42 => 5 ],
43 [ { dbic_colname => "single_track", sqlt_datatype => "integer" }
44 => 1 ],
45 [ { dbic_colname => "tracks.last_updated_on", sqlt_datatype => "datetime" }
46 => 2009 ],
47 [ { dbic_colname => "tracks.position", sqlt_datatype => "int" }
48 => 4 ],
49);
5e782048 50
2cfc22dd 51$schema->is_executed_sql_bind( sub { $rs->all }, [[
5e782048 52 '
d047d650 53 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
54 FROM cd me
55 LEFT JOIN track tracks ON tracks.cd = me.cdid
56 WHERE
57 cdid > ?
0e773352 58 AND me.single_track = ?
d047d650 59 AND tracks.last_updated_at IS NOT NULL
60 AND tracks.last_updated_on < ?
61 AND tracks.position = ?
5e782048 62 ',
2cfc22dd 63 @bind,
64]], 'expected sql with casting off' );
d047d650 65
66$schema->storage->auto_cast (1);
67
2cfc22dd 68$schema->is_executed_sql_bind( sub { $rs->all }, [[
5e782048 69 '
d047d650 70 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
71 FROM cd me
72 LEFT JOIN track tracks ON tracks.cd = me.cdid
73 WHERE
74 cdid > CAST(? AS INT)
0e773352 75 AND me.single_track = CAST(? AS INT)
d047d650 76 AND tracks.last_updated_at IS NOT NULL
d0e58487 77 AND tracks.last_updated_on < CAST (? AS DateTime)
d047d650 78 AND tracks.position = ?
5e782048 79 ',
2cfc22dd 80 @bind,
81]], 'expected sql with casting on' );
5e782048 82
d047d650 83done_testing;