Changes and small ommission
[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
55# start disconnected to test reconnection
56 $schema->storage->_dbh->disconnect;
57
58 my $dbh;
59 lives_ok (sub {
60 $dbh = $schema->storage->dbh;
61 }, 'reconnect works');
62
63 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL
64 DROP TABLE artist");
65 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL
66 DROP TABLE cd");
67
68 $dbh->do("CREATE TABLE artist (artistid INT IDENTITY PRIMARY KEY, name VARCHAR(100), rank INT DEFAULT '13', charfield CHAR(10) NULL);");
69 $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 70# Just to test compat shim, Auto is in Core
7379eb67 71 $schema->class('Artist')->load_components('PK::Auto::MSSQL');
eef2ff6c 72
73# Test PK
7379eb67 74 my $new = $schema->resultset('Artist')->create( { name => 'foo' } );
75 ok($new->artistid, "Auto-PK worked");
eef2ff6c 76
77# Test LIMIT
7379eb67 78 for (1..6) {
79 $schema->resultset('Artist')->create( { name => 'Artist ' . $_, rank => $_ } );
80 }
eef2ff6c 81
7379eb67 82 my $it = $schema->resultset('Artist')->search( { },
83 { rows => 3,
84 offset => 2,
85 order_by => 'artistid'
86 }
87 );
eef2ff6c 88
b4474f31 89# Test ? in data don't get treated as placeholders
7379eb67 90 my $cd = $schema->resultset('CD')->create( {
91 artist => 1,
92 title => 'Does this break things?',
93 year => 2007,
94 } );
95 ok($cd->id, 'Not treating ? in data as placeholders');
96
97 is( $it->count, 3, "LIMIT count ok" );
98 ok( $it->next->name, "iterator->next ok" );
99 $it->next;
100 $it->next;
101 is( $it->next, undef, "next past end of resultset ok" );
eef2ff6c 102
5064f5c3 103# test MONEY column support
7379eb67 104 $schema->storage->dbh_do (sub {
105 my ($storage, $dbh) = @_;
106 eval { $dbh->do("DROP TABLE money_test") };
107 $dbh->do(<<'SQL');
7379eb67 108 CREATE TABLE money_test (
109 id INT IDENTITY PRIMARY KEY,
110 amount MONEY NULL
111 )
5064f5c3 112SQL
113
7379eb67 114 });
5064f5c3 115
7379eb67 116 my $rs = $schema->resultset('Money');
5064f5c3 117
7379eb67 118 my $row;
119 lives_ok {
120 $row = $rs->create({ amount => 100 });
121 } 'inserted a money value';
5064f5c3 122
a33d2444 123 cmp_ok $rs->find($row->id)->amount, '==', 100, 'money value round-trip';
5064f5c3 124
7379eb67 125 lives_ok {
126 $row->update({ amount => 200 });
127 } 'updated a money value';
5064f5c3 128
a33d2444 129 cmp_ok $rs->find($row->id)->amount, '==', 200,
130 'updated money value round-trip';
5064f5c3 131
7379eb67 132 lives_ok {
133 $row->update({ amount => undef });
134 } 'updated a money value to NULL';
5064f5c3 135
c8365716 136 is $rs->find($row->id)->amount,
137 undef, 'updated money value to NULL round-trip';
a467a0c9 138
8a0720e2 139 $rs->create({ amount => 300 }) for (1..3);
140
a467a0c9 141 # test multiple active statements
142 lives_ok {
a467a0c9 143 my $artist_rs = $schema->resultset('Artist');
144 while (my $row = $rs->next) {
145 my $artist = $artist_rs->next;
146 }
8a0720e2 147 $rs->reset;
a467a0c9 148 } 'multiple active statements';
8a0720e2 149
b90d7eba 150 $rs->delete;
151
152 # test simple transaction with commit
153 lives_ok {
154 $schema->txn_do(sub {
155 $rs->create({ amount => 400 });
156 });
157 } 'simple transaction';
158
159 cmp_ok $rs->first->amount, '==', 400, 'committed';
160 $rs->reset;
161
162 $rs->delete;
163
164 # test rollback
165 throws_ok {
166 $schema->txn_do(sub {
167 $rs->create({ amount => 400 });
168 die 'mtfnpy';
169 });
170 } qr/mtfnpy/, 'simple failed txn';
171
172 is $rs->first, undef, 'rolled back';
173 $rs->reset;
7379eb67 174}
5064f5c3 175
559ae74c 176# test op-induced autoconnect
177lives_ok (sub {
178
179 my $schema = DBICTest::Schema->clone;
180 $schema->connection($dsn, $user, $pass);
181
182 my $artist = $schema->resultset ('Artist')->search ({}, { order_by => 'artistid' })->next;
183 is ($artist->id, 1, 'Artist retrieved successfully');
184}, 'Query-induced autoconnect works');
185
1a789a72 186done_testing;
187
3ff5b740 188# clean up our mess
189END {
7379eb67 190 if (my $dbh = eval { $schema->storage->dbh }) {
191 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL DROP TABLE artist");
192 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL DROP TABLE cd");
193 $dbh->do("IF OBJECT_ID('money_test', 'U') IS NOT NULL DROP TABLE money_test");
194 }
3ff5b740 195}