Merge 'trunk' into 'sybase'
[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 use DBIx::Class::Storage::DBI::Sybase::DateTime;
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 => 15;
15
16 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, {AutoCommit => 1});
17
18 $schema->storage->ensure_connected;
19 isa_ok( $schema->storage, 'DBIx::Class::Storage::DBI::Sybase' );
20
21 $schema->storage->dbh_do (sub {
22     my ($storage, $dbh) = @_;
23     eval { $dbh->do("DROP TABLE artist") };
24     eval { $dbh->do("DROP TABLE track") };
25     $dbh->do(<<'SQL');
26 CREATE TABLE artist (
27    artistid INT IDENTITY PRIMARY KEY,
28    name VARCHAR(100),
29    rank INT DEFAULT 13 NOT NULL,
30    charfield CHAR(10) NULL
31 )
32 SQL
33
34 # we only need the DT
35     $dbh->do(<<'SQL');
36 CREATE TABLE track (
37    trackid INT IDENTITY PRIMARY KEY,
38    cd INT,
39    position INT,
40    last_updated_on DATETIME,
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 = $schema->resultset('Artist')->search( {}, {
65     rows => 3,
66     order_by => 'artistid',
67 });
68
69 TODO: {
70     local $TODO = 'Sybase is very very fucked in the limit department';
71
72     is( $it->count, 3, "LIMIT count ok" );
73 }
74
75 # The iterator still works correctly with rows => 3, even though the sql is
76 # fucked, very interesting.
77
78 is( $it->next->name, "foo", "iterator->next ok" );
79 $it->next;
80 is( $it->next->name, "Artist 2", "iterator->next ok" );
81 is( $it->next, undef, "next past end of resultset ok" );
82
83 # Test DateTime inflation
84
85 my $dt = DBIx::Class::Storage::DBI::Sybase::DateTime
86     ->parse_datetime('2004-08-21T14:36:48.080Z');
87
88 my $row;
89 ok( $row = $schema->resultset('Track')->create({
90     last_updated_on => $dt,
91     cd => 1,
92 }));
93 ok( $row = $schema->resultset('Track')
94     ->search({ trackid => $row->trackid }, { select => ['last_updated_on'] })
95     ->first
96 );
97 is( $row->updated_date, $dt, 'DateTime inflation works' );
98
99 # clean up our mess
100 END {
101     if (my $dbh = eval { $schema->storage->_dbh }) {
102         $dbh->do('DROP TABLE artist');
103         $dbh->do('DROP TABLE track');
104     }
105 }