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