add a couple of dbd::sybase reconnection tests
[dbsrgits/DBIx-Class.git] / t / 746sybase.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_SYBASE_${_}" } qw/DSN USER PASS/};
9
10 plan skip_all => 'Set $ENV{DBICTEST_SYBASE_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 # start disconnected to test reconnection
18 $schema->storage->ensure_connected;
19 $schema->storage->disconnect;
20
21 isa_ok( $schema->storage, 'DBIx::Class::Storage::DBI::Sybase' );
22
23 $schema->storage->dbh_do (sub {
24     my ($storage, $dbh) = @_;
25     eval { $dbh->do("DROP TABLE artist") };
26     $dbh->do(<<'SQL');
27
28 CREATE TABLE artist (
29    artistid INT IDENTITY NOT NULL,
30    name VARCHAR(100),
31    rank INT DEFAULT 13 NOT NULL,
32    charfield CHAR(10) NULL,
33    primary key(artistid)
34 )
35
36 SQL
37
38 });
39
40 my %seen_id;
41
42 # fresh $schema so we start unconnected
43 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {AutoCommit => 1});
44
45 # test primary key handling
46 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
47 ok($new->artistid > 0, "Auto-PK worked");
48
49 $seen_id{$new->artistid}++;
50
51 # test LIMIT support
52 for (1..6) {
53     $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
54     is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
55     $seen_id{$new->artistid}++;
56 }
57
58 my $it;
59
60 $it = $schema->resultset('Artist')->search( {}, {
61     rows => 3,
62     order_by => 'artistid',
63 });
64
65 TODO: {
66     local $TODO = 'Sybase is very very fucked in the limit department';
67
68     is( $it->count, 3, "LIMIT count ok" );
69 }
70
71 # The iterator still works correctly with rows => 3, even though the sql is
72 # fucked, very interesting.
73
74 is( $it->next->name, "foo", "iterator->next ok" );
75 $it->next;
76 is( $it->next->name, "Artist 2", "iterator->next ok" );
77 is( $it->next, undef, "next past end of resultset ok" );
78
79
80 # clean up our mess
81 END {
82     my $dbh = eval { $schema->storage->_dbh };
83     $dbh->do('DROP TABLE artist') if $dbh;
84 }
85