add a couple of dbd::sybase reconnection tests
[dbsrgits/DBIx-Class.git] / t / 74mssql.t
1 use strict;
2 use warnings;  
3
4 # use this if you keep a copy of DBD::Sybase linked to FreeTDS somewhere else
5 BEGIN {
6   if (my $lib_dirs = $ENV{DBICTEST_MSSQL_PERL5LIB}) {
7     unshift @INC, $_ for split /:/, $lib_dirs;
8   }
9 }
10
11 use Test::More;
12 use lib qw(t/lib);
13 use DBICTest;
14
15 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_${_}" } qw/DSN USER PASS/};
16
17 plan skip_all => 'Set $ENV{DBICTEST_MSSQL_DSN}, _USER and _PASS to run this test'
18   unless ($dsn);
19
20 plan tests => 6;
21
22 my $schema = DBICTest::Schema->clone;
23 $schema->connection($dsn, $user, $pass);
24
25 # start disconnected to test reconnection
26 $schema->storage->ensure_connected;
27 $schema->storage->disconnect;
28
29 my $dbh = $schema->storage->dbh;
30
31 isa_ok($schema->storage, 'DBIx::Class::Storage::DBI::Sybase::Microsoft_SQL_Server');
32
33 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL
34     DROP TABLE artist");
35 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL
36     DROP TABLE cd");
37
38 $dbh->do("CREATE TABLE artist (artistid INT IDENTITY PRIMARY KEY, name VARCHAR(100), rank INT DEFAULT '13', charfield CHAR(10) NULL);");
39 $dbh->do("CREATE TABLE cd (cdid INT IDENTITY PRIMARY KEY, artist INT,  title VARCHAR(100), year VARCHAR(100), genreid INT NULL, single_track INT NULL);");
40 # Just to test compat shim, Auto is in Core
41 $schema->class('Artist')->load_components('PK::Auto::MSSQL');
42
43 # Test PK
44 my $new = $schema->resultset('Artist')->create( { name => 'foo' } );
45 ok($new->artistid, "Auto-PK worked");
46
47 # Test LIMIT
48 for (1..6) {
49     $schema->resultset('Artist')->create( { name => 'Artist ' . $_, rank => $_ } );
50 }
51
52 my $it = $schema->resultset('Artist')->search( { },
53     { rows     => 3,
54       offset   => 2,
55       order_by => 'artistid'
56     }
57 );
58
59 # Test ? in data don't get treated as placeholders
60 my $cd = $schema->resultset('CD')->create( {
61     artist      => 1,
62     title       => 'Does this break things?',
63     year        => 2007,
64 } );
65 ok($cd->id, 'Not treating ? in data as placeholders');
66
67 is( $it->count, 3, "LIMIT count ok" );
68 ok( $it->next->name, "iterator->next ok" );
69 $it->next;
70 $it->next;
71 is( $it->next, undef, "next past end of resultset ok" );
72
73 # clean up our mess
74 END {
75     $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL DROP TABLE artist")
76         if $dbh;
77     $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL DROP TABLE cd")
78         if $dbh;
79 }