bc85fdc1796b86bd2b7aaec8e07a6a71a4d4153c
[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 lib qw(t/lib);
7 use DBICTest;
8
9 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PASS/};
10
11 if (not ($dsn && $user)) {
12   plan skip_all =>
13     'Set $ENV{DBICTEST_MSSQL_ODBC_DSN}, _USER and _PASS to run this test' .
14     "\nWarning: This test drops and creates a table called 'track'";
15 } else {
16   eval "use DateTime; use DateTime::Format::Strptime;";
17   if ($@) {
18     plan skip_all => 'needs DateTime and DateTime::Format::Strptime for testing';
19   }
20   else {
21     plan tests => 4 * 2; # (tests * dt_types)
22   }
23 }
24
25 my $schema = DBICTest::Schema->clone;
26
27 $schema->connection($dsn, $user, $pass);
28 $schema->storage->ensure_connected;
29
30 # coltype, column, datehash
31 my @dt_types = (
32   ['DATETIME',
33    'last_updated_at',
34    {
35     year => 2004,
36     month => 8,
37     day => 21,
38     hour => 14,
39     minute => 36,
40     second => 48,
41     nanosecond => 500000000,
42   }],
43   ['SMALLDATETIME', # minute precision
44    'small_dt',
45    {
46     year => 2004,
47     month => 8,
48     day => 21,
49     hour => 14,
50     minute => 36,
51   }],
52 );
53
54 for my $dt_type (@dt_types) {
55   my ($type, $col, $sample_dt) = @$dt_type;
56
57   eval { $schema->storage->dbh->do("DROP TABLE track") };
58   $schema->storage->dbh->do(<<"SQL");
59 CREATE TABLE track (
60  trackid INT IDENTITY PRIMARY KEY,
61  cd INT,
62  position INT,
63  $col $type,
64 )
65 SQL
66   ok(my $dt = DateTime->new($sample_dt));
67
68   my $row;
69   ok( $row = $schema->resultset('Track')->create({
70         $col => $dt,
71         cd => 1,
72       }));
73   ok( $row = $schema->resultset('Track')
74     ->search({ trackid => $row->trackid }, { select => [$col] })
75     ->first
76   );
77   is( $row->$col, $dt, 'DateTime roundtrip' );
78 }
79
80 # clean up our mess
81 END {
82   if (my $dbh = eval { $schema->storage->_dbh }) {
83     $dbh->do('DROP TABLE track');
84   }
85 }