Fix incorrect comparison
[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 for my $connect_info (@connect_info) {
40   my ($dsn, $user, $pass) = @$connect_info;
41
42   next unless $dsn;
43
44   $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
45     on_connect_call => 'datetime_setup'
46   });
47
48   my $guard = Scope::Guard->new(\&cleanup);
49
50 # coltype, column, datehash
51   my @dt_types = (
52     ['DATETIME',
53      'last_updated_at',
54      {
55       year => 2004,
56       month => 8,
57       day => 21,
58       hour => 14,
59       minute => 36,
60       second => 48,
61       nanosecond => 500000000,
62     }],
63     ['SMALLDATETIME', # minute precision
64      'small_dt',
65      {
66       year => 2004,
67       month => 8,
68       day => 21,
69       hour => 14,
70       minute => 36,
71     }],
72   );
73
74   for my $dt_type (@dt_types) {
75     my ($type, $col, $sample_dt) = @$dt_type;
76
77     eval { $schema->storage->dbh->do("DROP TABLE track") };
78     $schema->storage->dbh->do(<<"SQL");
79 CREATE TABLE track (
80  trackid INT IDENTITY PRIMARY KEY,
81  cd INT,
82  position INT,
83  $col $type,
84 )
85 SQL
86     ok(my $dt = DateTime->new($sample_dt));
87
88     my $row;
89     ok( $row = $schema->resultset('Track')->create({
90           $col => $dt,
91           cd => 1,
92         }));
93     ok( $row = $schema->resultset('Track')
94       ->search({ trackid => $row->trackid }, { select => [$col] })
95       ->first
96     );
97     is( $row->$col, $dt, "$type roundtrip" );
98
99     cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond},
100       'DateTime fractional portion roundtrip' )
101       if exists $sample_dt->{nanosecond};
102   }
103 }
104
105 done_testing;
106
107 # clean up our mess
108 sub cleanup {
109   if (my $dbh = eval { $schema->storage->dbh }) {
110     $dbh->do('DROP TABLE track');
111   }
112 }