add ::DBI::_is_integer_type and use for SQLite
[dbsrgits/DBIx-Class.git] / t / 752sqlite.t
CommitLineData
86a51471 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
632d1e0f 6use Test::Warn;
67b35a45 7use Config;
c50a1dbf 8use Try::Tiny;
67b35a45 9
86a51471 10use lib qw(t/lib);
11use DBICTest;
12
67b35a45 13# savepoints test
14{
15 my $schema = DBICTest->init_schema(auto_savepoint => 1);
86a51471 16
67b35a45 17 my $ars = $schema->resultset('Artist');
86a51471 18
67b35a45 19 # test two-phase commit and inner transaction rollback from nested transactions
86a51471 20 $schema->txn_do(sub {
67b35a45 21 $ars->create({ name => 'in_outer_transaction' });
86a51471 22 $schema->txn_do(sub {
67b35a45 23 $ars->create({ name => 'in_inner_transaction' });
86a51471 24 });
67b35a45 25 ok($ars->search({ name => 'in_inner_transaction' })->first,
26 'commit from inner transaction visible in outer transaction');
27 throws_ok {
28 $schema->txn_do(sub {
29 $ars->create({ name => 'in_inner_transaction_rolling_back' });
30 die 'rolling back inner transaction';
31 });
32 } qr/rolling back inner transaction/, 'inner transaction rollback executed';
33 $ars->create({ name => 'in_outer_transaction2' });
34 });
35
36 ok($ars->search({ name => 'in_outer_transaction' })->first,
37 'commit from outer transaction');
38 ok($ars->search({ name => 'in_outer_transaction2' })->first,
39 'second commit from outer transaction');
40 ok($ars->search({ name => 'in_inner_transaction' })->first,
41 'commit from inner transaction');
42 is $ars->search({ name => 'in_inner_transaction_rolling_back' })->first,
43 undef,
44 'rollback from inner transaction';
45}
632d1e0f 46
67b35a45 47my $schema = DBICTest->init_schema();
86a51471 48
632d1e0f 49# make sure the side-effects of RT#67581 do not result in data loss
50my $row;
67b35a45 51warnings_exist { $row = $schema->resultset('Artist')->create ({ name => 'alpha rank', rank => 'abc' }) }
632d1e0f 52 [qr/Non-numeric value supplied for column 'rank' despite the numeric datatype/],
53 'proper warning on string insertion into an numeric column'
54;
55$row->discard_changes;
56is ($row->rank, 'abc', 'proper rank inserted into database');
57
67b35a45 58# and make sure we do not lose actual bigints
59{
60 package DBICTest::BigIntArtist;
61 use base 'DBICTest::Schema::Artist';
62 __PACKAGE__->table('artist');
63 __PACKAGE__->add_column(bigint => { data_type => 'bigint' });
64}
65$schema->register_class(BigIntArtist => 'DBICTest::BigIntArtist');
66$schema->storage->dbh_do(sub {
67 $_[1]->do('ALTER TABLE artist ADD COLUMN bigint BIGINT');
68});
69
70# test upper/lower boundaries for sqlite and some values inbetween
71# range is -(2**63) .. 2**63 - 1
72for my $bi (qw/
73 -9223372036854775808
74 -9223372036854775807
75 -8694837494948124658
76 -6848440844435891639
77 -5664812265578554454
78 -5380388020020483213
79 -2564279463598428141
80 2442753333597784273
81 4790993557925631491
82 6773854980030157393
83 7627910776496326154
84 8297530189347439311
85 9223372036854775806
86 9223372036854775807
87/) {
c50a1dbf 88 lives_ok {
89 $row = $schema->resultset('BigIntArtist')->create({ bigint => $bi });
90 } 'inserted a bigint';
91 is (try { $row->bigint }, $bi, "value in object correct ($bi)");
67b35a45 92
93 TODO: {
94 local $TODO = 'This perl does not seem to have 64bit int support - DBI roundtrip of large int will fail'
95 unless $Config{ivsize} >= 8;
96
97 $row->discard_changes;
c50a1dbf 98 is (try { $row->bigint }, $bi, "value in database correct ($bi)");
67b35a45 99 }
100}
101
c50a1dbf 102my $artists_with_more_than_one_cd = $schema->resultset('Artist')->search({}, {
103 join => 'cds',
104 '+select' => [ { count => 'cds.cdid', -as => 'cd_count' } ],
105 '+as' => ['cd_count'],
106 group_by => ['me.artistid'],
107 having => [ { cd_count => { '>' => 1 } } ],
108});
109
110my %artist_cd_counts;
111
112lives_ok {
113 while (my $row = $artists_with_more_than_one_cd->next) {
114 $artist_cd_counts{ $row->name } = $row->get_column('cd_count');
115 }
116} 'HAVING int comparison query with a bind survived';
117
118ok ((keys %artist_cd_counts),
119 'HAVING int comparison query with a bind returned results');
120
86a51471 121done_testing;
122
123# vim:sts=2 sw=2: