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