Some databases (db2) do not like leading __s - use a different weird identifier for...
[dbsrgits/DBIx-Class.git] / t / inflate / datetime_informix.t
CommitLineData
b0a4cf8e 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6use lib qw(t/lib);
7use DBICTest;
8use Scope::Guard ();
9
10my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_INFORMIX_${_}" } qw/DSN USER PASS/};
11
12if (not $dsn) {
13 plan skip_all => <<'EOF';
14Set $ENV{DBICTEST_INFORMIX_DSN} _USER and _PASS to run this test'.
15Warning: This test drops and creates a table called 'event'";
16EOF
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
24my $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 );
40SQL
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
67done_testing;
68
69# clean up our mess
70sub 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}