Convert the tests with multiple DBDs to new optdeps system
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 10_09firebird_common.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use Scope::Guard ();
5 use DBIx::Class::Schema::Loader::Optional::Dependencies;
6 use DBIx::Class::Schema::Loader::Utils qw/sigwarn_silencer/;
7 use lib qw(t/lib);
8 use dbixcsl_common_tests;
9
10 my %env2optdep = map { $_ => lc "test_rdbms_$_" } qw(FIREBIRD FIREBIRD_INTERBASE FIREBIRD_ODBC);
11
12 plan skip_all => 'requirements not satisfied:  ' . (join '  OR  ', map
13     { "[ @{[ DBIx::Class::Schema::Loader::Optional::Dependencies->req_missing_for( $_ ) ]} ]" }
14     values %env2optdep
15 ) unless scalar grep
16     { DBIx::Class::Schema::Loader::Optional::Dependencies->req_ok_for( $_ ) }
17     values %env2optdep
18 ;
19
20 my %dsns;
21 for my $type (keys %env2optdep) {
22     my %conninfo;
23     @conninfo{qw(dsn user password)} = map { $ENV{"DBICTEST_${type}_$_"} } qw(DSN USER PASS);
24     next unless $conninfo{dsn};
25
26     my $dep_group = $env2optdep{$type};
27     if (!DBIx::Class::Schema::Loader::Optional::Dependencies->req_ok_for($dep_group)) {
28         diag "Testing with DBICTEST_${type}_DSN needs " . DBIx::Class::Schema::Loader::Optional::Dependencies->req_missing_for($dep_group);
29         next;
30     }
31
32     $dsns{$type} = \%conninfo;
33     $dsns{$type}{connect_info_opts} = { on_connect_call => 'use_softcommit' }
34         if $type =~ /\AFIREBIRD(?:_INTERBASE)?\z/;
35 };
36
37 my $schema;
38
39 my $tester = dbixcsl_common_tests->new(
40     vendor      => 'Firebird',
41     auto_inc_pk => 'INTEGER NOT NULL PRIMARY KEY',
42     auto_inc_cb => sub {
43         my ($table, $col) = @_;
44         return (
45             qq{ CREATE GENERATOR gen_${table}_${col} },
46             qq{
47                 CREATE TRIGGER ${table}_bi FOR $table
48                 ACTIVE BEFORE INSERT POSITION 0
49                 AS
50                 BEGIN
51                  IF (NEW.$col IS NULL) THEN
52                   NEW.$col = GEN_ID(gen_${table}_${col},1);
53                 END
54             }
55         );
56     },
57     auto_inc_drop_cb => sub {
58         my ($table, $col) = @_;
59         return (
60             qq{ DROP TRIGGER ${table}_bi },
61             qq{ DROP GENERATOR gen_${table}_${col} },
62         );
63     },
64     null        => '',
65     preserve_case_mode_is_exclusive => 1,
66     quote_char                      => '"',
67     connect_info => [ map { $dsns{$_} } sort keys %dsns ],
68     data_types  => {
69         # based on the Interbase Data Definition Guide
70         # http://www.ibphoenix.com/downloads/60DataDef.zip
71         #
72         # Numeric types
73         'smallint'    => { data_type => 'smallint' },
74         'int'         => { data_type => 'integer' },
75         'integer'     => { data_type => 'integer' },
76         'bigint'      => { data_type => 'bigint' },
77         'float'       => { data_type => 'real' },
78         'double precision' =>
79                          { data_type => 'double precision' },
80         'real'        => { data_type => 'real' },
81
82         'float(2)'    => { data_type => 'real' },
83         'float(7)'    => { data_type => 'real' },
84         'float(8)'    => { data_type => 'double precision' },
85
86         'decimal'     => { data_type => 'decimal' },
87         'dec'         => { data_type => 'decimal' },
88         'numeric'     => { data_type => 'numeric' },
89
90         'decimal(3)'   => { data_type => 'decimal', size => [3,0] },
91
92         'decimal(3,3)' => { data_type => 'decimal', size => [3,3] },
93         'dec(3,3)'     => { data_type => 'decimal', size => [3,3] },
94         'numeric(3,3)' => { data_type => 'numeric', size => [3,3] },
95
96         'decimal(6,3)' => { data_type => 'decimal', size => [6,3] },
97         'numeric(6,3)' => { data_type => 'numeric', size => [6,3] },
98
99         'decimal(12,3)' => { data_type => 'decimal', size => [12,3] },
100         'numeric(12,3)' => { data_type => 'numeric', size => [12,3] },
101
102         'decimal(18,18)' => { data_type => 'decimal', size => [18,18] },
103         'dec(18,18)'     => { data_type => 'decimal', size => [18,18] },
104         'numeric(18,18)' => { data_type => 'numeric', size => [18,18] },
105
106         # Date and Time Types
107         'date'        => { data_type => 'date' },
108         'timestamp default current_timestamp'
109                       => { data_type => 'timestamp', default_value => \'current_timestamp' },
110         'time'        => { data_type => 'time' },
111
112         # String Types
113         'char'         => { data_type => 'char',      size => 1  },
114         'char(11)'     => { data_type => 'char',      size => 11 },
115         'varchar(20)'  => { data_type => 'varchar',   size => 20 },
116         'char(22) character set unicode_fss' =>
117                        => { data_type => 'char(x) character set unicode_fss', size => 22 },
118         'varchar(33) character set unicode_fss' =>
119                        => { data_type => 'varchar(x) character set unicode_fss', size => 33 },
120
121         # Blob types
122         'blob'        => { data_type => 'blob' },
123         'blob sub_type text'
124                       => { data_type => 'blob sub_type text' },
125         'blob sub_type text character set unicode_fss'
126                       => { data_type => 'blob sub_type text character set unicode_fss' },
127     },
128     extra => {
129         count  => 11,
130         create => [
131             q{
132                 CREATE TABLE "Firebird_Loader_Test1" (
133                     "Id" INTEGER NOT NULL PRIMARY KEY,
134                     "Foo" INTEGER DEFAULT 42
135                 )
136             },
137             q{
138                 CREATE GENERATOR "Gen_Firebird_Loader_Test1_Id"
139             },
140             q{
141                 CREATE TRIGGER "Firebird_Loader_Test1_BI" for "Firebird_Loader_Test1"
142                 ACTIVE BEFORE INSERT POSITION 0
143                 AS
144                 BEGIN
145                  IF (NEW."Id" IS NULL) THEN
146                   NEW."Id" = GEN_ID("Gen_Firebird_Loader_Test1_Id",1);
147                 END
148             },
149             q{
150                 CREATE VIEW firebird_loader_test2 AS SELECT * FROM "Firebird_Loader_Test1"
151             },
152         ],
153         pre_drop_ddl => [
154             'DROP VIEW firebird_loader_test2',
155             'DROP TRIGGER "Firebird_Loader_Test1_BI"',
156             'DROP GENERATOR "Gen_Firebird_Loader_Test1_Id"',
157             'DROP TABLE "Firebird_Loader_Test1"',
158         ],
159         run    => sub {
160             $schema = shift;
161             my ($monikers, $classes, $self) = @_;
162
163             my $dbh = $schema->storage->dbh;
164
165 # create a mixed case table
166             $dbh->do($_) for (
167             );
168
169             local $schema->loader->{preserve_case} = 1;
170             $schema->loader->_setup;
171
172             $self->rescan_without_warnings($schema);
173
174             ok ((my $rsrc = eval { $schema->resultset('FirebirdLoaderTest1')->result_source }),
175                 'got rsrc for mixed case table');
176
177             ok ((my $col_info = eval { $rsrc->column_info('Id') }),
178                 'got column_info for column Id');
179
180             is $col_info->{accessor}, 'id', 'column Id has lowercase accessor "id"';
181
182             is $col_info->{is_auto_increment}, 1, 'is_auto_increment detected for mixed case trigger';
183
184             is $col_info->{sequence}, 'Gen_Firebird_Loader_Test1_Id', 'correct mixed case sequence name';
185
186             is eval { $rsrc->column_info('Foo')->{default_value} }, 42, 'default_value detected for mixed case column';
187
188             # test that views are marked as such
189             my $view_source = $schema->resultset($monikers->{firebird_loader_test2})->result_source;
190             isa_ok $view_source, 'DBIx::Class::ResultSource::View',
191                 'view result source';
192
193             like $view_source->view_definition,
194                 qr/\A \s* select\b .* \bfrom \s+ (?-i:"Firebird_Loader_Test1") \s* \z/imsx,
195                 'view definition';
196
197             # test the fixed up ->_dbh_type_info_type_name for the ODBC driver
198             if ($schema->storage->_dbi_connect_info->[0] =~ /:ODBC:/i) {
199                 my %truncated_types = (
200                       4 => 'INTEGER',
201                      -9 => 'VARCHAR(x) CHARACTER SET UNICODE_FSS',
202                     -10 => 'BLOB SUB_TYPE TEXT CHARACTER SET UNICODE_FSS',
203                 );
204
205                 for my $type_num (keys %truncated_types) {
206                     is $schema->loader->_dbh_type_info_type_name($type_num),
207                         $truncated_types{$type_num},
208                         "ODBC ->_dbh_type_info_type_name correct for '$truncated_types{$type_num}'";
209                 }
210             }
211             else {
212                 my $tb = Test::More->builder;
213                 $tb->skip('not testing _dbh_type_info_type_name on DBD::InterBase') for 1..3;
214             }
215         },
216     },
217 );
218
219 {
220     # get rid of stupid warning from InterBase/GetInfo.pm
221     if ($dsns{FIREBIRD_INTERBASE}) {
222         local $SIG{__WARN__} = sigwarn_silencer(
223             qr{^(?:Use of uninitialized value|Argument "[0-9_]+" isn't numeric|Missing argument) in sprintf at \S+DBD/InterBase/GetInfo.pm line \d+\.$}
224         );
225         require DBD::InterBase;
226         require DBD::InterBase::GetInfo;
227     }
228
229     $tester->run_tests();
230 }
231
232 # vim:et sts=4 sw=4 tw=0: