Multiple code/test/doc improvements for MSSQL over DBD::ADO
[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 Try::Tiny;
8 use lib qw(t/lib);
9 use DBICTest;
10
11 DBICTest::Schema->load_classes('EventSmallDT');
12
13 # use this if you keep a copy of DBD::Sybase linked to FreeTDS somewhere else
14 BEGIN {
15   if (my $lib_dirs = $ENV{DBICTEST_MSSQL_PERL5LIB}) {
16     unshift @INC, $_ for split /:/, $lib_dirs;
17   }
18 }
19
20 my ($dsn,  $user,  $pass)  = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PASS/};
21 my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_MSSQL_${_}" }      qw/DSN USER PASS/};
22 my ($dsn3, $user3, $pass3) = @ENV{map { "DBICTEST_MSSQL_ADO_${_}" }  qw/DSN USER PASS/};
23
24 if (not ($dsn || $dsn2 || $dsn3)) {
25   plan skip_all =>
26     'Set $ENV{DBICTEST_MSSQL_ODBC_DSN} and/or $ENV{DBICTEST_MSSQL_DSN} and/or '
27     .'$ENV{DBICTEST_MSSQL_ADO_DSN} _USER and _PASS to run this test' .
28     "\nWarning: This test drops and creates tables called 'event_small_dt' and"
29     ." 'track'.";
30 }
31
32 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_dt')
33   unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_dt');
34
35 my @connect_info = (
36   [ $dsn,  $user,  $pass ],
37   [ $dsn2, $user2, $pass2 ],
38   [ $dsn3, $user3, $pass3 ],
39 );
40
41 my $schema;
42
43 SKIP:
44 for my $connect_info (@connect_info) {
45   my ($dsn, $user, $pass) = @$connect_info;
46
47   next unless $dsn;
48
49   $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
50     on_connect_call => 'datetime_setup'
51   });
52
53   {
54     my $w;
55     local $SIG{__WARN__} = sub { $w = shift };
56     $schema->storage->ensure_connected;
57     if ($w =~ /Your DBD::Sybase is too old to support DBIx::Class::InflateColumn::DateTime/) {
58       skip "Skipping tests on old DBD::Sybase " . DBD::Sybase->VERSION, 1;
59     }
60   }
61
62   my $guard = Scope::Guard->new(\&cleanup);
63
64   # $^W because DBD::ADO is a piece of crap
65   try { local $^W = 0; $schema->storage->dbh->do("DROP TABLE track") };
66   $schema->storage->dbh->do(<<"SQL");
67 CREATE TABLE track (
68  trackid INT IDENTITY PRIMARY KEY,
69  cd INT,
70  position INT,
71  last_updated_at DATETIME,
72 )
73 SQL
74   try { local $^W = 0; $schema->storage->dbh->do("DROP TABLE event_small_dt") };
75   $schema->storage->dbh->do(<<"SQL");
76 CREATE TABLE event_small_dt (
77  id INT IDENTITY PRIMARY KEY,
78  small_dt SMALLDATETIME,
79 )
80 SQL
81
82 # coltype, column, source, pk, create_extra, datehash
83   my @dt_types = (
84     ['DATETIME',
85      'last_updated_at',
86      'Track',
87      'trackid',
88      { cd => 1 },
89      {
90       year => 2004,
91       month => 8,
92       day => 21,
93       hour => 14,
94       minute => 36,
95       second => 48,
96       nanosecond => 500000000,
97     }],
98     ['SMALLDATETIME', # minute precision
99      'small_dt',
100      'EventSmallDT',
101      'id',
102      {},
103      {
104       year => 2004,
105       month => 8,
106       day => 21,
107       hour => 14,
108       minute => 36,
109     }],
110   );
111
112   for my $dt_type (@dt_types) {
113     my ($type, $col, $source, $pk, $create_extra, $sample_dt) = @$dt_type;
114
115     delete $sample_dt->{nanosecond} if $dsn =~ /:ADO:/;
116
117     ok(my $dt = DateTime->new($sample_dt));
118
119     my $row;
120     ok( $row = $schema->resultset($source)->create({
121           $col => $dt,
122           %$create_extra,
123         }));
124     ok( $row = $schema->resultset($source)
125       ->search({ $pk => $row->$pk }, { select => [$col] })
126       ->first
127     );
128     is( $row->$col, $dt, "$type roundtrip" );
129
130     cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond},
131       'DateTime fractional portion roundtrip' )
132       if exists $sample_dt->{nanosecond};
133   }
134 }
135
136 done_testing;
137
138 # clean up our mess
139 sub cleanup {
140   if (my $dbh = eval { $schema->storage->dbh }) {
141     $dbh->do('DROP TABLE track');
142     $dbh->do('DROP TABLE event_small_dt');
143   }
144 }