Really fix tests
[dbsrgits/DBIx-Class.git] / t / 746mssql.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PASS/};
9
10 plan skip_all => 'Set $ENV{DBICTEST_MSSQL_ODBC_DSN}, _USER and _PASS to run this test'
11   unless ($dsn && $user);
12
13 plan tests => 21;
14
15 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
16
17 {
18   no warnings 'redefine';
19   my $connect_count = 0;
20   my $orig_connect = \&DBI::connect;
21   local *DBI::connect = sub { $connect_count++; goto &$orig_connect };
22
23   $schema->storage->ensure_connected;
24
25   is( $connect_count, 1, 'only one connection made');
26 }
27
28 isa_ok( $schema->storage, 'DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server' );
29
30 $schema->storage->dbh_do (sub {
31     my ($storage, $dbh) = @_;
32     eval { $dbh->do("DROP TABLE artist") };
33     $dbh->do(<<'SQL');
34
35 CREATE TABLE artist (
36    artistid INT IDENTITY NOT NULL,
37    name VARCHAR(100),
38    rank INT NOT NULL DEFAULT '13',
39    charfield CHAR(10) NULL,
40    primary key(artistid)
41 )
42
43 SQL
44
45 });
46
47 my %seen_id;
48
49 # fresh $schema so we start unconnected
50 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {AutoCommit => 1});
51
52 # test primary key handling
53 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
54 ok($new->artistid > 0, "Auto-PK worked");
55
56 $seen_id{$new->artistid}++;
57
58 # test LIMIT support
59 for (1..6) {
60     $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
61     is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
62     $seen_id{$new->artistid}++;
63 }
64
65 my $it = $schema->resultset('Artist')->search( {}, {
66     rows => 3,
67     order_by => 'artistid',
68 });
69
70 is( $it->count, 3, "LIMIT count ok" );
71 is( $it->next->name, "foo", "iterator->next ok" );
72 $it->next;
73 is( $it->next->name, "Artist 2", "iterator->next ok" );
74 is( $it->next, undef, "next past end of resultset ok" );
75
76 $schema->storage->dbh_do (sub {
77     my ($storage, $dbh) = @_;
78     eval { $dbh->do("DROP TABLE Owners") };
79     eval { $dbh->do("DROP TABLE Books") };
80     $dbh->do(<<'SQL');
81
82
83 CREATE TABLE Books (
84    id INT IDENTITY (1, 1) NOT NULL,
85    source VARCHAR(100),
86    owner INT,
87    title VARCHAR(10),
88    price INT NULL
89 )
90
91 CREATE TABLE Owners (
92    id INT IDENTITY (1, 1) NOT NULL,
93    name VARCHAR(100),
94 )
95
96 SQL
97
98 });
99 $schema->populate ('Owners', [
100   [qw/id  name  /],
101   [qw/1   wiggle/],
102   [qw/2   woggle/],
103   [qw/3   boggle/],
104   [qw/4   fREW/],
105   [qw/5   fRIOUX/],
106   [qw/6   fROOH/],
107   [qw/7   fRUE/],
108   [qw/8   fISMBoC/],
109   [qw/9   station/],
110   [qw/10   mirror/],
111   [qw/11   dimly/],
112   [qw/12   face_to_face/],
113   [qw/13   icarus/],
114   [qw/14   dream/],
115   [qw/15   dyrstyggyr/],
116 ]);
117
118 $schema->populate ('BooksInLibrary', [
119   [qw/source  owner title   /],
120   [qw/Library 1     secrets0/],
121   [qw/Library 1     secrets1/],
122   [qw/Eatery  1     secrets2/],
123   [qw/Library 2     secrets3/],
124   [qw/Library 3     secrets4/],
125   [qw/Eatery  3     secrets5/],
126   [qw/Library 4     secrets6/],
127   [qw/Library 5     secrets7/],
128   [qw/Eatery  5     secrets8/],
129   [qw/Library 6     secrets9/],
130   [qw/Library 7     secrets10/],
131   [qw/Eatery  7     secrets11/],
132   [qw/Library 8     secrets12/],
133 ]);
134
135 #
136 # try a distinct + prefetch on tables with identically named columns
137 #
138
139 {
140   # try a ->has_many direction (group_by is not possible on has_many with limit)
141   my $owners = $schema->resultset ('Owners')->search ({
142       'books.id' => { '!=', undef }
143     }, {
144       prefetch => 'books',
145       order_by => 'name',
146       rows     => 3,  # 8 results total
147     });
148
149   is ($owners->page(1)->all, 3, 'has_many prefetch returns correct number of rows');
150   is ($owners->page(1)->count, 3, 'has-many prefetch returns correct count');
151
152   TODO: {
153     local $TODO = 'limit past end of resultset problem';
154     is ($owners->page(3)->all, 2, 'has_many prefetch returns correct number of rows');
155     is ($owners->page(3)->count, 2, 'has-many prefetch returns correct count');
156   }
157
158   # try a ->belongs_to direction (no select collapse, group_by should work)
159   my $books = $schema->resultset ('BooksInLibrary')->search ({
160       'owner.name' => [qw/wiggle woggle/],
161     }, {
162       distinct => 1,
163       prefetch => 'owner',
164       order_by => 'name',
165       rows     => 2,  # 3 results total
166     });
167
168
169   is ($books->page(1)->all, 2, 'Prefetched grouped search returns correct number of rows');
170   is ($books->page(1)->count, 2, 'Prefetched grouped search returns correct count');
171
172   TODO: {
173     local $TODO = 'limit past end of resultset problem';
174     is ($books->page(2)->all, 1, 'Prefetched grouped search returns correct number of rows');
175     is ($books->page(2)->count, 1, 'Prefetched grouped search returns correct count');
176   }
177 }
178
179 # clean up our mess
180 END {
181     my $dbh = eval { $schema->storage->_dbh };
182     $dbh->do('DROP TABLE artist') if $dbh;
183 }
184 # vim:sw=2 sts=2