Test and merge fixes
[dbsrgits/DBIx-Class.git] / t / 746mssql.t
CommitLineData
c1cac633 1use strict;
b9a2c3a5 2use warnings;
c1cac633 3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
8my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PASS/};
9
10plan skip_all => 'Set $ENV{DBICTEST_MSSQL_ODBC_DSN}, _USER and _PASS to run this test'
11 unless ($dsn && $user);
12
b1e1d073 13plan tests => 19;
c1cac633 14
42e5b103 15my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
c1cac633 16
8c0104fe 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}
9b3e916d 27
c1cac633 28isa_ok( $schema->storage, 'DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server' );
29
c5f77f6c 30$schema->storage->dbh_do (sub {
31 my ($storage, $dbh) = @_;
32 eval { $dbh->do("DROP TABLE artist") };
33 $dbh->do(<<'SQL');
c1cac633 34
c1cac633 35CREATE TABLE artist (
36 artistid INT IDENTITY NOT NULL,
a0dd8679 37 name VARCHAR(100),
39da2a2b 38 rank INT NOT NULL DEFAULT '13',
2eebd801 39 charfield CHAR(10) NULL,
c1cac633 40 primary key(artistid)
41)
42
c5f77f6c 43SQL
44
45});
46
c1cac633 47my %seen_id;
48
2eebd801 49# fresh $schema so we start unconnected
50$schema = DBICTest::Schema->connect($dsn, $user, $pass, {AutoCommit => 1});
51
c1cac633 52# test primary key handling
53my $new = $schema->resultset('Artist')->create({ name => 'foo' });
54ok($new->artistid > 0, "Auto-PK worked");
55
56$seen_id{$new->artistid}++;
57
58# test LIMIT support
59for (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
65my $it = $schema->resultset('Artist')->search( {}, {
66 rows => 3,
67 order_by => 'artistid',
68});
69
70is( $it->count, 3, "LIMIT count ok" );
71is( $it->next->name, "foo", "iterator->next ok" );
72$it->next;
73is( $it->next->name, "Artist 2", "iterator->next ok" );
74is( $it->next, undef, "next past end of resultset ok" );
75
b9a2c3a5 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
83CREATE 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
91CREATE TABLE Owners (
92 id INT IDENTITY (1, 1) NOT NULL,
42e5b103 93 name VARCHAR(100),
b9a2c3a5 94)
95
b9a2c3a5 96SQL
97
98});
99$schema->populate ('Owners', [
134a3bb9 100 [qw/id name /],
b9a2c3a5 101 [qw/1 wiggle/],
102 [qw/2 woggle/],
103 [qw/3 boggle/],
fc85215b 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/],
b9a2c3a5 116]);
117
118$schema->populate ('BooksInLibrary', [
119 [qw/source owner title /],
120 [qw/Library 1 secrets1/],
121 [qw/Eatery 1 secrets2/],
122 [qw/Library 2 secrets3/],
fc85215b 123 [qw/Library 3 secrets4/],
124 [qw/Eatery 3 secrets5/],
125 [qw/Library 4 secrets6/],
126 [qw/Library 5 secrets7/],
127 [qw/Eatery 5 secrets8/],
128 [qw/Library 6 secrets9/],
129 [qw/Library 7 secrets10/],
130 [qw/Eatery 7 secrets11/],
131 [qw/Library 8 secrets12/],
b9a2c3a5 132]);
133
134#
135# try a distinct + prefetch on tables with identically named columns
136#
137
138{
42e5b103 139 # try a ->has_many direction (group_by is not possible on has_many with limit)
fc85215b 140 my $owners = $schema->resultset ('Owners')->search ({
141 'books.id' => { '!=', undef }
142 }, {
143 prefetch => 'books',
ff1ef65b 144 order_by => 'name',
145 page => 2,
618a0fe3 146 rows => 4,
fc85215b 147 });
148
618a0fe3 149 TODO: {
150 local $TODO = 'limit past end of resultset problem';
151 is ($owners->all, 3, 'has_many prefetch returns correct number of rows');
152 is ($owners->count, 3, 'has-many prefetch returns correct count');
153 }
b9a2c3a5 154
42e5b103 155 # try a ->belongs_to direction (no select collapse, group_by should work)
fc85215b 156 my $books = $schema->resultset ('BooksInLibrary')->search ({
157 'owner.name' => 'wiggle'
158 }, {
fc85215b 159 distinct => 1,
42e5b103 160 prefetch => 'owner',
ff1ef65b 161 order_by => 'name',
b1e1d073 162 rows => 5,
fc85215b 163 });
164
b1e1d073 165
166 is ($books->page(1)->all, 1, 'Prefetched grouped search returns correct number of rows');
167 is ($books->page(1)->count, 1, 'Prefetched grouped search returns correct count');
168
618a0fe3 169 TODO: {
170 local $TODO = 'limit past end of resultset problem';
171 is ($books->page(2)->all, 0, 'Prefetched grouped search returns correct number of rows');
172 is ($books->page(2)->count, 0, 'Prefetched grouped search returns correct count');
173 }
b9a2c3a5 174}
c1cac633 175
176# clean up our mess
177END {
c5f77f6c 178 my $dbh = eval { $schema->storage->_dbh };
c1cac633 179 $dbh->do('DROP TABLE artist') if $dbh;
180}
fc85215b 181# vim:sw=2 sts=2