Take a copy instead of weakening in 5.8 leak workaround
[dbsrgits/DBIx-Class.git] / t / inflate / datetime_firebird.t
CommitLineData
9cd0b325 1use strict;
68de9438 2use warnings;
9cd0b325 3
4use Test::More;
199fbc45 5use DBIx::Class::Optional::Dependencies ();
9cd0b325 6use lib qw(t/lib);
7use DBICTest;
8use Scope::Guard ();
9
2b1fff81 10my $env2optdep = {
11 DBICTEST_FIREBIRD => 'test_rdbms_firebird',
12 DBICTEST_FIREBIRD_INTERBASE => 'test_rdbms_firebird_interbase',
13 DBICTEST_FIREBIRD_ODBC => 'test_rdbms_firebird_odbc',
14};
15
16plan skip_all => join (' ',
17 'Set $ENV{DBICTEST_FIREBIRD_DSN} and/or $ENV{DBICTEST_FIREBIRD_INTERBASE_DSN}',
18 'and/or $ENV{DBICTEST_FIREBIRD_ODBC_DSN},',
19 '_USER and _PASS to run these tests.',
9cd0b325 20
2b1fff81 21 "WARNING: This test drops and creates a table called 'event'",
22) unless grep { $ENV{"${_}_DSN"} } keys %$env2optdep;
23
24plan skip_all => ( 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for('test_dt') )
25 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_dt');
9cd0b325 26
27my $schema;
28
2b1fff81 29for my $prefix (keys %$env2optdep) { SKIP: {
30
31 my ($dsn, $user, $pass) = map { $ENV{"${prefix}_$_"} } qw/DSN USER PASS/;
9cd0b325 32
33 next unless $dsn;
34
2b1fff81 35 note "Testing with ${prefix}_DSN";
36
37 skip ("Testing with ${prefix}_DSN needs " . DBIx::Class::Optional::Dependencies->req_missing_for( $env2optdep->{$prefix} ), 1)
38 unless DBIx::Class::Optional::Dependencies->req_ok_for($env2optdep->{$prefix});
39
2c2bc4e5 40 $schema = DBICTest->connect_schema($dsn, $user, $pass, {
c5827074 41 quote_char => '"',
8273e845 42 name_sep => '.',
9cd0b325 43 on_connect_call => [ 'datetime_setup' ],
44 });
45
65d35121 46 my $sg = Scope::Guard->new(sub { cleanup($schema) } );
9cd0b325 47
c5827074 48 eval { $schema->storage->dbh->do('DROP TABLE "event"') };
49 $schema->storage->dbh->do(<<'SQL');
50 CREATE TABLE "event" (
51 "id" INT PRIMARY KEY,
52 "starts_at" DATE,
53 "created_on" TIMESTAMP
9cd0b325 54 )
55SQL
a870aa85 56 my $rs = $schema->resultset('Event');
c5827074 57
58 my $dt = DateTime->now;
a870aa85 59 $dt->set_nanosecond(555600000);
c5827074 60
61 my $date_only = DateTime->new(
62 year => $dt->year, month => $dt->month, day => $dt->day
63 );
64
9cd0b325 65 my $row;
c5827074 66 ok( $row = $rs->create({
67 id => 1,
8273e845 68 starts_at => $date_only,
c5827074 69 created_on => $dt,
70 }));
71 ok( $row = $rs->search({ id => 1 }, { select => [qw/starts_at created_on/] })
9cd0b325 72 ->first
73 );
c5827074 74 is $row->created_on, $dt, 'TIMESTAMP as DateTime roundtrip';
32323fc2 75
c5827074 76 cmp_ok $row->created_on->nanosecond, '==', $dt->nanosecond,
a870aa85 77 'fractional part of a second survived';
c5827074 78
79 is $row->starts_at, $date_only, 'DATE as DateTime roundtrip';
2b1fff81 80} }
9cd0b325 81
82done_testing;
83
84# clean up our mess
85sub cleanup {
65d35121 86 my $schema = shift;
8273e845 87 my $dbh;
9cd0b325 88 eval {
89 $schema->storage->disconnect; # to avoid object FOO is in use errors
90 $dbh = $schema->storage->dbh;
91 };
92 return unless $dbh;
93
c5827074 94 eval { $dbh->do(qq{DROP TABLE "$_"}) } for qw/event/;
9cd0b325 95}