Remove small_datetime from the main schema - it is not a standard datatype
[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
23 if (not ($dsn || $dsn2)) {
24   plan skip_all =>
25     'Set $ENV{DBICTEST_MSSQL_ODBC_DSN} and/or $ENV{DBICTEST_MSSQL_DSN} _USER '
26     .'and _PASS to run this test' .
27     "\nWarning: This test drops and creates a table called 'small_dt'";
28 }
29
30 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_dt')
31   unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_dt');
32
33 my @connect_info = (
34   [ $dsn,  $user,  $pass ],
35   [ $dsn2, $user2, $pass2 ],
36 );
37
38 my $schema;
39
40 SKIP:
41 for my $connect_info (@connect_info) {
42   my ($dsn, $user, $pass) = @$connect_info;
43
44   next unless $dsn;
45
46   $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
47     on_connect_call => 'datetime_setup'
48   });
49
50   {
51     my $w;
52     local $SIG{__WARN__} = sub { $w = shift };
53     $schema->storage->ensure_connected;
54     if ($w =~ /Your DBD::Sybase is too old to support DBIx::Class::InflateColumn::DateTime/) {
55       skip "Skipping tests on old DBD::Sybase " . DBD::Sybase->VERSION, 1;
56     }
57   }
58
59   my $guard = Scope::Guard->new(\&cleanup);
60
61   try { $schema->storage->dbh->do("DROP TABLE track") };
62   $schema->storage->dbh->do(<<"SQL");
63 CREATE TABLE track (
64  trackid INT IDENTITY PRIMARY KEY,
65  cd INT,
66  position INT,
67  last_updated_at DATETIME,
68 )
69 SQL
70   try { $schema->storage->dbh->do("DROP TABLE event_small_dt") };
71   $schema->storage->dbh->do(<<"SQL");
72 CREATE TABLE event_small_dt (
73  id INT IDENTITY PRIMARY KEY,
74  small_dt SMALLDATETIME,
75 )
76 SQL
77
78 # coltype, column, source, pk, create_extra, datehash
79   my @dt_types = (
80     ['DATETIME',
81      'last_updated_at',
82      'Track',
83      'trackid',
84      { cd => 1 },
85      {
86       year => 2004,
87       month => 8,
88       day => 21,
89       hour => 14,
90       minute => 36,
91       second => 48,
92       nanosecond => 500000000,
93     }],
94     ['SMALLDATETIME', # minute precision
95      'small_dt',
96      'EventSmallDT',
97      'id',
98      {},
99      {
100       year => 2004,
101       month => 8,
102       day => 21,
103       hour => 14,
104       minute => 36,
105     }],
106   );
107
108   for my $dt_type (@dt_types) {
109     my ($type, $col, $source, $pk, $create_extra, $sample_dt) = @$dt_type;
110
111     ok(my $dt = DateTime->new($sample_dt));
112
113     my $row;
114     ok( $row = $schema->resultset($source)->create({
115           $col => $dt,
116           %$create_extra,
117         }));
118     ok( $row = $schema->resultset($source)
119       ->search({ $pk => $row->$pk }, { select => [$col] })
120       ->first
121     );
122     is( $row->$col, $dt, "$type roundtrip" );
123
124     cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond},
125       'DateTime fractional portion roundtrip' )
126       if exists $sample_dt->{nanosecond};
127   }
128 }
129
130 done_testing;
131
132 # clean up our mess
133 sub cleanup {
134   if (my $dbh = eval { $schema->storage->dbh }) {
135     $dbh->do('DROP TABLE track');
136     $dbh->do('DROP TABLE event_small_dt');
137   }
138 }