Some fixes after review
[dbsrgits/DBIx-Class.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
893403c8 15plan tests => 27;
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
2eebd801 51# fresh $schema so we start unconnected
c6fc5645 52$schema = DBICTest::Schema->connect($dsn, $user, $pass);
2eebd801 53
c1cac633 54# test primary key handling
55my $new = $schema->resultset('Artist')->create({ name => 'foo' });
56ok($new->artistid > 0, "Auto-PK worked");
57
58$seen_id{$new->artistid}++;
59
60# test LIMIT support
61for (1..6) {
62 $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
63 is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
64 $seen_id{$new->artistid}++;
65}
66
67my $it = $schema->resultset('Artist')->search( {}, {
68 rows => 3,
69 order_by => 'artistid',
70});
71
72is( $it->count, 3, "LIMIT count ok" );
73is( $it->next->name, "foo", "iterator->next ok" );
74$it->next;
75is( $it->next->name, "Artist 2", "iterator->next ok" );
76is( $it->next, undef, "next past end of resultset ok" );
77
b9a2c3a5 78$schema->storage->dbh_do (sub {
79 my ($storage, $dbh) = @_;
80 eval { $dbh->do("DROP TABLE Owners") };
81 eval { $dbh->do("DROP TABLE Books") };
82 $dbh->do(<<'SQL');
83
84
85CREATE TABLE Books (
86 id INT IDENTITY (1, 1) NOT NULL,
87 source VARCHAR(100),
88 owner INT,
89 title VARCHAR(10),
90 price INT NULL
91)
92
93CREATE TABLE Owners (
94 id INT IDENTITY (1, 1) NOT NULL,
42e5b103 95 name VARCHAR(100),
b9a2c3a5 96)
97
b9a2c3a5 98SQL
99
100});
893403c8 101
102lives_ok ( sub {
103 $schema->populate ('Owners', [
104 [qw/id name /],
105 [qw/1 wiggle/],
106 [qw/2 woggle/],
107 [qw/3 boggle/],
108 [qw/4 fREW/],
109 [qw/5 fRIOUX/],
110 [qw/6 fROOH/],
111 [qw/7 fRUE/],
112 [qw/8 fISMBoC/],
113 [qw/9 station/],
114 [qw/10 mirror/],
115 [qw/11 dimly/],
116 [qw/12 face_to_face/],
117 [qw/13 icarus/],
118 [qw/14 dream/],
119 [qw/15 dyrstyggyr/],
120 ]);
121}, 'populate with PKs supplied ok' );
122
123lives_ok ( sub {
124 $schema->populate ('BooksInLibrary', [
125 [qw/source owner title /],
126 [qw/Library 1 secrets0/],
127 [qw/Library 1 secrets1/],
128 [qw/Eatery 1 secrets2/],
129 [qw/Library 2 secrets3/],
130 [qw/Library 3 secrets4/],
131 [qw/Eatery 3 secrets5/],
132 [qw/Library 4 secrets6/],
133 [qw/Library 5 secrets7/],
134 [qw/Eatery 5 secrets8/],
135 [qw/Library 6 secrets9/],
136 [qw/Library 7 secrets10/],
137 [qw/Eatery 7 secrets11/],
138 [qw/Library 8 secrets12/],
139 ]);
140}, 'populate without PKs supplied ok' );
b9a2c3a5 141
142#
02d133f0 143# try a prefetch on tables with identically named columns
b9a2c3a5 144#
145
02d133f0 146# set quote char - make sure things work while quoted
147$schema->storage->_sql_maker->{quote_char} = [qw/[ ]/];
148$schema->storage->_sql_maker->{name_sep} = '.';
149
b9a2c3a5 150{
893403c8 151 # try a ->has_many direction
fc85215b 152 my $owners = $schema->resultset ('Owners')->search ({
153 'books.id' => { '!=', undef }
154 }, {
155 prefetch => 'books',
ff1ef65b 156 order_by => 'name',
56d2561e 157 rows => 3, # 8 results total
fc85215b 158 });
159
56d2561e 160 is ($owners->page(1)->all, 3, 'has_many prefetch returns correct number of rows');
161 is ($owners->page(1)->count, 3, 'has-many prefetch returns correct count');
162
618a0fe3 163 TODO: {
164 local $TODO = 'limit past end of resultset problem';
56d2561e 165 is ($owners->page(3)->all, 2, 'has_many prefetch returns correct number of rows');
166 is ($owners->page(3)->count, 2, 'has-many prefetch returns correct count');
64c7c000 167 is ($owners->page(3)->count_rs->next, 2, 'has-many prefetch returns correct count_rs');
168
169 # make sure count does not become overly complex FIXME
170 is_same_sql_bind (
171 $owners->page(3)->count_rs->as_query,
172 '(
173 SELECT COUNT( * )
174 FROM (
02d133f0 175 SELECT TOP 3 [me].[id]
176 FROM [owners] [me]
177 LEFT JOIN [books] [books] ON [books].[owner] = [me].[id]
178 WHERE ( [books].[id] IS NOT NULL )
179 GROUP BY [me].[id]
180 ORDER BY [me].[id] DESC
181 ) [count_subq]
64c7c000 182 )',
183 [],
184 );
618a0fe3 185 }
b9a2c3a5 186
42e5b103 187 # try a ->belongs_to direction (no select collapse, group_by should work)
fc85215b 188 my $books = $schema->resultset ('BooksInLibrary')->search ({
56d2561e 189 'owner.name' => [qw/wiggle woggle/],
fc85215b 190 }, {
fc85215b 191 distinct => 1,
42e5b103 192 prefetch => 'owner',
ff1ef65b 193 order_by => 'name',
56d2561e 194 rows => 2, # 3 results total
fc85215b 195 });
196
b1e1d073 197
56d2561e 198 is ($books->page(1)->all, 2, 'Prefetched grouped search returns correct number of rows');
199 is ($books->page(1)->count, 2, 'Prefetched grouped search returns correct count');
b1e1d073 200
618a0fe3 201 TODO: {
202 local $TODO = 'limit past end of resultset problem';
56d2561e 203 is ($books->page(2)->all, 1, 'Prefetched grouped search returns correct number of rows');
204 is ($books->page(2)->count, 1, 'Prefetched grouped search returns correct count');
64c7c000 205 is ($books->page(2)->count_rs->next, 1, 'Prefetched grouped search returns correct count_rs');
206
207 # make sure count does not become overly complex FIXME
208 is_same_sql_bind (
209 $books->page(2)->count_rs->as_query,
210 '(
211 SELECT COUNT( * )
212 FROM (
02d133f0 213 SELECT TOP 2 [me].[id]
214 FROM [books] [me]
215 JOIN [owners] [owner] ON [owner].[id] = [me].[owner]
216 WHERE ( ( ( [owner].[name] = ? OR [owner].[name] = ? ) AND [source] = ? ) )
217 GROUP BY [me].[id], [me].[source], [me].[owner], [me].[title], [me].[price], [owner].[id], [owner].[name]
218 ORDER BY [me].[id] DESC
219 ) [count_subq]
64c7c000 220 )',
221 [
222 [ 'owner.name' => 'wiggle' ],
223 [ 'owner.name' => 'woggle' ],
224 [ 'source' => 'Library' ],
225 ],
226 );
618a0fe3 227 }
b2d16f1f 228
b9a2c3a5 229}
c1cac633 230
231# clean up our mess
232END {
c5f77f6c 233 my $dbh = eval { $schema->storage->_dbh };
c1cac633 234 $dbh->do('DROP TABLE artist') if $dbh;
235}
fc85215b 236# vim:sw=2 sts=2