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