b2f2a8c67c9447abb808f9657a2f99ff2ada65bb
[dbsrgits/DBIx-Class.git] / t / inflate / datetime_msaccess.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Scope::Guard ();
6 use Try::Tiny;
7 use DBIx::Class::Optional::Dependencies ();
8 use lib qw(t/lib);
9 use DBICTest;
10
11 my ($dsn,  $user,  $pass)  = @ENV{map { "DBICTEST_MSACCESS_ODBC_${_}" } qw/DSN USER PASS/};
12 my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_MSACCESS_ADO_${_}" }  qw/DSN USER PASS/};
13
14 plan skip_all => 'Test needs ' .
15   (join ' and ', map { $_ ? $_ : () }
16     DBIx::Class::Optional::Dependencies->req_missing_for('test_dt'),
17     (join ' or ', map { $_ ? $_ : () }
18       DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_msaccess_odbc'),
19       DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_msaccess_ado')))
20   unless
21     DBIx::Class::Optional::Dependencies->req_ok_for ('test_dt') && (
22     $dsn && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_msaccess_odbc')
23     or
24     $dsn2 && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_msaccess_ado'))
25       or (not $dsn || $dsn2);
26
27 plan skip_all => <<'EOF' unless $dsn || $dsn2;
28 Set $ENV{DBICTEST_MSACCESS_ODBC_DSN} and/or $ENV{DBICTEST_MSACCESS_ADO_DSN} (and optionally _USER and _PASS) to run these tests.
29 Warning: this test drops and creates the table 'track'.
30 EOF
31
32 my @connect_info = (
33   [ $dsn,  $user  || '', $pass  || '' ],
34   [ $dsn2, $user2 || '', $pass2 || '' ],
35 );
36
37 for my $connect_info (@connect_info) {
38   my ($dsn, $user, $pass) = @$connect_info;
39
40   next unless $dsn;
41
42   my $schema = DBICTest->connect_schema($dsn, $user, $pass, {
43     on_connect_call => 'datetime_setup',
44     quote_names => 1,
45   });
46
47   my $guard = Scope::Guard->new(sub { cleanup($schema) });
48
49   try { local $^W = 0; $schema->storage->dbh->do('DROP TABLE track') };
50   $schema->storage->dbh->do(<<"SQL");
51 CREATE TABLE track (
52   trackid AUTOINCREMENT PRIMARY KEY,
53   cd INT,
54   [position] INT,
55   last_updated_at DATETIME
56 )
57 SQL
58
59   ok(my $dt = DateTime->new({
60     year => 2004,
61     month => 8,
62     day => 21,
63     hour => 14,
64     minute => 36,
65     second => 48,
66   }));
67
68   ok(my $row = $schema->resultset('Track')->create({
69     last_updated_at => $dt,
70     cd => 1
71   }));
72   ok($row = $schema->resultset('Track')
73     ->search({ trackid => $row->trackid }, { select => ['last_updated_at'] })
74     ->first
75   );
76   is($row->last_updated_at, $dt, "DATETIME roundtrip" );
77 }
78
79 done_testing;
80
81 # clean up our mess
82 sub cleanup {
83   my $schema = shift;
84   # have to reconnect to drop a table that's in use
85   if (my $storage = eval { $schema->storage }) {
86     local $^W = 0;
87     $storage->disconnect;
88     $storage->dbh->do('DROP TABLE track');
89   }
90 }