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