more informative tests
[dbsrgits/DBIx-Class.git] / t / sqlmaker / op_dt.t
CommitLineData
5e6893d4 1use strict;
2use warnings;
3
4use Test::More;
6edb8b2f 5use Test::Fatal;
5e6893d4 6
7use lib qw(t/lib);
8use DBIC::SqlMakerTest;
9use DateTime;
10
11use_ok('DBICTest');
12
13my $schema = DBICTest->init_schema();
14
15my $sql_maker = $schema->storage->sql_maker;
16
17my $date = DateTime->new(
18 year => 2010,
19 month => 12,
20 day => 14,
21 hour => 12,
22 minute => 12,
23 second => 12,
24);
25
26my $date2 = $date->clone->set_day(16);
27
28is_same_sql_bind (
29 \[ $sql_maker->select ('artist', '*', { 'artist.when_began' => { -dt => $date } } ) ],
30 "SELECT *
31 FROM artist
32 WHERE artist.when_began = ?
33 ",
34 [['artist.when_began', '2010-12-14 12:12:12']],
6edb8b2f 35 '-dt works'
5e6893d4 36);
37
38is_same_sql_bind (
39 \[ $sql_maker->update ('artist',
40 { 'artist.when_began' => { -dt => $date } },
41 { 'artist.when_ended' => { '<' => { -dt => $date2 } } },
42 ) ],
43 "UPDATE artist
44 SET artist.when_began = ?
45 WHERE artist.when_ended < ?
46 ",
47 [
48 ['artist.when_began', '2010-12-14 12:12:12'],
49 ['artist.when_ended', '2010-12-16 12:12:12'],
50 ],
6edb8b2f 51 '-dt works'
5e6893d4 52);
53
54is_same_sql_bind (
55 \[ $sql_maker->select ('artist', '*', {
56 -and => [
57 { -op => [ '=', 12, { -dt_month => { -ident => 'artist.when_began' } } ] },
58 { -op => [ '=', 2010, { -dt_get => [year => \'artist.when_began'] } ] },
59 { -op => [ '=', 14, { -dt_get => [day_of_month => \'artist.when_began'] } ] },
6edb8b2f 60 { -op => [ '=', 100, { -dt_diff => [second => { -ident => 'artist.when_began' }, \'artist.when_ended'] } ] },
61 { -op => [ '=', 10, { -dt_diff => [day => { -ident => 'artist.when_played_last' }, \'artist.when_ended'] } ] },
5e6893d4 62 ]
63 } ) ],
64 "SELECT *
65 FROM artist
66 WHERE ( (
c9700c1c 67 ( ? = STRFTIME('%m', artist.when_began) ) AND
68 ( ? = STRFTIME('%Y', artist.when_began) ) AND
69 ( ? = STRFTIME('%d', artist.when_began) ) AND
6edb8b2f 70 ( ? = ( STRFTIME('%s', artist.when_began) - STRFTIME('%s', artist.when_ended))) AND
71 ( ? = ( JULIANDAY(artist.when_played_last) - JULIANDAY(artist.when_ended)))
5e6893d4 72 ) )
73 ",
74 [
75 ['', 12],
76 ['', 2010],
77 ['', 14],
6edb8b2f 78 ['', 100],
5e6893d4 79 ['', 10],
80 ],
6edb8b2f 81 '-dt_month, -dt_get, and -dt_diff work'
5e6893d4 82);
83
6edb8b2f 84like exception { $sql_maker->select('foo', '*', { -dt_diff => [year => \'artist.lololol', \'artist.fail'] }) }, qr/date diff not supported for part "year" with database "SQLite"/, 'SQLite does not support year diff';
85
5e6893d4 86done_testing;