datetime millisecond precision for MSSQL
[dbsrgits/DBIx-Class.git] / t / inflate / datetime_sybase.t
CommitLineData
d867eeda 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6use lib qw(t/lib);
7use DBICTest;
8
9my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
10
11if (not ($dsn && $user)) {
12 plan skip_all =>
13 'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test' .
14 "\nWarning: This test drops and creates a table called 'track'";
15} else {
16 eval "use DateTime; use DateTime::Format::Sybase;";
17 if ($@) {
18 plan skip_all => 'needs DateTime and DateTime::Format::Sybase for testing';
19 }
d867eeda 20}
21
22my @storage_types = (
95787afe 23 'DBI::Sybase::ASE',
24 'DBI::Sybase::ASE::NoBindVars',
d867eeda 25);
26my $schema;
27
28for my $storage_type (@storage_types) {
29 $schema = DBICTest::Schema->clone;
30
95787afe 31 unless ($storage_type eq 'DBI::Sybase::ASE') { # autodetect
d867eeda 32 $schema->storage_type("::$storage_type");
33 }
34 $schema->connection($dsn, $user, $pass, {
35 AutoCommit => 1,
36 on_connect_call => [ 'datetime_setup' ],
37 });
38
39 $schema->storage->ensure_connected;
40
41 isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
42
43# coltype, col, date
44 my @dt_types = (
45 ['DATETIME', 'last_updated_at', '2004-08-21T14:36:48.080Z'],
46# minute precision
47 ['SMALLDATETIME', 'small_dt', '2004-08-21T14:36:00.000Z'],
48 );
49
50 for my $dt_type (@dt_types) {
51 my ($type, $col, $sample_dt) = @$dt_type;
52
53 eval { $schema->storage->dbh->do("DROP TABLE track") };
54 $schema->storage->dbh->do(<<"SQL");
55CREATE TABLE track (
56 trackid INT IDENTITY PRIMARY KEY,
6469dabf 57 cd INT NULL,
58 position INT NULL,
59 $col $type NULL
d867eeda 60)
61SQL
62 ok(my $dt = DateTime::Format::Sybase->parse_datetime($sample_dt));
63
64 my $row;
65 ok( $row = $schema->resultset('Track')->create({
66 $col => $dt,
67 cd => 1,
68 }));
69 ok( $row = $schema->resultset('Track')
70 ->search({ trackid => $row->trackid }, { select => [$col] })
71 ->first
72 );
fb95dc4d 73 is( $row->$col, $dt, "$type roundtrip" );
74
75 is( $row->$col->nanosecond, $dt->nanosecond,
76 'fractional DateTime portion roundtrip' )
77 if $dt->nanosecond > 0;
d867eeda 78 }
6469dabf 79
80 # test a computed datetime column
81 eval { $schema->storage->dbh->do("DROP TABLE track") };
82 $schema->storage->dbh->do(<<"SQL");
83CREATE TABLE track (
84 trackid INT IDENTITY PRIMARY KEY,
85 cd INT NULL,
86 position INT NULL,
87 title VARCHAR(100) NULL,
88 last_updated_on DATETIME NULL,
89 last_updated_at AS getdate(),
90 small_dt SMALLDATETIME NULL
91)
92SQL
93
94 my $now = DateTime->now;
95 sleep 1;
96 my $new_row = $schema->resultset('Track')->create({});
97 $new_row->discard_changes;
98
99 lives_and {
100 cmp_ok (($new_row->last_updated_at - $now)->seconds, '>=', 1)
101 } 'getdate() computed column works';
d867eeda 102}
103
6469dabf 104done_testing;
105
d867eeda 106# clean up our mess
107END {
108 if (my $dbh = eval { $schema->storage->_dbh }) {
109 $dbh->do('DROP TABLE track');
110 }
111}