af23410fecf22889c9eb6f79a91db0db212f599f
[dbsrgits/DBIx-Class.git] / t / inflate / datetime_informix.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_INFORMIX_${_}" } qw/DSN USER PASS/};
11
12 if (not $dsn) {
13   plan skip_all => <<'EOF';
14 Set $ENV{DBICTEST_INFORMIX_DSN} _USER and _PASS to run this test'.
15 Warning: This test drops and creates a table called 'event'";
16 EOF
17 } else {
18   eval "use DateTime; use DateTime::Format::Strptime;";
19   if ($@) {
20     plan skip_all => 'needs DateTime and DateTime::Format::Strptime for testing';
21   }
22 }
23
24 my $schema;
25
26 {
27   $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
28     on_connect_call => [ 'datetime_setup' ],
29   });
30
31   my $sg = Scope::Guard->new(\&cleanup);
32
33   eval { $schema->storage->dbh->do('DROP TABLE event') };
34   $schema->storage->dbh->do(<<'SQL');
35   CREATE TABLE event (
36     id INT PRIMARY KEY,
37     starts_at DATE,
38     created_on DATETIME YEAR TO FRACTION(5)
39   );
40 SQL
41   my $rs = $schema->resultset('Event');
42
43   my $dt = DateTime->now;
44   $dt->set_nanosecond(555640000);
45
46   my $date_only = DateTime->new(
47     year => $dt->year, month => $dt->month, day => $dt->day
48   );
49
50   my $row;
51   ok( $row = $rs->create({
52     id => 1,
53     starts_at => $date_only, 
54     created_on => $dt,
55   }));
56   ok( $row = $rs->search({ id => 1 }, { select => [qw/starts_at created_on/] })
57     ->first
58   );
59   is $row->created_on, $dt, 'TIMESTAMP as DateTime roundtrip';
60
61   cmp_ok $row->created_on->nanosecond, '==', $dt->nanosecond,
62     'fractional part of a second survived';
63
64   is $row->starts_at, $date_only, 'DATE as DateTime roundtrip';
65 }
66
67 done_testing;
68
69 # clean up our mess
70 sub cleanup {
71   my $dbh; 
72   eval {
73     $dbh = $schema->storage->dbh;
74   };
75   return unless $dbh;
76
77   eval { $dbh->do(qq{DROP TABLE $_}) } for qw/event/;
78 }