Enable pg test disabled god knows why, minor cleanup.
[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'";
25} else {
26 eval "use DateTime; use DateTime::Format::Strptime;";
27 if ($@) {
28 plan skip_all => 'needs DateTime and DateTime::Format::Strptime for testing';
29 }
5a77aa8b 30}
31
fb95dc4d 32my @connect_info = (
33 [ $dsn, $user, $pass ],
34 [ $dsn2, $user2, $pass2 ],
35);
36
37my $schema;
38
dd572bad 39SKIP:
fb95dc4d 40for my $connect_info (@connect_info) {
41 my ($dsn, $user, $pass) = @$connect_info;
42
43 next unless $dsn;
5a77aa8b 44
fb95dc4d 45 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
46 on_connect_call => 'datetime_setup'
47 });
48
dd572bad 49 {
50 my $w;
51 local $SIG{__WARN__} = sub { $w = shift };
52 $schema->storage->ensure_connected;
53 if ($w =~ /Your DBD::Sybase is too old to support DBIx::Class::InflateColumn::DateTime/) {
54 skip "Skipping tests on old DBD::Sybase " . DBD::Sybase->VERSION, 1;
55 }
56 }
57
fb95dc4d 58 my $guard = Scope::Guard->new(\&cleanup);
5a77aa8b 59
b7ad9162 60# coltype, column, datehash
fb95dc4d 61 my @dt_types = (
62 ['DATETIME',
63 'last_updated_at',
64 {
65 year => 2004,
66 month => 8,
67 day => 21,
68 hour => 14,
69 minute => 36,
70 second => 48,
71 nanosecond => 500000000,
72 }],
73 ['SMALLDATETIME', # minute precision
74 'small_dt',
75 {
76 year => 2004,
77 month => 8,
78 day => 21,
79 hour => 14,
80 minute => 36,
81 }],
82 );
5a77aa8b 83
fb95dc4d 84 for my $dt_type (@dt_types) {
85 my ($type, $col, $sample_dt) = @$dt_type;
5a77aa8b 86
fb95dc4d 87 eval { $schema->storage->dbh->do("DROP TABLE track") };
88 $schema->storage->dbh->do(<<"SQL");
5a77aa8b 89CREATE TABLE track (
90 trackid INT IDENTITY PRIMARY KEY,
91 cd INT,
92 position INT,
b7ad9162 93 $col $type,
5a77aa8b 94)
95SQL
fb95dc4d 96 ok(my $dt = DateTime->new($sample_dt));
97
98 my $row;
99 ok( $row = $schema->resultset('Track')->create({
100 $col => $dt,
101 cd => 1,
102 }));
103 ok( $row = $schema->resultset('Track')
104 ->search({ trackid => $row->trackid }, { select => [$col] })
105 ->first
106 );
107 is( $row->$col, $dt, "$type roundtrip" );
0f72fb47 108
109 cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond},
fb95dc4d 110 'DateTime fractional portion roundtrip' )
111 if exists $sample_dt->{nanosecond};
112 }
5a77aa8b 113}
114
fb95dc4d 115done_testing;
116
5a77aa8b 117# clean up our mess
fb95dc4d 118sub cleanup {
119 if (my $dbh = eval { $schema->storage->dbh }) {
5a77aa8b 120 $dbh->do('DROP TABLE track');
121 }
122}