Commit | Line | Data |
a964a928 |
1 | use strict; |
2 | use warnings; |
b0b44f97 |
3 | no warnings 'uninitialized'; |
a964a928 |
4 | |
5 | use Test::More; |
e0b2344f |
6 | use Test::Exception; |
a964a928 |
7 | use lib qw(t/lib); |
8 | use DBICTest; |
9 | |
10 | my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/}; |
11 | |
b5453fbb |
12 | my $TESTS = 29 + 2; |
5703eb14 |
13 | |
7d17f469 |
14 | if (not ($dsn && $user)) { |
15 | plan skip_all => |
16 | 'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test' . |
17 | "\nWarning: This test drops and creates the tables " . |
18 | "'artist' and 'bindtype_test'"; |
19 | } else { |
5703eb14 |
20 | plan tests => $TESTS*2; |
7d17f469 |
21 | } |
a964a928 |
22 | |
6b1f5ef7 |
23 | my @storage_types = ( |
24 | 'DBI::Sybase', |
25 | 'DBI::Sybase::NoBindVars', |
26 | ); |
27 | my $schema; |
5703eb14 |
28 | my $storage_idx = -1; |
6b1f5ef7 |
29 | |
30 | for my $storage_type (@storage_types) { |
5703eb14 |
31 | $storage_idx++; |
61cfaef7 |
32 | # this is so we can set ->storage_type before connecting |
33 | my $schema = DBICTest::Schema->clone; |
6b1f5ef7 |
34 | |
35 | unless ($storage_type eq 'DBI::Sybase') { # autodetect |
36 | $schema->storage_type("::$storage_type"); |
37 | } |
61cfaef7 |
38 | |
b25da5cf |
39 | $schema->connection($dsn, $user, $pass, { |
40 | AutoCommit => 1, |
41 | on_connect_call => [ |
b25da5cf |
42 | [ blob_setup => log_on_update => 1 ], # this is a safer option |
43 | ], |
44 | }); |
a964a928 |
45 | |
6b1f5ef7 |
46 | $schema->storage->ensure_connected; |
a964a928 |
47 | |
5703eb14 |
48 | if ($storage_idx == 0 && |
49 | $schema->storage->isa('DBIx::Class::Storage::DBI::Sybase::NoBindVars')) { |
8c4b6c50 |
50 | # no placeholders in this version of Sybase or DBD::Sybase (or using FreeTDS) |
5703eb14 |
51 | my $tb = Test::More->builder; |
52 | $tb->skip('no placeholders') for 1..$TESTS; |
53 | next; |
54 | } |
55 | |
6b1f5ef7 |
56 | isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" ); |
57 | |
61cfaef7 |
58 | $schema->storage->_dbh->disconnect; |
64f4e691 |
59 | lives_ok (sub { $schema->storage->dbh }, 'reconnect works'); |
60 | |
6b1f5ef7 |
61 | $schema->storage->dbh_do (sub { |
62 | my ($storage, $dbh) = @_; |
63 | eval { $dbh->do("DROP TABLE artist") }; |
6b1f5ef7 |
64 | $dbh->do(<<'SQL'); |
a964a928 |
65 | CREATE TABLE artist ( |
c5ce7cd6 |
66 | artistid INT IDENTITY PRIMARY KEY, |
a964a928 |
67 | name VARCHAR(100), |
68 | rank INT DEFAULT 13 NOT NULL, |
c5ce7cd6 |
69 | charfield CHAR(10) NULL |
a964a928 |
70 | ) |
c5ce7cd6 |
71 | SQL |
6b1f5ef7 |
72 | }); |
a964a928 |
73 | |
6b1f5ef7 |
74 | my %seen_id; |
a964a928 |
75 | |
6b1f5ef7 |
76 | # so we start unconnected |
77 | $schema->storage->disconnect; |
a964a928 |
78 | |
79 | # test primary key handling |
6b1f5ef7 |
80 | my $new = $schema->resultset('Artist')->create({ name => 'foo' }); |
81 | ok($new->artistid > 0, "Auto-PK worked"); |
a964a928 |
82 | |
6b1f5ef7 |
83 | $seen_id{$new->artistid}++; |
a964a928 |
84 | |
6b1f5ef7 |
85 | for (1..6) { |
a964a928 |
86 | $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ }); |
87 | is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" ); |
88 | $seen_id{$new->artistid}++; |
6b1f5ef7 |
89 | } |
a964a928 |
90 | |
aa56ff9a |
91 | # test simple count |
92 | is ($schema->resultset('Artist')->count, 7, 'count(*) of whole table ok'); |
93 | |
94 | # test LIMIT support |
e0b2344f |
95 | my $it = $schema->resultset('Artist')->search({ |
96 | artistid => { '>' => 0 } |
97 | }, { |
a964a928 |
98 | rows => 3, |
99 | order_by => 'artistid', |
6b1f5ef7 |
100 | }); |
a964a928 |
101 | |
b7505130 |
102 | is( $it->count, 3, "LIMIT count ok" ); |
a964a928 |
103 | |
6b1f5ef7 |
104 | is( $it->next->name, "foo", "iterator->next ok" ); |
105 | $it->next; |
106 | is( $it->next->name, "Artist 2", "iterator->next ok" ); |
107 | is( $it->next, undef, "next past end of resultset ok" ); |
a964a928 |
108 | |
a0348159 |
109 | # now try with offset |
110 | $it = $schema->resultset('Artist')->search({}, { |
111 | rows => 3, |
112 | offset => 3, |
113 | order_by => 'artistid', |
114 | }); |
115 | |
116 | is( $it->count, 3, "LIMIT with offset count ok" ); |
117 | |
118 | is( $it->next->name, "Artist 3", "iterator->next ok" ); |
119 | $it->next; |
120 | is( $it->next->name, "Artist 5", "iterator->next ok" ); |
121 | is( $it->next, undef, "next past end of resultset ok" ); |
122 | |
92e7a79b |
123 | # now try a grouped count |
124 | $schema->resultset('Artist')->create({ name => 'Artist 6' }) |
125 | for (1..6); |
126 | |
127 | $it = $schema->resultset('Artist')->search({}, { |
128 | group_by => 'name' |
129 | }); |
130 | |
131 | is( $it->count, 7, 'COUNT of GROUP_BY ok' ); |
132 | |
5703eb14 |
133 | # mostly stolen from the blob stuff Nniuq wrote for t/73oracle.t |
134 | SKIP: { |
8c4b6c50 |
135 | skip 'TEXT/IMAGE support does not work with FreeTDS', 12 |
136 | if $schema->storage->_using_freetds; |
5703eb14 |
137 | |
138 | my $dbh = $schema->storage->dbh; |
139 | { |
140 | local $SIG{__WARN__} = sub {}; |
141 | eval { $dbh->do('DROP TABLE bindtype_test') }; |
142 | |
143 | $dbh->do(qq[ |
144 | CREATE TABLE bindtype_test |
145 | ( |
146 | id INT IDENTITY PRIMARY KEY, |
147 | bytea INT NULL, |
148 | blob IMAGE NULL, |
149 | clob TEXT NULL |
150 | ) |
151 | ],{ RaiseError => 1, PrintError => 0 }); |
152 | } |
e0b2344f |
153 | |
5703eb14 |
154 | my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) ); |
155 | $binstr{'large'} = $binstr{'small'} x 1024; |
e0b2344f |
156 | |
5703eb14 |
157 | my $maxloblen = length $binstr{'large'}; |
158 | note |
159 | "Localizing LongReadLen to $maxloblen to avoid truncation of test data"; |
078a332f |
160 | local $dbh->{'LongReadLen'} = $maxloblen * 2; |
e0b2344f |
161 | |
5703eb14 |
162 | my $rs = $schema->resultset('BindType'); |
163 | my $last_id; |
e0b2344f |
164 | |
5703eb14 |
165 | foreach my $type (qw(blob clob)) { |
166 | foreach my $size (qw(small large)) { |
167 | no warnings 'uninitialized'; |
bc54ca97 |
168 | |
5703eb14 |
169 | my $created = eval { $rs->create( { $type => $binstr{$size} } ) }; |
170 | ok(!$@, "inserted $size $type without dying"); |
171 | diag $@ if $@; |
172 | |
173 | $last_id = $created->id if $created; |
174 | |
175 | my $got = eval { |
23419345 |
176 | $rs->find($last_id)->$type |
5703eb14 |
177 | }; |
178 | diag $@ if $@; |
179 | ok($got eq $binstr{$size}, "verified inserted $size $type"); |
180 | } |
181 | } |
bc54ca97 |
182 | |
5703eb14 |
183 | # blob insert with explicit PK |
289877b0 |
184 | # also a good opportunity to test IDENTITY_INSERT |
5703eb14 |
185 | { |
186 | local $SIG{__WARN__} = sub {}; |
187 | eval { $dbh->do('DROP TABLE bindtype_test') }; |
188 | |
189 | $dbh->do(qq[ |
190 | CREATE TABLE bindtype_test |
191 | ( |
289877b0 |
192 | id INT IDENTITY PRIMARY KEY, |
5703eb14 |
193 | bytea INT NULL, |
194 | blob IMAGE NULL, |
195 | clob TEXT NULL |
196 | ) |
197 | ],{ RaiseError => 1, PrintError => 0 }); |
198 | } |
199 | my $created = eval { $rs->create( { id => 1, blob => $binstr{large} } ) }; |
078a332f |
200 | ok(!$@, "inserted large blob without dying with manual PK"); |
5703eb14 |
201 | diag $@ if $@; |
7d17f469 |
202 | |
5703eb14 |
203 | my $got = eval { |
23419345 |
204 | $rs->find(1)->blob |
5703eb14 |
205 | }; |
7d17f469 |
206 | diag $@ if $@; |
078a332f |
207 | ok($got eq $binstr{large}, "verified inserted large blob with manual PK"); |
208 | |
209 | # try a blob update |
210 | my $new_str = $binstr{large} . 'mtfnpy'; |
211 | eval { $rs->search({ id => 1 })->update({ blob => $new_str }) }; |
212 | ok !$@, 'updated blob successfully'; |
213 | diag $@ if $@; |
214 | $got = eval { |
215 | $rs->find(1)->blob |
216 | }; |
217 | diag $@ if $@; |
218 | ok($got eq $new_str, "verified updated blob"); |
9b3dabe0 |
219 | } |
6b1f5ef7 |
220 | } |
a964a928 |
221 | |
222 | # clean up our mess |
223 | END { |
6b1f5ef7 |
224 | if (my $dbh = eval { $schema->storage->_dbh }) { |
225 | $dbh->do('DROP TABLE artist'); |
5703eb14 |
226 | eval { $dbh->do('DROP TABLE bindtype_test') }; |
6b1f5ef7 |
227 | } |
a964a928 |
228 | } |