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