e65a994ea07f378ed0d36950a43835795beab2ba
[dbsrgits/DBIx-Class.git] / t / icdt / engine_specific / mssql.t
1 use DBIx::Class::Optional::Dependencies -skip_all_without => qw( ic_dt _rdbms_mssql_common );
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8 use Try::Tiny;
9 use DBIx::Class::_Util 'scope_guard';
10 use lib qw(t/lib);
11 use DBICTest;
12
13 my @tdeps = qw( test_rdbms_mssql_odbc test_rdbms_mssql_sybase test_rdbms_mssql_ado );
14 plan 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
22 my ($dsn,  $user,  $pass)  = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PASS/};
23 my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_MSSQL_${_}" }      qw/DSN USER PASS/};
24 my ($dsn3, $user3, $pass3) = @ENV{map { "DBICTEST_MSSQL_ADO_${_}" }  qw/DSN USER PASS/};
25
26 DBICTest::Schema->load_classes('EventSmallDT');
27
28 my @connect_info = (
29   [ $dsn,  $user,  $pass ],
30   [ $dsn2, $user2, $pass2 ],
31   [ $dsn3, $user3, $pass3 ],
32 );
33
34 my $schema;
35
36 SKIP:
37 for my $connect_info (@connect_info) {
38   my ($dsn, $user, $pass) = @$connect_info;
39
40   next unless $dsn;
41
42   $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
43     on_connect_call => 'datetime_setup'
44   });
45
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
55   my $guard = scope_guard { cleanup($schema) };
56
57   # $^W because DBD::ADO is a piece of crap
58   try { local $^W = 0; $schema->storage->dbh->do("DROP TABLE track") };
59   $schema->storage->dbh->do(<<"SQL");
60 CREATE TABLE track (
61  trackid INT IDENTITY PRIMARY KEY,
62  cd INT,
63  position INT,
64  last_updated_at DATETIME,
65 )
66 SQL
67   try { local $^W = 0; $schema->storage->dbh->do("DROP TABLE event_small_dt") };
68   $schema->storage->dbh->do(<<"SQL");
69 CREATE TABLE event_small_dt (
70  id INT IDENTITY PRIMARY KEY,
71  small_dt SMALLDATETIME,
72 )
73 SQL
74   try { local $^W = 0; $schema->storage->dbh->do("DROP TABLE event") };
75   $schema->storage->dbh->do(<<"SQL");
76 CREATE 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 )
85 SQL
86
87 # coltype, column, source, pk, create_extra, datehash
88   my @dt_types = (
89     ['DATETIME',
90      'last_updated_at',
91      'Track',
92      'trackid',
93      { cd => 1 },
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',
105      'EventSmallDT',
106      'id',
107      {},
108      {
109       year => 2004,
110       month => 8,
111       day => 21,
112       hour => 14,
113       minute => 36,
114     }],
115   );
116
117   for my $dt_type (@dt_types) {
118     my ($type, $col, $source, $pk, $create_extra, $sample_dt) = @$dt_type;
119
120     delete $sample_dt->{nanosecond} if $dsn =~ /:ADO:/;
121
122     ok(my $dt = DateTime->new($sample_dt));
123
124     my $row;
125     ok( $row = $schema->resultset($source)->create({
126           $col => $dt,
127           %$create_extra,
128         }));
129     ok( $row = $schema->resultset($source)
130       ->search({ $pk => $row->$pk }, { select => [$col] })
131       ->first
132     );
133     is( $row->$col, $dt, "$type roundtrip" );
134
135     cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond},
136       'DateTime fractional portion roundtrip' )
137       if exists $sample_dt->{nanosecond};
138   }
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' );
152 }
153
154
155 done_testing;
156
157 # clean up our mess
158 sub cleanup {
159   my $schema = shift;
160   if (my $dbh = eval { $schema->storage->dbh }) {
161     $dbh->do('DROP TABLE track');
162     $dbh->do('DROP TABLE event_small_dt');
163     $dbh->do('DROP TABLE event');
164   }
165 }