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