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