add IC::DT tests for odbc/mssql
[dbsrgits/DBIx-Class-Historic.git] / t / 746mssql.t
CommitLineData
c1cac633 1use strict;
b9a2c3a5 2use warnings;
c1cac633 3
4use Test::More;
893403c8 5use Test::Exception;
c1cac633 6use lib qw(t/lib);
7use DBICTest;
b2d16f1f 8use DBIC::SqlMakerTest;
c1cac633 9
10my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PASS/};
11
12plan skip_all => 'Set $ENV{DBICTEST_MSSQL_ODBC_DSN}, _USER and _PASS to run this test'
13 unless ($dsn && $user);
14
ef131d82 15plan tests => 28;
c1cac633 16
42e5b103 17my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
c1cac633 18
8c0104fe 19{
20 no warnings 'redefine';
21 my $connect_count = 0;
22 my $orig_connect = \&DBI::connect;
23 local *DBI::connect = sub { $connect_count++; goto &$orig_connect };
24
25 $schema->storage->ensure_connected;
26
27 is( $connect_count, 1, 'only one connection made');
28}
9b3e916d 29
c1cac633 30isa_ok( $schema->storage, 'DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server' );
31
c5f77f6c 32$schema->storage->dbh_do (sub {
33 my ($storage, $dbh) = @_;
34 eval { $dbh->do("DROP TABLE artist") };
35 $dbh->do(<<'SQL');
c1cac633 36
c1cac633 37CREATE TABLE artist (
38 artistid INT IDENTITY NOT NULL,
a0dd8679 39 name VARCHAR(100),
39da2a2b 40 rank INT NOT NULL DEFAULT '13',
2eebd801 41 charfield CHAR(10) NULL,
c1cac633 42 primary key(artistid)
43)
44
c5f77f6c 45SQL
46
47});
48
c1cac633 49my %seen_id;
50
ef131d82 51my @opts = (
52 { on_connect_call => 'use_dynamic_cursors' },
53 {},
54);
55my $new;
2eebd801 56
ef131d82 57# test Auto-PK with different options
58for my $opts (@opts) {
59 $schema = DBICTest::Schema->clone;
60 $schema->connection($dsn, $user, $pass, $opts);
61
62 $schema->resultset('Artist')->search({ name => 'foo' })->delete;
63
64 $new = $schema->resultset('Artist')->create({ name => 'foo' });
65 ok($new->artistid > 0, "Auto-PK worked");
66}
c1cac633 67
68$seen_id{$new->artistid}++;
69
70# test LIMIT support
71for (1..6) {
72 $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
73 is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
74 $seen_id{$new->artistid}++;
75}
76
77my $it = $schema->resultset('Artist')->search( {}, {
78 rows => 3,
79 order_by => 'artistid',
80});
81
82is( $it->count, 3, "LIMIT count ok" );
83is( $it->next->name, "foo", "iterator->next ok" );
84$it->next;
85is( $it->next->name, "Artist 2", "iterator->next ok" );
86is( $it->next, undef, "next past end of resultset ok" );
87
b9a2c3a5 88$schema->storage->dbh_do (sub {
89 my ($storage, $dbh) = @_;
90 eval { $dbh->do("DROP TABLE Owners") };
91 eval { $dbh->do("DROP TABLE Books") };
92 $dbh->do(<<'SQL');
93
94
95CREATE TABLE Books (
96 id INT IDENTITY (1, 1) NOT NULL,
97 source VARCHAR(100),
98 owner INT,
99 title VARCHAR(10),
100 price INT NULL
101)
102
103CREATE TABLE Owners (
104 id INT IDENTITY (1, 1) NOT NULL,
42e5b103 105 name VARCHAR(100),
b9a2c3a5 106)
107
b9a2c3a5 108SQL
109
110});
893403c8 111
112lives_ok ( sub {
113 $schema->populate ('Owners', [
114 [qw/id name /],
115 [qw/1 wiggle/],
116 [qw/2 woggle/],
117 [qw/3 boggle/],
118 [qw/4 fREW/],
119 [qw/5 fRIOUX/],
120 [qw/6 fROOH/],
121 [qw/7 fRUE/],
122 [qw/8 fISMBoC/],
123 [qw/9 station/],
124 [qw/10 mirror/],
125 [qw/11 dimly/],
126 [qw/12 face_to_face/],
127 [qw/13 icarus/],
128 [qw/14 dream/],
129 [qw/15 dyrstyggyr/],
130 ]);
131}, 'populate with PKs supplied ok' );
132
133lives_ok ( sub {
134 $schema->populate ('BooksInLibrary', [
135 [qw/source owner title /],
136 [qw/Library 1 secrets0/],
137 [qw/Library 1 secrets1/],
138 [qw/Eatery 1 secrets2/],
139 [qw/Library 2 secrets3/],
140 [qw/Library 3 secrets4/],
141 [qw/Eatery 3 secrets5/],
142 [qw/Library 4 secrets6/],
143 [qw/Library 5 secrets7/],
144 [qw/Eatery 5 secrets8/],
145 [qw/Library 6 secrets9/],
146 [qw/Library 7 secrets10/],
147 [qw/Eatery 7 secrets11/],
148 [qw/Library 8 secrets12/],
149 ]);
150}, 'populate without PKs supplied ok' );
b9a2c3a5 151
152#
02d133f0 153# try a prefetch on tables with identically named columns
b9a2c3a5 154#
155
02d133f0 156# set quote char - make sure things work while quoted
157$schema->storage->_sql_maker->{quote_char} = [qw/[ ]/];
158$schema->storage->_sql_maker->{name_sep} = '.';
159
b9a2c3a5 160{
893403c8 161 # try a ->has_many direction
fc85215b 162 my $owners = $schema->resultset ('Owners')->search ({
163 'books.id' => { '!=', undef }
164 }, {
165 prefetch => 'books',
ff1ef65b 166 order_by => 'name',
56d2561e 167 rows => 3, # 8 results total
fc85215b 168 });
169
56d2561e 170 is ($owners->page(1)->all, 3, 'has_many prefetch returns correct number of rows');
171 is ($owners->page(1)->count, 3, 'has-many prefetch returns correct count');
172
618a0fe3 173 TODO: {
174 local $TODO = 'limit past end of resultset problem';
56d2561e 175 is ($owners->page(3)->all, 2, 'has_many prefetch returns correct number of rows');
176 is ($owners->page(3)->count, 2, 'has-many prefetch returns correct count');
64c7c000 177 is ($owners->page(3)->count_rs->next, 2, 'has-many prefetch returns correct count_rs');
178
179 # make sure count does not become overly complex FIXME
180 is_same_sql_bind (
181 $owners->page(3)->count_rs->as_query,
182 '(
183 SELECT COUNT( * )
184 FROM (
02d133f0 185 SELECT TOP 3 [me].[id]
186 FROM [owners] [me]
187 LEFT JOIN [books] [books] ON [books].[owner] = [me].[id]
188 WHERE ( [books].[id] IS NOT NULL )
189 GROUP BY [me].[id]
190 ORDER BY [me].[id] DESC
191 ) [count_subq]
64c7c000 192 )',
193 [],
194 );
618a0fe3 195 }
b9a2c3a5 196
42e5b103 197 # try a ->belongs_to direction (no select collapse, group_by should work)
fc85215b 198 my $books = $schema->resultset ('BooksInLibrary')->search ({
56d2561e 199 'owner.name' => [qw/wiggle woggle/],
fc85215b 200 }, {
fc85215b 201 distinct => 1,
42e5b103 202 prefetch => 'owner',
56d2561e 203 rows => 2, # 3 results total
ac93965c 204 order_by => { -desc => 'owner' },
205 # there is no sane way to order by the right side of a grouped prefetch currently :(
206 #order_by => { -desc => 'owner.name' },
fc85215b 207 });
208
b1e1d073 209
56d2561e 210 is ($books->page(1)->all, 2, 'Prefetched grouped search returns correct number of rows');
211 is ($books->page(1)->count, 2, 'Prefetched grouped search returns correct count');
b1e1d073 212
618a0fe3 213 TODO: {
214 local $TODO = 'limit past end of resultset problem';
56d2561e 215 is ($books->page(2)->all, 1, 'Prefetched grouped search returns correct number of rows');
216 is ($books->page(2)->count, 1, 'Prefetched grouped search returns correct count');
64c7c000 217 is ($books->page(2)->count_rs->next, 1, 'Prefetched grouped search returns correct count_rs');
218
219 # make sure count does not become overly complex FIXME
220 is_same_sql_bind (
221 $books->page(2)->count_rs->as_query,
222 '(
223 SELECT COUNT( * )
224 FROM (
02d133f0 225 SELECT TOP 2 [me].[id]
226 FROM [books] [me]
227 JOIN [owners] [owner] ON [owner].[id] = [me].[owner]
228 WHERE ( ( ( [owner].[name] = ? OR [owner].[name] = ? ) AND [source] = ? ) )
229 GROUP BY [me].[id], [me].[source], [me].[owner], [me].[title], [me].[price], [owner].[id], [owner].[name]
230 ORDER BY [me].[id] DESC
231 ) [count_subq]
64c7c000 232 )',
233 [
234 [ 'owner.name' => 'wiggle' ],
235 [ 'owner.name' => 'woggle' ],
236 [ 'source' => 'Library' ],
237 ],
238 );
618a0fe3 239 }
b2d16f1f 240
b9a2c3a5 241}
c1cac633 242
243# clean up our mess
244END {
c5f77f6c 245 my $dbh = eval { $schema->storage->_dbh };
c1cac633 246 $dbh->do('DROP TABLE artist') if $dbh;
247}
fc85215b 248# vim:sw=2 sts=2