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