Rebase Oracle datetime inflation test on top of DBICTest::Schema::Event
[dbsrgits/DBIx-Class.git] / t / inflate / datetime_mssql.t
CommitLineData
5a77aa8b 1use strict;
dd572bad 2use warnings;
5a77aa8b 3
4use Test::More;
124162a0 5use Test::Exception;
fb95dc4d 6use Scope::Guard ();
3d98c75e 7use Try::Tiny;
199fbc45 8use DBIx::Class::Optional::Dependencies ();
5a77aa8b 9use lib qw(t/lib);
10use DBICTest;
11
199fbc45 12my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PASS/};
13my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_MSSQL_${_}" } qw/DSN USER PASS/};
14my ($dsn3, $user3, $pass3) = @ENV{map { "DBICTEST_MSSQL_ADO_${_}" } qw/DSN USER PASS/};
15
16plan skip_all => 'Test needs ' .
17 (join ' and ', map { $_ ? $_ : () }
18 DBIx::Class::Optional::Dependencies->req_missing_for('test_dt'),
19 (join ' or ', map { $_ ? $_ : () }
20 DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_mssql_odbc'),
21 DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_mssql_sybase'),
22 DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_mssql_ado')))
23 unless
24 DBIx::Class::Optional::Dependencies->req_ok_for ('test_dt') && (
25 $dsn && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_mssql_odbc')
26 or
27 $dsn2 && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_mssql_sybase')
28 or
29 $dsn3 && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_mssql_ado'))
30 or (not $dsn || $dsn2 || $dsn3);
3d98c75e 31
56dca25f 32if (not ($dsn || $dsn2 || $dsn3)) {
5a77aa8b 33 plan skip_all =>
56dca25f 34 'Set $ENV{DBICTEST_MSSQL_ODBC_DSN} and/or $ENV{DBICTEST_MSSQL_DSN} and/or '
35 .'$ENV{DBICTEST_MSSQL_ADO_DSN} _USER and _PASS to run this test' .
36 "\nWarning: This test drops and creates tables called 'event_small_dt' and"
37 ." 'track'.";
5a77aa8b 38}
39
199fbc45 40DBICTest::Schema->load_classes('EventSmallDT');
68de9438 41
fb95dc4d 42my @connect_info = (
43 [ $dsn, $user, $pass ],
44 [ $dsn2, $user2, $pass2 ],
56dca25f 45 [ $dsn3, $user3, $pass3 ],
fb95dc4d 46);
47
48my $schema;
49
dd572bad 50SKIP:
fb95dc4d 51for my $connect_info (@connect_info) {
52 my ($dsn, $user, $pass) = @$connect_info;
53
54 next unless $dsn;
5a77aa8b 55
fb95dc4d 56 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
57 on_connect_call => 'datetime_setup'
58 });
59
dd572bad 60 {
61 my $w;
62 local $SIG{__WARN__} = sub { $w = shift };
63 $schema->storage->ensure_connected;
64 if ($w =~ /Your DBD::Sybase is too old to support DBIx::Class::InflateColumn::DateTime/) {
65 skip "Skipping tests on old DBD::Sybase " . DBD::Sybase->VERSION, 1;
66 }
67 }
68
65d35121 69 my $guard = Scope::Guard->new(sub{ cleanup($schema) });
5a77aa8b 70
56dca25f 71 # $^W because DBD::ADO is a piece of crap
72 try { local $^W = 0; $schema->storage->dbh->do("DROP TABLE track") };
3d98c75e 73 $schema->storage->dbh->do(<<"SQL");
74CREATE TABLE track (
75 trackid INT IDENTITY PRIMARY KEY,
76 cd INT,
77 position INT,
78 last_updated_at DATETIME,
79)
80SQL
56dca25f 81 try { local $^W = 0; $schema->storage->dbh->do("DROP TABLE event_small_dt") };
3d98c75e 82 $schema->storage->dbh->do(<<"SQL");
83CREATE TABLE event_small_dt (
84 id INT IDENTITY PRIMARY KEY,
85 small_dt SMALLDATETIME,
86)
87SQL
124162a0 88 try { local $^W = 0; $schema->storage->dbh->do("DROP TABLE event") };
89 $schema->storage->dbh->do(<<"SQL");
90CREATE TABLE event (
91 id int IDENTITY(1,1) NOT NULL,
92 starts_at smalldatetime NULL,
93 created_on datetime NULL,
94 varchar_date varchar(20) NULL,
95 varchar_datetime varchar(20) NULL,
96 skip_inflation datetime NULL,
97 ts_without_tz datetime NULL
98)
99SQL
3d98c75e 100
101# coltype, column, source, pk, create_extra, datehash
fb95dc4d 102 my @dt_types = (
103 ['DATETIME',
104 'last_updated_at',
3d98c75e 105 'Track',
106 'trackid',
107 { cd => 1 },
fb95dc4d 108 {
109 year => 2004,
110 month => 8,
111 day => 21,
112 hour => 14,
113 minute => 36,
114 second => 48,
115 nanosecond => 500000000,
116 }],
117 ['SMALLDATETIME', # minute precision
118 'small_dt',
3d98c75e 119 'EventSmallDT',
120 'id',
121 {},
fb95dc4d 122 {
123 year => 2004,
124 month => 8,
125 day => 21,
126 hour => 14,
127 minute => 36,
128 }],
129 );
5a77aa8b 130
fb95dc4d 131 for my $dt_type (@dt_types) {
3d98c75e 132 my ($type, $col, $source, $pk, $create_extra, $sample_dt) = @$dt_type;
5a77aa8b 133
56dca25f 134 delete $sample_dt->{nanosecond} if $dsn =~ /:ADO:/;
135
fb95dc4d 136 ok(my $dt = DateTime->new($sample_dt));
137
138 my $row;
3d98c75e 139 ok( $row = $schema->resultset($source)->create({
fb95dc4d 140 $col => $dt,
3d98c75e 141 %$create_extra,
fb95dc4d 142 }));
3d98c75e 143 ok( $row = $schema->resultset($source)
144 ->search({ $pk => $row->$pk }, { select => [$col] })
fb95dc4d 145 ->first
146 );
147 is( $row->$col, $dt, "$type roundtrip" );
0f72fb47 148
149 cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond},
fb95dc4d 150 'DateTime fractional portion roundtrip' )
151 if exists $sample_dt->{nanosecond};
152 }
124162a0 153
154 # Check for bulk insert SQL_DATE funtimes when using DBD::ODBC and sqlncli
155 # dbi:ODBC:driver=SQL Server Native Client 10.0;server=10.6.0.9;database=odbctest;
156 lives_ok {
157 $schema->resultset('Event')->populate([{
158 id => 1,
159 starts_at => undef,
160 },{
161 id => 2,
162 starts_at => '2011-03-22',
163 }])
164 } 'populate with datetime does not throw';
165 ok ( my $row = $schema->resultset('Event')->find(2), 'SQL_DATE bulk insert check' );
5a77aa8b 166}
167
124162a0 168
fb95dc4d 169done_testing;
170
5a77aa8b 171# clean up our mess
fb95dc4d 172sub cleanup {
65d35121 173 my $schema = shift;
fb95dc4d 174 if (my $dbh = eval { $schema->storage->dbh }) {
5a77aa8b 175 $dbh->do('DROP TABLE track');
3d98c75e 176 $dbh->do('DROP TABLE event_small_dt');
124162a0 177 $dbh->do('DROP TABLE event');
5a77aa8b 178 }
179}