Merge 'trunk' into 'sybase'
[dbsrgits/DBIx-Class.git] / t / 746sybase.t
CommitLineData
a964a928 1use strict;
2use warnings;
3
4use Test::More;
e0b2344f 5use Test::Exception;
a964a928 6use lib qw(t/lib);
7use DBICTest;
c21c1eb6 8use DateTime::Format::Sybase;
a964a928 9
10my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
11
12plan skip_all => 'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test'
13 unless ($dsn && $user);
14
e0b2344f 15plan tests => (26 + 4*2)*2;
a964a928 16
6b1f5ef7 17my @storage_types = (
18 'DBI::Sybase',
19 'DBI::Sybase::NoBindVars',
20);
21my $schema;
22
23for my $storage_type (@storage_types) {
24 $schema = DBICTest::Schema->clone;
25
26 unless ($storage_type eq 'DBI::Sybase') { # autodetect
27 $schema->storage_type("::$storage_type");
28 }
29 $schema->connection($dsn, $user, $pass, {AutoCommit => 1});
a964a928 30
6b1f5ef7 31 $schema->storage->ensure_connected;
a964a928 32
6b1f5ef7 33 isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
34
35 $schema->storage->dbh_do (sub {
36 my ($storage, $dbh) = @_;
37 eval { $dbh->do("DROP TABLE artist") };
6b1f5ef7 38 $dbh->do(<<'SQL');
a964a928 39CREATE TABLE artist (
c5ce7cd6 40 artistid INT IDENTITY PRIMARY KEY,
a964a928 41 name VARCHAR(100),
42 rank INT DEFAULT 13 NOT NULL,
c5ce7cd6 43 charfield CHAR(10) NULL
a964a928 44)
c5ce7cd6 45SQL
6b1f5ef7 46 });
a964a928 47
6b1f5ef7 48 my %seen_id;
a964a928 49
6b1f5ef7 50# so we start unconnected
51 $schema->storage->disconnect;
a964a928 52
53# test primary key handling
6b1f5ef7 54 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
55 ok($new->artistid > 0, "Auto-PK worked");
a964a928 56
6b1f5ef7 57 $seen_id{$new->artistid}++;
a964a928 58
6b1f5ef7 59 for (1..6) {
a964a928 60 $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
61 is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
62 $seen_id{$new->artistid}++;
6b1f5ef7 63 }
a964a928 64
aa56ff9a 65# test simple count
66 is ($schema->resultset('Artist')->count, 7, 'count(*) of whole table ok');
67
68# test LIMIT support
e0b2344f 69 my $it = $schema->resultset('Artist')->search({
70 artistid => { '>' => 0 }
71 }, {
a964a928 72 rows => 3,
73 order_by => 'artistid',
6b1f5ef7 74 });
a964a928 75
b7505130 76 is( $it->count, 3, "LIMIT count ok" );
a964a928 77
6b1f5ef7 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" );
a964a928 82
a0348159 83# now try with offset
84 $it = $schema->resultset('Artist')->search({}, {
85 rows => 3,
86 offset => 3,
87 order_by => 'artistid',
88 });
89
90 is( $it->count, 3, "LIMIT with offset count ok" );
91
92 is( $it->next->name, "Artist 3", "iterator->next ok" );
93 $it->next;
94 is( $it->next->name, "Artist 5", "iterator->next ok" );
95 is( $it->next, undef, "next past end of resultset ok" );
96
92e7a79b 97# now try a grouped count
98 $schema->resultset('Artist')->create({ name => 'Artist 6' })
99 for (1..6);
100
101 $it = $schema->resultset('Artist')->search({}, {
102 group_by => 'name'
103 });
104
105 is( $it->count, 7, 'COUNT of GROUP_BY ok' );
106
d391697f 107# Test DateTime inflation with DATETIME
8242035e 108 my @dt_types = (
109 ['DATETIME', '2004-08-21T14:36:48.080Z'],
110 ['SMALLDATETIME', '2004-08-21T14:36:00.000Z'], # minute precision
111 );
112
113 for my $dt_type (@dt_types) {
114 my ($type, $sample_dt) = @$dt_type;
115
116 eval { $schema->storage->dbh->do("DROP TABLE track") };
117 $schema->storage->dbh->do(<<"SQL");
d391697f 118CREATE TABLE track (
119 trackid INT IDENTITY PRIMARY KEY,
120 cd INT,
121 position INT,
122 last_updated_on $type,
123)
124SQL
c21c1eb6 125 ok(my $dt = DateTime::Format::Sybase->parse_datetime($sample_dt));
8242035e 126
127 my $row;
128 ok( $row = $schema->resultset('Track')->create({
129 last_updated_on => $dt,
130 cd => 1,
131 }));
132 ok( $row = $schema->resultset('Track')
133 ->search({ trackid => $row->trackid }, { select => ['last_updated_on'] })
134 ->first
135 );
136 is( $row->updated_date, $dt, 'DateTime inflation works' );
6b1f5ef7 137 }
e0b2344f 138
6636ad53 139# mostly stole the blob stuff Nniuq wrote for t/73oracle.t
e0b2344f 140 my $dbh = $schema->storage->dbh;
141 {
142 local $SIG{__WARN__} = sub {};
143 eval { $dbh->do('DROP TABLE bindtype_test') };
144
145 $dbh->do(qq[
146 CREATE TABLE bindtype_test
147 (
148 id INT PRIMARY KEY,
149 bytea INT NULL,
150 blob IMAGE NULL,
151 clob TEXT NULL
152 )
153 ],{ RaiseError => 1, PrintError => 1 });
154 }
155
156 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
157 $binstr{'large'} = $binstr{'small'} x 1024;
158
159 my $maxloblen = length $binstr{'large'};
160 note "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
161 local $dbh->{'LongReadLen'} = $maxloblen;
162
163 my $rs = $schema->resultset('BindType');
164 my $id = 0;
165
bc54ca97 166 foreach my $type (qw(blob clob)) {
167 foreach my $size (qw(small large)) {
168 no warnings 'uninitialized';
169 $id++;
170
171 eval { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) };
172 ok(!$@, "inserted $size $type without dying");
173 diag $@ if $@;
174
175 ok(eval {
176 $rs->search({ id=> $id }, { select => [$type] })->single->$type
177 } eq $binstr{$size}, "verified inserted $size $type" );
e0b2344f 178 }
179 }
6b1f5ef7 180}
a964a928 181
182# clean up our mess
183END {
6b1f5ef7 184 if (my $dbh = eval { $schema->storage->_dbh }) {
185 $dbh->do('DROP TABLE artist');
186 $dbh->do('DROP TABLE track');
e0b2344f 187 $dbh->do('DROP TABLE bindtype_test');
6b1f5ef7 188 }
a964a928 189}