fix double connect for ODBC/MSSQL
[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
13plan tests => 12;
14
15my $schema = DBICTest::Schema->connect($dsn, $user, $pass, {AutoCommit => 1});
16
17$schema->storage->ensure_connected;
9b3e916d 18
c1cac633 19isa_ok( $schema->storage, 'DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server' );
20
c5f77f6c 21$schema->storage->dbh_do (sub {
22 my ($storage, $dbh) = @_;
23 eval { $dbh->do("DROP TABLE artist") };
24 $dbh->do(<<'SQL');
c1cac633 25
c1cac633 26CREATE TABLE artist (
27 artistid INT IDENTITY NOT NULL,
a0dd8679 28 name VARCHAR(100),
39da2a2b 29 rank INT NOT NULL DEFAULT '13',
2eebd801 30 charfield CHAR(10) NULL,
c1cac633 31 primary key(artistid)
32)
33
c5f77f6c 34SQL
35
36});
37
c1cac633 38my %seen_id;
39
2eebd801 40# fresh $schema so we start unconnected
41$schema = DBICTest::Schema->connect($dsn, $user, $pass, {AutoCommit => 1});
42
c1cac633 43# test primary key handling
44my $new = $schema->resultset('Artist')->create({ name => 'foo' });
45ok($new->artistid > 0, "Auto-PK worked");
46
47$seen_id{$new->artistid}++;
48
49# test LIMIT support
50for (1..6) {
51 $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
52 is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
53 $seen_id{$new->artistid}++;
54}
55
56my $it = $schema->resultset('Artist')->search( {}, {
57 rows => 3,
58 order_by => 'artistid',
59});
60
61is( $it->count, 3, "LIMIT count ok" );
62is( $it->next->name, "foo", "iterator->next ok" );
63$it->next;
64is( $it->next->name, "Artist 2", "iterator->next ok" );
65is( $it->next, undef, "next past end of resultset ok" );
66
67
68# clean up our mess
69END {
c5f77f6c 70 my $dbh = eval { $schema->storage->_dbh };
c1cac633 71 $dbh->do('DROP TABLE artist') if $dbh;
72}
73