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