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