Really fix @INC munging of the taint test
[dbsrgits/DBIx-Class.git] / t / 93autocast.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7 use DBIC::SqlMakerTest;
8 use DBIC::DebugObj;
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
24     sub _native_data_type {
25       return $type_map->{$_[1]};
26     }
27 }
28
29 my $schema = DBICTest->init_schema (storage_type => 'DBICTest::SQLite::AutoCast');
30
31 # 'me.id' will be cast unlike the unqualified 'id'
32 my $rs = $schema->resultset ('CD')->search ({
33   cdid => { '>', 5 },
34   'tracks.last_updated_at' => { '!=', undef },
35   'tracks.last_updated_on' => { '<', 2009 },
36   'tracks.position' => 4,
37   'me.single_track' => \[ '= ?', [ single_track => 1 ] ],
38 }, { join => 'tracks' });
39
40 my ($sql, @bind);
41 my $debugobj = DBIC::DebugObj->new (\$sql, \@bind);
42 my $storage = $schema->storage;
43 my ($orig_debug, $orig_debugobj) = ($storage->debug, $storage->debugobj);
44 $storage->debugobj ($debugobj);
45 $storage->debug (1);
46
47 # the quoting is a debugobj thing, not dbic-internals
48 my $bind = [ map { "'$_'" } qw/
49   5 1 2009 4
50 /];
51
52 $rs->all;
53 is_same_sql_bind (
54   $sql,
55   \@bind,
56   '
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 > ?
62       AND me.single_track = ?
63       AND tracks.last_updated_at IS NOT NULL
64       AND tracks.last_updated_on < ?
65       AND tracks.position = ?
66   ',
67   $bind,
68   'expected sql with casting off',
69 );
70
71 $schema->storage->auto_cast (1);
72
73 $rs->all;
74 is_same_sql_bind (
75   $sql,
76   \@bind,
77   '
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)
83       AND me.single_track = CAST(? AS INT)
84       AND tracks.last_updated_at IS NOT NULL
85       AND tracks.last_updated_on < CAST (? AS DateTime)
86       AND tracks.position = ?
87   ',
88   $bind,
89   'expected sql with casting on',
90 );
91
92 $storage->debugobj ($orig_debugobj);
93 $storage->debug ($orig_debug);
94
95 done_testing;