Merge 'ic_dt_post_inflate' into 'trunk'
[dbsrgits/DBIx-Class.git] / t / 749sybase_asa.t
CommitLineData
f200d74b 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6use lib qw(t/lib);
7use DBICTest;
8
b341186f 9# tests stolen from 748informix.t
f200d74b 10
b341186f 11my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_ASA_${_}" } qw/DSN USER PASS/};
f200d74b 12
13plan skip_all => 'Set $ENV{DBICTEST_SYBASE_ASA_DSN}, _USER and _PASS to run this test'
b341186f 14 unless ($dsn);
f200d74b 15
16my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
17
18my $dbh = $schema->storage->dbh;
19
20eval { $dbh->do("DROP TABLE artist") };
21
ed720bc5 22$dbh->do(<<EOF);
23CREATE TABLE artist (
24 artistid INT IDENTITY PRIMARY KEY,
25 name VARCHAR(255) NULL,
26 charfield CHAR(10) NULL,
27 rank INT DEFAULT 13
28)
29EOF
f200d74b 30
31my $ars = $schema->resultset('Artist');
32is ( $ars->count, 0, 'No rows at first' );
33
34# test primary key handling
35my $new = $ars->create({ name => 'foo' });
36ok($new->artistid, "Auto-PK worked");
37
38# test explicit key spec
39$new = $ars->create ({ name => 'bar', artistid => 66 });
40is($new->artistid, 66, 'Explicit PK worked');
41$new->discard_changes;
42is($new->artistid, 66, 'Explicit PK assigned');
43
44# test populate
45lives_ok (sub {
46 my @pop;
47 for (1..2) {
48 push @pop, { name => "Artist_$_" };
49 }
50 $ars->populate (\@pop);
51});
52
53# test populate with explicit key
54lives_ok (sub {
55 my @pop;
56 for (1..2) {
57 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
58 }
59 $ars->populate (\@pop);
60});
61
62# count what we did so far
63is ($ars->count, 6, 'Simple count works');
64
65# test LIMIT support
66my $lim = $ars->search( {},
67 {
68 rows => 3,
69 offset => 4,
70 order_by => 'artistid'
71 }
72);
73is( $lim->count, 2, 'ROWS+OFFSET count ok' );
74is( $lim->all, 2, 'Number of ->all objects matches count' );
75
76# test iterator
77$lim->reset;
78is( $lim->next->artistid, 101, "iterator->next ok" );
79is( $lim->next->artistid, 102, "iterator->next ok" );
80is( $lim->next, undef, "next past end of resultset ok" );
81
ed720bc5 82# test empty insert
83{
84 local $ars->result_source->column_info('artistid')->{is_auto_increment} = 0;
85
86 lives_ok { $ars->create({}) }
87 'empty insert works';
88}
89
b341186f 90# test blobs (stolen from 73oracle.t)
91eval { $dbh->do('DROP TABLE bindtype_test') };
92$dbh->do(qq[
93CREATE TABLE bindtype_test
94(
95 id INT NOT NULL PRIMARY KEY,
96 bytea INT NULL,
97 blob LONG BINARY NULL,
98 clob LONG VARCHAR NULL
99)
100],{ RaiseError => 1, PrintError => 1 });
101
102my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
103$binstr{'large'} = $binstr{'small'} x 1024;
104
105my $maxloblen = length $binstr{'large'};
106local $dbh->{'LongReadLen'} = $maxloblen;
107
108my $rs = $schema->resultset('BindType');
109my $id = 0;
110
111foreach my $type (qw( blob clob )) {
112 foreach my $size (qw( small large )) {
113 $id++;
114
ed720bc5 115# turn off horrendous binary DBIC_TRACE output
116 local $schema->storage->{debug} = 0;
117
b341186f 118 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
119 "inserted $size $type without dying";
120
121 ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" );
122 }
123}
f200d74b 124
125done_testing;
126
127# clean up our mess
128END {
ed720bc5 129 if (my $dbh = eval { $schema->storage->_dbh }) {
130 $dbh->do("DROP TABLE $_") for qw/artist bindtype_test/;
131 }
f200d74b 132}