Commit | Line | Data |
c0329273 |
1 | BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) } |
54a9a088 |
2 | use DBIx::Class::Optional::Dependencies -skip_all_without => qw( ic_dt _rdbms_mssql_common ); |
2baba3d9 |
3 | |
5a77aa8b |
4 | use strict; |
dd572bad |
5 | use warnings; |
5a77aa8b |
6 | |
7 | use Test::More; |
124162a0 |
8 | use Test::Exception; |
3d98c75e |
9 | use Try::Tiny; |
bbf6a9a5 |
10 | use DBIx::Class::_Util 'scope_guard'; |
c0329273 |
11 | |
5a77aa8b |
12 | use DBICTest; |
13 | |
2baba3d9 |
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 | |
199fbc45 |
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 | |
199fbc45 |
27 | DBICTest::Schema->load_classes('EventSmallDT'); |
68de9438 |
28 | |
fb95dc4d |
29 | my @connect_info = ( |
30 | [ $dsn, $user, $pass ], |
31 | [ $dsn2, $user2, $pass2 ], |
56dca25f |
32 | [ $dsn3, $user3, $pass3 ], |
fb95dc4d |
33 | ); |
34 | |
35 | my $schema; |
36 | |
dd572bad |
37 | SKIP: |
fb95dc4d |
38 | for my $connect_info (@connect_info) { |
39 | my ($dsn, $user, $pass) = @$connect_info; |
40 | |
41 | next unless $dsn; |
5a77aa8b |
42 | |
fb95dc4d |
43 | $schema = DBICTest::Schema->connect($dsn, $user, $pass, { |
44 | on_connect_call => 'datetime_setup' |
45 | }); |
46 | |
dd572bad |
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 | |
bbf6a9a5 |
56 | my $guard = scope_guard { cleanup($schema) }; |
5a77aa8b |
57 | |
56dca25f |
58 | # $^W because DBD::ADO is a piece of crap |
59 | try { local $^W = 0; $schema->storage->dbh->do("DROP TABLE track") }; |
3d98c75e |
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 |
56dca25f |
68 | try { local $^W = 0; $schema->storage->dbh->do("DROP TABLE event_small_dt") }; |
3d98c75e |
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 |
124162a0 |
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 |
3d98c75e |
87 | |
88 | # coltype, column, source, pk, create_extra, datehash |
fb95dc4d |
89 | my @dt_types = ( |
90 | ['DATETIME', |
91 | 'last_updated_at', |
3d98c75e |
92 | 'Track', |
93 | 'trackid', |
94 | { cd => 1 }, |
fb95dc4d |
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', |
3d98c75e |
106 | 'EventSmallDT', |
107 | 'id', |
108 | {}, |
fb95dc4d |
109 | { |
110 | year => 2004, |
111 | month => 8, |
112 | day => 21, |
113 | hour => 14, |
114 | minute => 36, |
115 | }], |
116 | ); |
5a77aa8b |
117 | |
fb95dc4d |
118 | for my $dt_type (@dt_types) { |
3d98c75e |
119 | my ($type, $col, $source, $pk, $create_extra, $sample_dt) = @$dt_type; |
5a77aa8b |
120 | |
56dca25f |
121 | delete $sample_dt->{nanosecond} if $dsn =~ /:ADO:/; |
122 | |
fb95dc4d |
123 | ok(my $dt = DateTime->new($sample_dt)); |
124 | |
125 | my $row; |
3d98c75e |
126 | ok( $row = $schema->resultset($source)->create({ |
fb95dc4d |
127 | $col => $dt, |
3d98c75e |
128 | %$create_extra, |
fb95dc4d |
129 | })); |
3d98c75e |
130 | ok( $row = $schema->resultset($source) |
131 | ->search({ $pk => $row->$pk }, { select => [$col] }) |
fb95dc4d |
132 | ->first |
133 | ); |
134 | is( $row->$col, $dt, "$type roundtrip" ); |
0f72fb47 |
135 | |
136 | cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond}, |
fb95dc4d |
137 | 'DateTime fractional portion roundtrip' ) |
138 | if exists $sample_dt->{nanosecond}; |
139 | } |
124162a0 |
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' ); |
5a77aa8b |
153 | } |
154 | |
124162a0 |
155 | |
fb95dc4d |
156 | done_testing; |
157 | |
5a77aa8b |
158 | # clean up our mess |
fb95dc4d |
159 | sub cleanup { |
65d35121 |
160 | my $schema = shift; |
fb95dc4d |
161 | if (my $dbh = eval { $schema->storage->dbh }) { |
5a77aa8b |
162 | $dbh->do('DROP TABLE track'); |
3d98c75e |
163 | $dbh->do('DROP TABLE event_small_dt'); |
124162a0 |
164 | $dbh->do('DROP TABLE event'); |
5a77aa8b |
165 | } |
166 | } |