add ::DBI::_is_integer_type and use for SQLite
[dbsrgits/DBIx-Class.git] / t / 752sqlite.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use Test::Warn;
7 use Config;
8 use Try::Tiny;
9
10 use lib qw(t/lib);
11 use DBICTest;
12
13 # savepoints test
14 {
15   my $schema = DBICTest->init_schema(auto_savepoint => 1);
16
17   my $ars = $schema->resultset('Artist');
18
19   # test two-phase commit and inner transaction rollback from nested transactions
20   $schema->txn_do(sub {
21     $ars->create({ name => 'in_outer_transaction' });
22     $schema->txn_do(sub {
23       $ars->create({ name => 'in_inner_transaction' });
24     });
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 }
46
47 my $schema = DBICTest->init_schema();
48
49 # make sure the side-effects of RT#67581 do not result in data loss
50 my $row;
51 warnings_exist { $row = $schema->resultset('Artist')->create ({ name => 'alpha rank', rank => 'abc' }) }
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;
56 is ($row->rank, 'abc', 'proper rank inserted into database');
57
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
72 for 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 /) {
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)");
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;
98     is (try { $row->bigint }, $bi, "value in database correct ($bi)");
99   }
100 }
101
102 my $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
110 my %artist_cd_counts;
111
112 lives_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
118 ok ((keys %artist_cd_counts),
119   'HAVING int comparison query with a bind returned results');
120
121 done_testing;
122
123 # vim:sts=2 sw=2: