Massive cleanup of DateTime test dependencies, other interim
[dbsrgits/DBIx-Class.git] / t / inflate / datetime_mssql.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use Scope::Guard ();
7 use lib qw(t/lib);
8 use DBICTest;
9
10 # use this if you keep a copy of DBD::Sybase linked to FreeTDS somewhere else
11 BEGIN {
12   if (my $lib_dirs = $ENV{DBICTEST_MSSQL_PERL5LIB}) {
13     unshift @INC, $_ for split /:/, $lib_dirs;
14   }
15 }
16
17 my ($dsn, $user, $pass)    = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PASS/};
18 my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_MSSQL_${_}" }      qw/DSN USER PASS/};
19
20 if (not ($dsn || $dsn2)) {
21   plan skip_all =>
22     'Set $ENV{DBICTEST_MSSQL_ODBC_DSN} and/or $ENV{DBICTEST_MSSQL_DSN} _USER '
23     .'and _PASS to run this test' .
24     "\nWarning: This test drops and creates a table called 'track'";
25 }
26
27 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_dt')
28   unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_dt');
29
30 my @connect_info = (
31   [ $dsn,  $user,  $pass ],
32   [ $dsn2, $user2, $pass2 ],
33 );
34
35 my $schema;
36
37 SKIP:
38 for my $connect_info (@connect_info) {
39   my ($dsn, $user, $pass) = @$connect_info;
40
41   next unless $dsn;
42
43   $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
44     on_connect_call => 'datetime_setup'
45   });
46
47   {
48     my $w;
49     local $SIG{__WARN__} = sub { $w = shift };
50     $schema->storage->ensure_connected;
51     if ($w =~ /Your DBD::Sybase is too old to support DBIx::Class::InflateColumn::DateTime/) {
52       skip "Skipping tests on old DBD::Sybase " . DBD::Sybase->VERSION, 1;
53     }
54   }
55
56   my $guard = Scope::Guard->new(\&cleanup);
57
58 # coltype, column, datehash
59   my @dt_types = (
60     ['DATETIME',
61      'last_updated_at',
62      {
63       year => 2004,
64       month => 8,
65       day => 21,
66       hour => 14,
67       minute => 36,
68       second => 48,
69       nanosecond => 500000000,
70     }],
71     ['SMALLDATETIME', # minute precision
72      'small_dt',
73      {
74       year => 2004,
75       month => 8,
76       day => 21,
77       hour => 14,
78       minute => 36,
79     }],
80   );
81
82   for my $dt_type (@dt_types) {
83     my ($type, $col, $sample_dt) = @$dt_type;
84
85     eval { $schema->storage->dbh->do("DROP TABLE track") };
86     $schema->storage->dbh->do(<<"SQL");
87 CREATE TABLE track (
88  trackid INT IDENTITY PRIMARY KEY,
89  cd INT,
90  position INT,
91  $col $type,
92 )
93 SQL
94     ok(my $dt = DateTime->new($sample_dt));
95
96     my $row;
97     ok( $row = $schema->resultset('Track')->create({
98           $col => $dt,
99           cd => 1,
100         }));
101     ok( $row = $schema->resultset('Track')
102       ->search({ trackid => $row->trackid }, { select => [$col] })
103       ->first
104     );
105     is( $row->$col, $dt, "$type roundtrip" );
106
107     cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond},
108       'DateTime fractional portion roundtrip' )
109       if exists $sample_dt->{nanosecond};
110   }
111 }
112
113 done_testing;
114
115 # clean up our mess
116 sub cleanup {
117   if (my $dbh = eval { $schema->storage->dbh }) {
118     $dbh->do('DROP TABLE track');
119   }
120 }