Fix failing test case missed during da9346a03
[dbsrgits/DBIx-Class.git] / t / 752sqlite.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
86a51471 3use strict;
4use warnings;
5
6use Test::More;
7use Test::Exception;
632d1e0f 8use Test::Warn;
2aeb3c7f 9use Time::HiRes 'time';
f3b1224b 10use Math::BigInt;
67b35a45 11
c0329273 12
86a51471 13use DBICTest;
d634850b 14use DBIx::Class::_Util qw( sigwarn_silencer modver_gt_or_eq modver_gt_or_eq_and_lt );
86a51471 15
da9346a0 16# make one deploy() round before we load anything else - need this in order
17# to prime SQLT if we are using it (deep depchain is deep)
18DBICTest->init_schema( no_populate => 1 );
19
2aeb3c7f 20# check that we work somewhat OK with braindead SQLite transaction handling
21#
22# As per https://metacpan.org/source/ADAMK/DBD-SQLite-1.37/lib/DBD/SQLite.pm#L921
23# SQLite does *not* try to synchronize
ab0b0a09 24#
25# However DBD::SQLite 1.38_02 seems to fix this, with an accompanying test:
26# https://metacpan.org/source/ADAMK/DBD-SQLite-1.38_02/t/54_literal_txn.t
b1dbf716 27my $lit_txn_todo = modver_gt_or_eq('DBD::SQLite', '1.38_02')
ab0b0a09 28 ? undef
29 : "DBD::SQLite before 1.38_02 is retarded wrt detecting literal BEGIN/COMMIT statements"
30;
2aeb3c7f 31
32for my $prefix_comment (qw/Begin_only Commit_only Begin_and_Commit/) {
33 note "Testing with comment prefixes on $prefix_comment";
34
35 # FIXME warning won't help us for the time being
36 # perhaps when (if ever) DBD::SQLite gets fixed,
37 # we can do something extra here
052a832c 38 local $SIG{__WARN__} = sigwarn_silencer( qr/Internal transaction state .+? does not seem to match/ )
ab0b0a09 39 if ( $lit_txn_todo && !$ENV{TEST_VERBOSE} );
2aeb3c7f 40
41 my ($c_begin, $c_commit) = map { $prefix_comment =~ $_ ? 1 : 0 } (qr/Begin/, qr/Commit/);
42
43 my $schema = DBICTest->init_schema( no_deploy => 1 );
44 my $ars = $schema->resultset('Artist');
45
46 ok (! $schema->storage->connected, 'No connection yet');
47
48 $schema->storage->dbh->do(<<'DDL');
49CREATE TABLE artist (
50 artistid INTEGER PRIMARY KEY NOT NULL,
51 name varchar(100),
52 rank integer DEFAULT 13,
53 charfield char(10) NULL
54);
55DDL
56
57 my $artist = $ars->create({ name => 'Artist_' . time() });
58 is ($ars->count, 1, 'Inserted artist ' . $artist->name);
59
60 ok ($schema->storage->connected, 'Connected');
61 ok ($schema->storage->_dbh->{AutoCommit}, 'DBD not in txn yet');
62
63 $schema->storage->dbh->do(join "\n",
64 $c_begin ? '-- comment' : (),
65 'BEGIN TRANSACTION'
66 );
67 ok ($schema->storage->connected, 'Still connected');
68 {
ab0b0a09 69 local $TODO = $lit_txn_todo if $c_begin;
2aeb3c7f 70 ok (! $schema->storage->_dbh->{AutoCommit}, "DBD aware of txn begin with comments on $prefix_comment");
71 }
72
73 $schema->storage->dbh->do(join "\n",
74 $c_commit ? '-- comment' : (),
75 'COMMIT'
76 );
77 ok ($schema->storage->connected, 'Still connected');
78 {
ab0b0a09 79 local $TODO = $lit_txn_todo if $c_commit and ! $c_begin;
2aeb3c7f 80 ok ($schema->storage->_dbh->{AutoCommit}, "DBD aware txn ended with comments on $prefix_comment");
81 }
82
83 is ($ars->count, 1, 'Inserted artists still there');
84
85 {
86 # this never worked in the 1st place
ab0b0a09 87 local $TODO = $lit_txn_todo if ! $c_begin and $c_commit;
2aeb3c7f 88
08615cfb 89 # odd argument passing, because such nested crefs leak on 5.8
2aeb3c7f 90 lives_ok {
91 $schema->storage->txn_do (sub {
08615cfb 92 ok ($_[0]->find({ name => $_[1] }), "Artist still where we left it after cycle with comments on $prefix_comment");
93 }, $ars, $artist->name );
2aeb3c7f 94 } "Succesfull transaction with comments on $prefix_comment";
95 }
96}
97
398215b1 98# test blank begin/svp/commit/begin cycle
99warnings_are {
100 my $schema = DBICTest->init_schema( no_populate => 1 );
101 my $rs = $schema->resultset('Artist');
102 is ($rs->count, 0, 'Start with empty table');
103
104 for my $do_commit (1, 0) {
105 $schema->txn_begin;
106 $schema->svp_begin;
107 $schema->svp_rollback;
108
109 $schema->svp_begin;
110 $schema->svp_rollback;
111
112 $schema->svp_release;
113
114 $schema->svp_begin;
115
116 $schema->txn_rollback;
117
118 $schema->txn_begin;
119 $schema->svp_begin;
120 $schema->svp_rollback;
121
122 $schema->svp_begin;
123 $schema->svp_rollback;
124
125 $schema->svp_release;
126
127 $schema->svp_begin;
128
129 $do_commit ? $schema->txn_commit : $schema->txn_rollback;
130
131 is_deeply $schema->storage->savepoints, [], 'Savepoint names cleared away'
132 }
133
134 $schema->txn_do(sub {
135 ok (1, 'all seems fine');
136 });
137} [], 'No warnings emitted';
2aeb3c7f 138
67b35a45 139my $schema = DBICTest->init_schema();
86a51471 140
632d1e0f 141# make sure the side-effects of RT#67581 do not result in data loss
142my $row;
67b35a45 143warnings_exist { $row = $schema->resultset('Artist')->create ({ name => 'alpha rank', rank => 'abc' }) }
445bc0cd 144 [qr/Non-integer value supplied for column 'rank' despite the integer datatype/],
632d1e0f 145 'proper warning on string insertion into an numeric column'
146;
147$row->discard_changes;
148is ($row->rank, 'abc', 'proper rank inserted into database');
149
67b35a45 150# and make sure we do not lose actual bigints
83e81c2d 151SKIP: {
152
153skip "Not testing bigint handling on known broken DBD::SQLite trial versions", 1
154 if modver_gt_or_eq_and_lt( 'DBD::SQLite', '1.45', '1.45_03' );
155
67b35a45 156{
157 package DBICTest::BigIntArtist;
158 use base 'DBICTest::Schema::Artist';
159 __PACKAGE__->table('artist');
160 __PACKAGE__->add_column(bigint => { data_type => 'bigint' });
161}
162$schema->register_class(BigIntArtist => 'DBICTest::BigIntArtist');
163$schema->storage->dbh_do(sub {
164 $_[1]->do('ALTER TABLE artist ADD COLUMN bigint BIGINT');
165});
166
d634850b 167my $sqlite_broken_bigint = modver_gt_or_eq_and_lt( 'DBD::SQLite', '1.34', '1.37' );
f3b1224b 168
169# 63 bit integer
170my $many_bits = (Math::BigInt->new(2) ** 62);
171
67b35a45 172# test upper/lower boundaries for sqlite and some values inbetween
173# range is -(2**63) .. 2**63 - 1
f3b1224b 174#
175# Not testing -0 - it seems to overflow to ~0 on some combinations,
176# thus not triggering the >32 bit guards
177# interesting read: https://en.wikipedia.org/wiki/Signed_zero#Representations
04ab4eb1 178for my $bi ( qw(
f3b1224b 179 -2
180 -1
181 0
182 +0
183 1
184 2
185
04ab4eb1 186 -9223372036854775807
187 -8694837494948124658
188 -6848440844435891639
189 -5664812265578554454
190 -5380388020020483213
191 -2564279463598428141
192 2442753333597784273
193 4790993557925631491
194 6773854980030157393
195 7627910776496326154
196 8297530189347439311
197 9223372036854775806
198 9223372036854775807
199
200 4294967295
201 4294967296
202
203 -4294967296
204 -4294967295
205 -4294967294
206
207 -2147483649
208 -2147483648
209 -2147483647
210 -2147483646
211
212 2147483646
213 2147483647
214),
215 # these values cause exceptions even with all workarounds in place on these
216 # fucked DBD::SQLite versions *regardless* of ivsize >.<
f3b1224b 217 $sqlite_broken_bigint
04ab4eb1 218 ? ()
219 : ( '2147483648', '2147483649' )
7e763495 220 ,
221
222 # with newer compilers ( gcc 4.9+ ) older DBD::SQLite does not
223 # play well with the "Most Negative Number"
224 modver_gt_or_eq( 'DBD::SQLite', '1.33' )
225 ? ( '-9223372036854775808' )
226 : ()
227 ,
228
04ab4eb1 229) {
230 # unsigned 32 bit ints have a range of −2,147,483,648 to 2,147,483,647
231 # alternatively expressed as the hexadecimal numbers below
232 # the comparison math will come out right regardless of ivsize, since
233 # we are operating within 31 bits
234 # P.S. 31 because one bit is lost for the sign
235 my $v_bits = ($bi > 0x7fff_ffff || $bi < -0x8000_0000) ? 64 : 32;
236
237 my $v_desc = sprintf '%s (%d bit signed int)', $bi, $v_bits;
238
1363f0f5 239 my @w;
113322e5 240 local $SIG{__WARN__} = sub {
241 if ($_[0] =~ /datatype mismatch/) {
242 push @w, @_;
243 }
244 elsif ($_[0] =~ /An integer value occupying more than 32 bits was supplied .+ can not bind properly so DBIC will treat it as a string instead/ ) {
245 # do nothing, this warning will pop up here and there depending on
246 # DBD/bitness combination
247 # we don't want to test for it explicitly, we are just interested
248 # in the results matching at the end
249 }
250 else {
251 warn @_;
252 }
253 };
1363f0f5 254
f3b1224b 255 # some combinations of SQLite 1.35 and older 5.8 faimly is wonky
256 # instead of a warning we get a full exception. Sod it
257 eval {
2e092442 258 $row = $schema->resultset('BigIntArtist')->create({ bigint => $bi });
f3b1224b 259 } or do {
7e763495 260 fail("Exception on inserting $v_desc: $@") unless $sqlite_broken_bigint;
f3b1224b 261 next;
262 };
04ab4eb1 263
264 # explicitly using eq, to make sure we did not nummify the argument
265 # which can be an issue on 32 bit ivsize
266 cmp_ok ($row->bigint, 'eq', $bi, "value in object correct ($v_desc)");
267
268 $row->discard_changes;
269
270 cmp_ok (
271 $row->bigint,
272
273 # the test will not pass an == if we are running under 32 bit ivsize
274 # use 'eq' on the numified (and possibly "scientificied") returned value
f3b1224b 275 (DBIx::Class::_ENV_::IV_SIZE < 8 and $v_bits > 32) ? 'eq' : '==',
04ab4eb1 276
277 # in 1.37 DBD::SQLite switched to proper losless representation of bigints
278 # regardless of ivize
279 # before this use 'eq' (from above) on the numified (and possibly
280 # "scientificied") returned value
281 (DBIx::Class::_ENV_::IV_SIZE < 8 and ! modver_gt_or_eq('DBD::SQLite', '1.37')) ? $bi+0 : $bi,
282
283 "value in database correct ($v_desc)"
284 );
1363f0f5 285
f3b1224b 286 # check if math works
287 # start by adding/subtracting a 50 bit integer, and then divide by 2 for good measure
288 my ($sqlop, $expect) = $bi < 0
289 ? ( '(bigint + ? )', ($bi + $many_bits) )
290 : ( '(bigint - ? )', ($bi - $many_bits) )
291 ;
292
293 $expect = ($expect + ($expect % 2)) / 2;
294
295 # read https://en.wikipedia.org/wiki/Modulo_operation#Common_pitfalls
296 # and check the tables on the right side of the article for an
297 # enlightening journey on why a mere bigint % 2 won't work
298 $sqlop = "( $sqlop + ( ((bigint % 2)+2)%2 ) ) / 2";
299
300 for my $dtype (undef, \'int', \'bigint') {
301
302 # FIXME - the double-load should not be needed
303 # will fix in the future
304 $row->update({ bigint => $bi });
305 $row->discard_changes;
306 $row->update({ bigint => \[ $sqlop, [ $dtype => $many_bits ] ] });
307 $row->discard_changes;
308
309 # can't use cmp_ok - will not engage the M::BI overload of $many_bits
310 ok (
311 $row->bigint
312
313 ==
314
315 (DBIx::Class::_ENV_::IV_SIZE < 8 and ! modver_gt_or_eq('DBD::SQLite', '1.37')) ? $expect->bstr + 0 : $expect
316 , "simple integer math with@{[ $dtype ? '' : 'out' ]} bindtype in database correct (base $v_desc)")
317 or diag sprintf '%s != %s', $row->bigint, $expect;
318 }
f3b1224b 319
320 is_deeply (\@w, [], "No mismatch warnings on bigint operations ($v_desc)" );
83e81c2d 321
322}}
67b35a45 323
86a51471 324done_testing;
325
326# vim:sts=2 sw=2: