Commit | Line | Data |
9cd0b325 |
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 | |
9cd0b325 |
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 | } 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 | |
26 | my @info = ( |
27 | [ $dsn, $user, $pass ], |
28 | [ $dsn2, $user2, $pass2 ], |
29 | ); |
30 | |
31 | my $schema; |
32 | |
32323fc2 |
33 | foreach 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 | ) |
53 | SQL |
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 | |
80 | done_testing; |
81 | |
82 | # clean up our mess |
83 | sub 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 | } |