Merge 'trunk' into 'storage-interbase'
[dbsrgits/DBIx-Class.git] / t / inflate / datetime_firebird.t
CommitLineData
9cd0b325 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6use lib qw(t/lib);
7use DBICTest;
8use Scope::Guard ();
9
10# XXX we're only testing TIMESTAMP here
11
12my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_FIREBIRD_${_}" } qw/DSN USER PASS/};
13my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_FIREBIRD_ODBC_${_}" } qw/DSN USER PASS/};
14
15if (not ($dsn || $dsn2)) {
16 plan skip_all => <<'EOF';
17Set $ENV{DBICTEST_FIREBIRD_DSN} and/or $ENV{DBICTEST_FIREBIRD_ODBC_DSN}
18_USER and _PASS to run this test'.
19Warning: This test drops and creates a table called 'event'";
20EOF
21} else {
22 eval "use DateTime; use DateTime::Format::Strptime;";
23 if ($@) {
24 plan skip_all => 'needs DateTime and DateTime::Format::Strptime for testing';
25 }
26}
27
28my @info = (
29 [ $dsn, $user, $pass ],
30 [ $dsn2, $user2, $pass2 ],
31);
32
33my $schema;
34
32323fc2 35foreach my $conn_idx (0..$#info) {
c5827074 36 my ($dsn, $user, $pass) = @{ $info[$conn_idx] || [] };
9cd0b325 37
38 next unless $dsn;
39
32323fc2 40 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
c5827074 41 quote_char => '"',
42 name_sep => '.',
9cd0b325 43 on_connect_call => [ 'datetime_setup' ],
44 });
45
46 my $sg = Scope::Guard->new(\&cleanup);
47
c5827074 48 eval { $schema->storage->dbh->do('DROP TABLE "event"') };
49 $schema->storage->dbh->do(<<'SQL');
50 CREATE TABLE "event" (
51 "id" INT PRIMARY KEY,
52 "starts_at" DATE,
53 "created_on" TIMESTAMP
9cd0b325 54 )
55SQL
c5827074 56 my $rs = $schema->resultset('Event');
57
58 my $dt = DateTime->now;
59 $dt->set_nanosecond($dsn =~ /odbc/i ? 0 : 555600000);
60
61 my $date_only = DateTime->new(
62 year => $dt->year, month => $dt->month, day => $dt->day
63 );
64
9cd0b325 65 my $row;
c5827074 66 ok( $row = $rs->create({
67 id => 1,
68 starts_at => $date_only,
69 created_on => $dt,
70 }));
71 ok( $row = $rs->search({ id => 1 }, { select => [qw/starts_at created_on/] })
9cd0b325 72 ->first
73 );
c5827074 74 is $row->created_on, $dt, 'TIMESTAMP as DateTime roundtrip';
32323fc2 75
c5827074 76 cmp_ok $row->created_on->nanosecond, '==', $dt->nanosecond,
77 'fractional part of a second survived' if 0+$dt->nanosecond;
78
79 is $row->starts_at, $date_only, 'DATE as DateTime roundtrip';
9cd0b325 80}
81
82done_testing;
83
84# clean up our mess
85sub cleanup {
86 my $dbh;
87 eval {
88 $schema->storage->disconnect; # to avoid object FOO is in use errors
89 $dbh = $schema->storage->dbh;
90 };
91 return unless $dbh;
92
c5827074 93 eval { $dbh->do(qq{DROP TABLE "$_"}) } for qw/event/;
9cd0b325 94}