added test to make sure only one connection to ODBC/MSSQL is made
[dbsrgits/DBIx-Class.git] / t / 746mssql.t
CommitLineData
c1cac633 1use strict;
2use warnings;
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
8c0104fe 13plan tests => 13;
c1cac633 14
15my $schema = DBICTest::Schema->connect($dsn, $user, $pass, {AutoCommit => 1});
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
76
77# clean up our mess
78END {
c5f77f6c 79 my $dbh = eval { $schema->storage->_dbh };
c1cac633 80 $dbh->do('DROP TABLE artist') if $dbh;
81}
82