Fix RT57467, simplify test
[dbsrgits/DBIx-Class.git] / t / 74mssql.t
CommitLineData
70350518 1use strict;
2use warnings;
3
46e3af47 4# use this if you keep a copy of DBD::Sybase linked to FreeTDS somewhere else
5BEGIN {
6 if (my $lib_dirs = $ENV{DBICTEST_MSSQL_PERL5LIB}) {
7 unshift @INC, $_ for split /:/, $lib_dirs;
8 }
9}
10
70350518 11use Test::More;
8c52ffd3 12use Test::Exception;
70350518 13use lib qw(t/lib);
14use DBICTest;
eef2ff6c 15
16my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_${_}" } qw/DSN USER PASS/};
17
70350518 18plan skip_all => 'Set $ENV{DBICTEST_MSSQL_DSN}, _USER and _PASS to run this test'
eef2ff6c 19 unless ($dsn);
20
7379eb67 21my @storage_types = (
c29ce317 22 'DBI::Sybase::Microsoft_SQL_Server',
23 'DBI::Sybase::Microsoft_SQL_Server::NoBindVars',
7379eb67 24);
25my $storage_idx = -1;
26my $schema;
27
1a789a72 28my $NUMBER_OF_TESTS_IN_BLOCK = 18;
7379eb67 29for my $storage_type (@storage_types) {
30 $storage_idx++;
b3f41261 31
7379eb67 32 $schema = DBICTest::Schema->clone;
33
7379eb67 34 $schema->connection($dsn, $user, $pass);
8c52ffd3 35
918ab8ae 36 if ($storage_idx != 0) { # autodetect
37 no warnings 'redefine';
38 local *DBIx::Class::Storage::DBI::_typeless_placeholders_supported =
39 sub { 0 };
40# $schema->storage_type("::$storage_type");
41 $schema->storage->ensure_connected;
42 }
43 else {
44 $schema->storage->ensure_connected;
45 }
eef2ff6c 46
7379eb67 47 if ($storage_idx == 0 && ref($schema->storage) =~ /NoBindVars\z/) {
48 my $tb = Test::More->builder;
1a789a72 49 $tb->skip('no placeholders') for 1..$NUMBER_OF_TESTS_IN_BLOCK;
7379eb67 50 next;
51 }
52
c29ce317 53 isa_ok($schema->storage, "DBIx::Class::Storage::$storage_type");
7379eb67 54
99083752 55 SKIP: {
56 skip 'This version of DBD::Sybase segfaults on disconnect', 1 if DBD::Sybase->VERSION < 1.08;
7379eb67 57
99083752 58 # start disconnected to test _ping
59 $schema->storage->_dbh->disconnect;
60
61 lives_ok {
62 $schema->storage->dbh_do(sub { $_[1]->do('select 1') })
63 } '_ping works';
64 }
ecdf1ac8 65
66 my $dbh = $schema->storage->dbh;
7379eb67 67
68 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL
69 DROP TABLE artist");
70 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL
71 DROP TABLE cd");
72
73 $dbh->do("CREATE TABLE artist (artistid INT IDENTITY PRIMARY KEY, name VARCHAR(100), rank INT DEFAULT '13', charfield CHAR(10) NULL);");
74 $dbh->do("CREATE TABLE cd (cdid INT IDENTITY PRIMARY KEY, artist INT, title VARCHAR(100), year VARCHAR(100), genreid INT NULL, single_track INT NULL);");
3ff5b740 75# Just to test compat shim, Auto is in Core
7379eb67 76 $schema->class('Artist')->load_components('PK::Auto::MSSQL');
eef2ff6c 77
78# Test PK
7379eb67 79 my $new = $schema->resultset('Artist')->create( { name => 'foo' } );
80 ok($new->artistid, "Auto-PK worked");
eef2ff6c 81
82# Test LIMIT
7379eb67 83 for (1..6) {
84 $schema->resultset('Artist')->create( { name => 'Artist ' . $_, rank => $_ } );
85 }
eef2ff6c 86
7379eb67 87 my $it = $schema->resultset('Artist')->search( { },
88 { rows => 3,
89 offset => 2,
90 order_by => 'artistid'
91 }
92 );
eef2ff6c 93
b4474f31 94# Test ? in data don't get treated as placeholders
7379eb67 95 my $cd = $schema->resultset('CD')->create( {
96 artist => 1,
97 title => 'Does this break things?',
98 year => 2007,
99 } );
100 ok($cd->id, 'Not treating ? in data as placeholders');
101
102 is( $it->count, 3, "LIMIT count ok" );
103 ok( $it->next->name, "iterator->next ok" );
104 $it->next;
105 $it->next;
106 is( $it->next, undef, "next past end of resultset ok" );
eef2ff6c 107
5064f5c3 108# test MONEY column support
7379eb67 109 $schema->storage->dbh_do (sub {
110 my ($storage, $dbh) = @_;
111 eval { $dbh->do("DROP TABLE money_test") };
112 $dbh->do(<<'SQL');
7379eb67 113 CREATE TABLE money_test (
114 id INT IDENTITY PRIMARY KEY,
115 amount MONEY NULL
116 )
5064f5c3 117SQL
118
7379eb67 119 });
5064f5c3 120
7379eb67 121 my $rs = $schema->resultset('Money');
5064f5c3 122
7379eb67 123 my $row;
124 lives_ok {
125 $row = $rs->create({ amount => 100 });
126 } 'inserted a money value';
5064f5c3 127
a33d2444 128 cmp_ok $rs->find($row->id)->amount, '==', 100, 'money value round-trip';
5064f5c3 129
7379eb67 130 lives_ok {
131 $row->update({ amount => 200 });
132 } 'updated a money value';
5064f5c3 133
a33d2444 134 cmp_ok $rs->find($row->id)->amount, '==', 200,
135 'updated money value round-trip';
5064f5c3 136
7379eb67 137 lives_ok {
138 $row->update({ amount => undef });
139 } 'updated a money value to NULL';
5064f5c3 140
c8365716 141 is $rs->find($row->id)->amount,
142 undef, 'updated money value to NULL round-trip';
a467a0c9 143
8a0720e2 144 $rs->create({ amount => 300 }) for (1..3);
145
a467a0c9 146 # test multiple active statements
147 lives_ok {
a467a0c9 148 my $artist_rs = $schema->resultset('Artist');
149 while (my $row = $rs->next) {
150 my $artist = $artist_rs->next;
151 }
8a0720e2 152 $rs->reset;
a467a0c9 153 } 'multiple active statements';
8a0720e2 154
b90d7eba 155 $rs->delete;
156
157 # test simple transaction with commit
158 lives_ok {
159 $schema->txn_do(sub {
160 $rs->create({ amount => 400 });
161 });
162 } 'simple transaction';
163
164 cmp_ok $rs->first->amount, '==', 400, 'committed';
165 $rs->reset;
166
167 $rs->delete;
168
169 # test rollback
170 throws_ok {
171 $schema->txn_do(sub {
172 $rs->create({ amount => 400 });
173 die 'mtfnpy';
174 });
175 } qr/mtfnpy/, 'simple failed txn';
176
177 is $rs->first, undef, 'rolled back';
178 $rs->reset;
a218ef4e 179
180 # test RNO detection when version detection fails
181 SKIP: {
182 my $storage = $schema->storage;
183 my $version = $storage->_server_info->{normalized_dbms_version};
99083752 184
185 skip 'could not detect SQL Server version', 1 if not defined $version;
a218ef4e 186
187 my $have_rno = $version >= 9 ? 1 : 0;
188
a218ef4e 189 local $storage->{_sql_maker} = undef;
190 local $storage->{_sql_maker_opts} = undef;
191
4282b6f8 192 local $storage->{_server_info_hash} = { %{ $storage->_server_info_hash } }; # clone
193 delete @{$storage->{_server_info_hash}}{qw/dbms_version normalized_dbms_version/};
194
a218ef4e 195 $storage->sql_maker;
196
197 my $rno_detected =
233c3a46 198 ($storage->{_sql_maker_opts}{limit_dialect} eq 'RowNumberOver') ? 1 : 0;
a218ef4e 199
233c3a46 200 ok (($have_rno == $rno_detected),
a218ef4e 201 'row_number() over support detected correctly');
202 }
0a064375 203
204 {
205 my $schema = DBICTest::Schema->clone;
206 $schema->connection($dsn, $user, $pass);
207
208 like $schema->storage->sql_maker->{limit_dialect},
209 qr/^(?:Top|RowNumberOver)\z/,
210 'sql_maker is correct on unconnected schema';
211 }
7379eb67 212}
5064f5c3 213
559ae74c 214# test op-induced autoconnect
215lives_ok (sub {
216
217 my $schema = DBICTest::Schema->clone;
218 $schema->connection($dsn, $user, $pass);
219
220 my $artist = $schema->resultset ('Artist')->search ({}, { order_by => 'artistid' })->next;
221 is ($artist->id, 1, 'Artist retrieved successfully');
222}, 'Query-induced autoconnect works');
223
1a789a72 224done_testing;
225
3ff5b740 226# clean up our mess
227END {
7379eb67 228 if (my $dbh = eval { $schema->storage->dbh }) {
229 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL DROP TABLE artist");
230 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL DROP TABLE cd");
231 $dbh->do("IF OBJECT_ID('money_test', 'U') IS NOT NULL DROP TABLE money_test");
232 }
3ff5b740 233}