datetime millisecond precision for MSSQL
[dbsrgits/DBIx-Class.git] / t / inflate / datetime_mssql.t
CommitLineData
5a77aa8b 1use strict;
2use warnings;
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
39for my $connect_info (@connect_info) {
40 my ($dsn, $user, $pass) = @$connect_info;
41
42 next unless $dsn;
5a77aa8b 43
fb95dc4d 44 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
45 on_connect_call => 'datetime_setup'
46 });
47
48 my $guard = Scope::Guard->new(\&cleanup);
5a77aa8b 49
b7ad9162 50# coltype, column, datehash
fb95dc4d 51 my @dt_types = (
52 ['DATETIME',
53 'last_updated_at',
54 {
55 year => 2004,
56 month => 8,
57 day => 21,
58 hour => 14,
59 minute => 36,
60 second => 48,
61 nanosecond => 500000000,
62 }],
63 ['SMALLDATETIME', # minute precision
64 'small_dt',
65 {
66 year => 2004,
67 month => 8,
68 day => 21,
69 hour => 14,
70 minute => 36,
71 }],
72 );
5a77aa8b 73
fb95dc4d 74 for my $dt_type (@dt_types) {
75 my ($type, $col, $sample_dt) = @$dt_type;
5a77aa8b 76
fb95dc4d 77 eval { $schema->storage->dbh->do("DROP TABLE track") };
78 $schema->storage->dbh->do(<<"SQL");
5a77aa8b 79CREATE TABLE track (
80 trackid INT IDENTITY PRIMARY KEY,
81 cd INT,
82 position INT,
b7ad9162 83 $col $type,
5a77aa8b 84)
85SQL
fb95dc4d 86 ok(my $dt = DateTime->new($sample_dt));
87
88 my $row;
89 ok( $row = $schema->resultset('Track')->create({
90 $col => $dt,
91 cd => 1,
92 }));
93 ok( $row = $schema->resultset('Track')
94 ->search({ trackid => $row->trackid }, { select => [$col] })
95 ->first
96 );
97 is( $row->$col, $dt, "$type roundtrip" );
98
99 is( $row->$col->nanosecond, $sample_dt->{nanosecond},
100 'DateTime fractional portion roundtrip' )
101 if exists $sample_dt->{nanosecond};
102 }
5a77aa8b 103}
104
fb95dc4d 105done_testing;
106
5a77aa8b 107# clean up our mess
fb95dc4d 108sub cleanup {
109 if (my $dbh = eval { $schema->storage->dbh }) {
5a77aa8b 110 $dbh->do('DROP TABLE track');
111 }
112}