Take a copy instead of weakening in 5.8 leak workaround
[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
2c2bc4e5 40require DBICTest::Schema;
199fbc45 41DBICTest::Schema->load_classes('EventSmallDT');
68de9438 42
fb95dc4d 43my @connect_info = (
44 [ $dsn, $user, $pass ],
45 [ $dsn2, $user2, $pass2 ],
56dca25f 46 [ $dsn3, $user3, $pass3 ],
fb95dc4d 47);
48
49my $schema;
50
dd572bad 51SKIP:
fb95dc4d 52for my $connect_info (@connect_info) {
53 my ($dsn, $user, $pass) = @$connect_info;
54
55 next unless $dsn;
5a77aa8b 56
2c2bc4e5 57 $schema = DBICTest->connect_schema($dsn, $user, $pass, {
fb95dc4d 58 on_connect_call => 'datetime_setup'
59 });
60
dd572bad 61 {
62 my $w;
63 local $SIG{__WARN__} = sub { $w = shift };
64 $schema->storage->ensure_connected;
65 if ($w =~ /Your DBD::Sybase is too old to support DBIx::Class::InflateColumn::DateTime/) {
66 skip "Skipping tests on old DBD::Sybase " . DBD::Sybase->VERSION, 1;
67 }
68 }
69
65d35121 70 my $guard = Scope::Guard->new(sub{ cleanup($schema) });
5a77aa8b 71
56dca25f 72 # $^W because DBD::ADO is a piece of crap
73 try { local $^W = 0; $schema->storage->dbh->do("DROP TABLE track") };
3d98c75e 74 $schema->storage->dbh->do(<<"SQL");
75CREATE TABLE track (
76 trackid INT IDENTITY PRIMARY KEY,
77 cd INT,
78 position INT,
79 last_updated_at DATETIME,
80)
81SQL
56dca25f 82 try { local $^W = 0; $schema->storage->dbh->do("DROP TABLE event_small_dt") };
3d98c75e 83 $schema->storage->dbh->do(<<"SQL");
84CREATE TABLE event_small_dt (
85 id INT IDENTITY PRIMARY KEY,
86 small_dt SMALLDATETIME,
87)
88SQL
124162a0 89 try { local $^W = 0; $schema->storage->dbh->do("DROP TABLE event") };
90 $schema->storage->dbh->do(<<"SQL");
91CREATE TABLE event (
92 id int IDENTITY(1,1) NOT NULL,
93 starts_at smalldatetime NULL,
94 created_on datetime NULL,
95 varchar_date varchar(20) NULL,
96 varchar_datetime varchar(20) NULL,
97 skip_inflation datetime NULL,
98 ts_without_tz datetime NULL
99)
100SQL
3d98c75e 101
102# coltype, column, source, pk, create_extra, datehash
fb95dc4d 103 my @dt_types = (
104 ['DATETIME',
105 'last_updated_at',
3d98c75e 106 'Track',
107 'trackid',
108 { cd => 1 },
fb95dc4d 109 {
110 year => 2004,
111 month => 8,
112 day => 21,
113 hour => 14,
114 minute => 36,
115 second => 48,
116 nanosecond => 500000000,
117 }],
118 ['SMALLDATETIME', # minute precision
119 'small_dt',
3d98c75e 120 'EventSmallDT',
121 'id',
122 {},
fb95dc4d 123 {
124 year => 2004,
125 month => 8,
126 day => 21,
127 hour => 14,
128 minute => 36,
129 }],
130 );
5a77aa8b 131
fb95dc4d 132 for my $dt_type (@dt_types) {
3d98c75e 133 my ($type, $col, $source, $pk, $create_extra, $sample_dt) = @$dt_type;
5a77aa8b 134
56dca25f 135 delete $sample_dt->{nanosecond} if $dsn =~ /:ADO:/;
136
fb95dc4d 137 ok(my $dt = DateTime->new($sample_dt));
138
139 my $row;
3d98c75e 140 ok( $row = $schema->resultset($source)->create({
fb95dc4d 141 $col => $dt,
3d98c75e 142 %$create_extra,
fb95dc4d 143 }));
3d98c75e 144 ok( $row = $schema->resultset($source)
145 ->search({ $pk => $row->$pk }, { select => [$col] })
fb95dc4d 146 ->first
147 );
148 is( $row->$col, $dt, "$type roundtrip" );
0f72fb47 149
150 cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond},
fb95dc4d 151 'DateTime fractional portion roundtrip' )
152 if exists $sample_dt->{nanosecond};
153 }
124162a0 154
155 # Check for bulk insert SQL_DATE funtimes when using DBD::ODBC and sqlncli
156 # dbi:ODBC:driver=SQL Server Native Client 10.0;server=10.6.0.9;database=odbctest;
157 lives_ok {
158 $schema->resultset('Event')->populate([{
159 id => 1,
160 starts_at => undef,
161 },{
162 id => 2,
163 starts_at => '2011-03-22',
164 }])
165 } 'populate with datetime does not throw';
166 ok ( my $row = $schema->resultset('Event')->find(2), 'SQL_DATE bulk insert check' );
5a77aa8b 167}
168
124162a0 169
fb95dc4d 170done_testing;
171
5a77aa8b 172# clean up our mess
fb95dc4d 173sub cleanup {
65d35121 174 my $schema = shift;
fb95dc4d 175 if (my $dbh = eval { $schema->storage->dbh }) {
5a77aa8b 176 $dbh->do('DROP TABLE track');
3d98c75e 177 $dbh->do('DROP TABLE event_small_dt');
124162a0 178 $dbh->do('DROP TABLE event');
5a77aa8b 179 }
180}