Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / t / 93autocast.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 use DBICTest;
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 @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 );
50
51 $schema->is_executed_sql_bind( sub { $rs->all }, [[
52   '
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 > ?
58       AND me.single_track = ?
59       AND tracks.last_updated_at IS NOT NULL
60       AND tracks.last_updated_on < ?
61       AND tracks.position = ?
62   ',
63   @bind,
64 ]], 'expected sql with casting off' );
65
66 $schema->storage->auto_cast (1);
67
68 $schema->is_executed_sql_bind( sub { $rs->all }, [[
69   '
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)
75       AND me.single_track = CAST(? AS INT)
76       AND tracks.last_updated_at IS NOT NULL
77       AND tracks.last_updated_on < CAST (? AS DateTime)
78       AND tracks.position = ?
79   ',
80   @bind,
81 ]], 'expected sql with casting on' );
82
83 done_testing;