Merge 'trunk' into 'sybase'
[dbsrgits/DBIx-Class-Historic.git] / t / 746sybase.t
CommitLineData
a964a928 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
c5ce7cd6 7use DBIx::Class::Storage::DBI::Sybase::DateTime;
a964a928 8
9my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
10
11plan skip_all => 'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test'
12 unless ($dsn && $user);
13
c5ce7cd6 14plan tests => 15;
a964a928 15
16my $schema = DBICTest::Schema->connect($dsn, $user, $pass, {AutoCommit => 1});
17
18$schema->storage->ensure_connected;
19isa_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") };
c5ce7cd6 24 eval { $dbh->do("DROP TABLE track") };
a964a928 25 $dbh->do(<<'SQL');
a964a928 26CREATE TABLE artist (
c5ce7cd6 27 artistid INT IDENTITY PRIMARY KEY,
a964a928 28 name VARCHAR(100),
29 rank INT DEFAULT 13 NOT NULL,
c5ce7cd6 30 charfield CHAR(10) NULL
a964a928 31)
c5ce7cd6 32SQL
a964a928 33
c5ce7cd6 34# we only need the DT
35 $dbh->do(<<'SQL');
36CREATE TABLE track (
37 trackid INT IDENTITY PRIMARY KEY,
38 cd INT,
39 position INT,
40 last_updated_on DATETIME,
41)
a964a928 42SQL
43
44});
45
46my %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
52my $new = $schema->resultset('Artist')->create({ name => 'foo' });
53ok($new->artistid > 0, "Auto-PK worked");
54
55$seen_id{$new->artistid}++;
56
57# test LIMIT support
58for (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
c5ce7cd6 64my $it = $schema->resultset('Artist')->search( {}, {
a964a928 65 rows => 3,
66 order_by => 'artistid',
67});
68
69TODO: {
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
78is( $it->next->name, "foo", "iterator->next ok" );
79$it->next;
80is( $it->next->name, "Artist 2", "iterator->next ok" );
81is( $it->next, undef, "next past end of resultset ok" );
82
c5ce7cd6 83# Test DateTime inflation
84
85my $dt = DBIx::Class::Storage::DBI::Sybase::DateTime
86 ->parse_datetime('2004-08-21T14:36:48.080Z');
87
88my $row;
89ok( $row = $schema->resultset('Track')->create({
90 last_updated_on => $dt,
91 cd => 1,
92}));
93ok( $row = $schema->resultset('Track')
94 ->search({ trackid => $row->trackid }, { select => ['last_updated_on'] })
95 ->first
96);
97is( $row->updated_date, $dt, 'DateTime inflation works' );
a964a928 98
99# clean up our mess
100END {
c5ce7cd6 101 if (my $dbh = eval { $schema->storage->_dbh }) {
102 $dbh->do('DROP TABLE artist');
103 $dbh->do('DROP TABLE track');
104 }
a964a928 105}