44984a320e8c71fcb3e3f95a0e56695dcaf1a1a2
[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 } else {
26   eval "use DateTime; use DateTime::Format::Strptime;";
27   if ($@) {
28     plan skip_all => 'needs DateTime and DateTime::Format::Strptime for testing';
29   }
30 }
31
32 my @connect_info = (
33   [ $dsn,  $user,  $pass ],
34   [ $dsn2, $user2, $pass2 ],
35 );
36
37 my $schema;
38
39 SKIP:
40 for my $connect_info (@connect_info) {
41   my ($dsn, $user, $pass) = @$connect_info;
42
43   next unless $dsn;
44
45   $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
46     on_connect_call => 'datetime_setup'
47   });
48
49   {
50     my $w;
51     local $SIG{__WARN__} = sub { $w = shift };
52     $schema->storage->ensure_connected;
53     if ($w =~ /Your DBD::Sybase is too old to support DBIx::Class::InflateColumn::DateTime/) {
54       skip "Skipping tests on old DBD::Sybase " . DBD::Sybase->VERSION, 1;
55     }
56   }
57
58   my $guard = Scope::Guard->new(\&cleanup);
59
60 # coltype, column, datehash
61   my @dt_types = (
62     ['DATETIME',
63      'last_updated_at',
64      {
65       year => 2004,
66       month => 8,
67       day => 21,
68       hour => 14,
69       minute => 36,
70       second => 48,
71       nanosecond => 500000000,
72     }],
73     ['SMALLDATETIME', # minute precision
74      'small_dt',
75      {
76       year => 2004,
77       month => 8,
78       day => 21,
79       hour => 14,
80       minute => 36,
81     }],
82   );
83
84   for my $dt_type (@dt_types) {
85     my ($type, $col, $sample_dt) = @$dt_type;
86
87     eval { $schema->storage->dbh->do("DROP TABLE track") };
88     $schema->storage->dbh->do(<<"SQL");
89 CREATE TABLE track (
90  trackid INT IDENTITY PRIMARY KEY,
91  cd INT,
92  position INT,
93  $col $type,
94 )
95 SQL
96     ok(my $dt = DateTime->new($sample_dt));
97
98     my $row;
99     ok( $row = $schema->resultset('Track')->create({
100           $col => $dt,
101           cd => 1,
102         }));
103     ok( $row = $schema->resultset('Track')
104       ->search({ trackid => $row->trackid }, { select => [$col] })
105       ->first
106     );
107     is( $row->$col, $dt, "$type roundtrip" );
108
109     cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond},
110       'DateTime fractional portion roundtrip' )
111       if exists $sample_dt->{nanosecond};
112   }
113 }
114
115 done_testing;
116
117 # clean up our mess
118 sub cleanup {
119   if (my $dbh = eval { $schema->storage->dbh }) {
120     $dbh->do('DROP TABLE track');
121   }
122 }