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