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