Augment commit r5037 - the column_info feature on SQLite is not buggy but partially...
[dbsrgits/DBIx-Class.git] / t / 64db.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 my $schema = DBICTest->init_schema();
9
10 plan tests => 4;
11
12 # add some rows inside a transaction and commit it
13 # XXX: Is storage->dbh the only way to get a dbh?
14 $schema->storage->txn_begin;
15 for (10..15) {
16     $schema->resultset("Artist")->create( { 
17         artistid => $_,
18         name => "artist number $_",
19     } );
20 }
21 $schema->storage->txn_commit;
22 my ($artist) = $schema->resultset("Artist")->find(15);
23 is($artist->name, 'artist number 15', "Commit ok");
24
25 # add some rows inside a transaction and roll it back
26 $schema->storage->txn_begin;
27 for (21..30) {
28     $schema->resultset("Artist")->create( {
29         artistid => $_,
30         name => "artist number $_",
31     } );
32 }
33 $schema->storage->txn_rollback;
34 ($artist) = $schema->resultset("Artist")->search( artistid => 25 );
35 is($artist, undef, "Rollback ok");
36
37 is_deeply (
38   get_storage_column_info ($schema->storage, 'collection', qw/size is_nullable/),
39   {
40     collectionid => {
41       data_type => 'INTEGER',
42     },
43     name => {
44       data_type => 'varchar',
45     },
46   },
47   'Correctly retrieve column info (no size or is_nullable)'
48 );
49
50 TODO: {
51   local $TODO = 'All current versions of SQLite seem to mis-report is_nullable';
52
53   is_deeply (
54     get_storage_column_info ($schema->storage, 'artist', qw/size/),
55     {
56       'artistid' => {
57           'data_type' => 'INTEGER',
58           'is_nullable' => 0,
59       },
60       'name' => {
61           'data_type' => 'varchar',
62           'is_nullable' => 1,
63       },
64       'rank' => {
65           'data_type' => 'integer',
66           'is_nullable' => 0,
67       },
68     },
69     'Correctly retrieve column info (mixed null and non-null columns)'
70   );
71 };
72
73
74 # Depending on test we need to strip away certain column info.
75 #  - SQLite is known to report the size differently from release to release
76 #  - Current DBD::SQLite versions do not implement NULLABLE
77 #  - Some SQLite releases report stuff that isn't there as undef
78
79 sub get_storage_column_info {
80   my ($storage, $table, @ignore) = @_;
81
82   my $type_info = $storage->columns_info_for($table);
83
84   for my $col (keys %$type_info) {
85     for my $type (keys %{$type_info->{$col}}) {
86       if (
87         grep { $type eq $_ } (@ignore)
88           or
89         not defined $type_info->{$col}{$type}
90       ) {
91         delete $type_info->{$col}{$type};
92       }
93     }
94   }
95
96   return $type_info;
97 }