Massive cleanup of DateTime test dependencies, other interim
[dbsrgits/DBIx-Class.git] / t / inflate / datetime_mssql.t
CommitLineData
5a77aa8b 1use strict;
dd572bad 2use warnings;
5a77aa8b 3
4use Test::More;
5use Test::Exception;
fb95dc4d 6use Scope::Guard ();
5a77aa8b 7use lib qw(t/lib);
8use DBICTest;
9
fb95dc4d 10# use this if you keep a copy of DBD::Sybase linked to FreeTDS somewhere else
11BEGIN {
12 if (my $lib_dirs = $ENV{DBICTEST_MSSQL_PERL5LIB}) {
13 unshift @INC, $_ for split /:/, $lib_dirs;
14 }
15}
5a77aa8b 16
fb95dc4d 17my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PASS/};
18my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_MSSQL_${_}" } qw/DSN USER PASS/};
19
20if (not ($dsn || $dsn2)) {
5a77aa8b 21 plan skip_all =>
fb95dc4d 22 'Set $ENV{DBICTEST_MSSQL_ODBC_DSN} and/or $ENV{DBICTEST_MSSQL_DSN} _USER '
23 .'and _PASS to run this test' .
5a77aa8b 24 "\nWarning: This test drops and creates a table called 'track'";
5a77aa8b 25}
26
68de9438 27plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_dt')
28 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_dt');
29
fb95dc4d 30my @connect_info = (
31 [ $dsn, $user, $pass ],
32 [ $dsn2, $user2, $pass2 ],
33);
34
35my $schema;
36
dd572bad 37SKIP:
fb95dc4d 38for my $connect_info (@connect_info) {
39 my ($dsn, $user, $pass) = @$connect_info;
40
41 next unless $dsn;
5a77aa8b 42
fb95dc4d 43 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
44 on_connect_call => 'datetime_setup'
45 });
46
dd572bad 47 {
48 my $w;
49 local $SIG{__WARN__} = sub { $w = shift };
50 $schema->storage->ensure_connected;
51 if ($w =~ /Your DBD::Sybase is too old to support DBIx::Class::InflateColumn::DateTime/) {
52 skip "Skipping tests on old DBD::Sybase " . DBD::Sybase->VERSION, 1;
53 }
54 }
55
fb95dc4d 56 my $guard = Scope::Guard->new(\&cleanup);
5a77aa8b 57
b7ad9162 58# coltype, column, datehash
fb95dc4d 59 my @dt_types = (
60 ['DATETIME',
61 'last_updated_at',
62 {
63 year => 2004,
64 month => 8,
65 day => 21,
66 hour => 14,
67 minute => 36,
68 second => 48,
69 nanosecond => 500000000,
70 }],
71 ['SMALLDATETIME', # minute precision
72 'small_dt',
73 {
74 year => 2004,
75 month => 8,
76 day => 21,
77 hour => 14,
78 minute => 36,
79 }],
80 );
5a77aa8b 81
fb95dc4d 82 for my $dt_type (@dt_types) {
83 my ($type, $col, $sample_dt) = @$dt_type;
5a77aa8b 84
fb95dc4d 85 eval { $schema->storage->dbh->do("DROP TABLE track") };
86 $schema->storage->dbh->do(<<"SQL");
5a77aa8b 87CREATE TABLE track (
88 trackid INT IDENTITY PRIMARY KEY,
89 cd INT,
90 position INT,
b7ad9162 91 $col $type,
5a77aa8b 92)
93SQL
fb95dc4d 94 ok(my $dt = DateTime->new($sample_dt));
95
96 my $row;
97 ok( $row = $schema->resultset('Track')->create({
98 $col => $dt,
99 cd => 1,
100 }));
101 ok( $row = $schema->resultset('Track')
102 ->search({ trackid => $row->trackid }, { select => [$col] })
103 ->first
104 );
105 is( $row->$col, $dt, "$type roundtrip" );
0f72fb47 106
107 cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond},
fb95dc4d 108 'DateTime fractional portion roundtrip' )
109 if exists $sample_dt->{nanosecond};
110 }
5a77aa8b 111}
112
fb95dc4d 113done_testing;
114
5a77aa8b 115# clean up our mess
fb95dc4d 116sub cleanup {
117 if (my $dbh = eval { $schema->storage->dbh }) {
5a77aa8b 118 $dbh->do('DROP TABLE track');
119 }
120}