Institute a central "load this first in testing" package
[dbsrgits/DBIx-Class.git] / t / icdt / engine_specific / informix.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2 use DBIx::Class::Optional::Dependencies -skip_all_without => qw( ic_dt test_rdbms_informix );
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8 use DBIx::Class::_Util 'scope_guard';
9
10 use DBICTest;
11
12 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_INFORMIX_${_}" } qw/DSN USER PASS/};
13 my $schema;
14
15 {
16   $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
17     on_connect_call => [ 'datetime_setup' ],
18   });
19
20   my $sg = scope_guard { cleanup($schema) };
21
22   eval { $schema->storage->dbh->do('DROP TABLE event') };
23   $schema->storage->dbh->do(<<'SQL');
24   CREATE TABLE event (
25     id INT PRIMARY KEY,
26     starts_at DATE,
27     created_on DATETIME YEAR TO FRACTION(5)
28   );
29 SQL
30   my $rs = $schema->resultset('Event');
31
32   my $dt = DateTime->now;
33   $dt->set_nanosecond(555640000);
34
35   my $date_only = DateTime->new(
36     year => $dt->year, month => $dt->month, day => $dt->day
37   );
38
39   my $row;
40   ok( $row = $rs->create({
41     id => 1,
42     starts_at => $date_only,
43     created_on => $dt,
44   }));
45   ok( $row = $rs->search({ id => 1 }, { select => [qw/starts_at created_on/] })
46     ->first
47   );
48   is $row->created_on, $dt, 'TIMESTAMP as DateTime roundtrip';
49
50   cmp_ok $row->created_on->nanosecond, '==', $dt->nanosecond,
51     'fractional part of a second survived';
52
53   is $row->starts_at, $date_only, 'DATE as DateTime roundtrip';
54 }
55
56 done_testing;
57
58 # clean up our mess
59 sub cleanup {
60   my $schema = shift;
61   my $dbh;
62   eval {
63     $dbh = $schema->storage->dbh;
64   };
65   return unless $dbh;
66
67   eval { $dbh->do(qq{DROP TABLE $_}) } for qw/event/;
68 }