Split Firebird tests into per-DBD files with a shared library
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / lib / dbixcsl_firebird_tests.pm
1 package dbixcsl_firebird_tests;
2
3 use strict;
4 use warnings;
5 use Test::More;
6 use Scope::Guard ();
7 use lib qw(t/lib);
8
9 use MRO::Compat;
10 use base 'dbixcsl_common_tests';
11
12 sub new {
13     my ($class, %opts) = @_;
14
15     return $class->next::method(
16         %opts,
17         vendor      => 'Firebird',
18         auto_inc_pk => 'INTEGER NOT NULL PRIMARY KEY',
19         auto_inc_cb => sub {
20             my ($table, $col) = @_;
21             return (
22                 qq{ CREATE GENERATOR gen_${table}_${col} },
23                 qq{
24                 CREATE TRIGGER ${table}_bi FOR $table
25                 ACTIVE BEFORE INSERT POSITION 0
26                 AS
27                 BEGIN
28                  IF (NEW.$col IS NULL) THEN
29                   NEW.$col = GEN_ID(gen_${table}_${col},1);
30                 END
31             }
32             );
33         },
34         auto_inc_drop_cb => sub {
35             my ($table, $col) = @_;
36             return (
37                 qq{ DROP TRIGGER ${table}_bi },
38                 qq{ DROP GENERATOR gen_${table}_${col} },
39             );
40         },
41         null        => '',
42         preserve_case_mode_is_exclusive => 1,
43         quote_char                      => '"',
44         data_types  => {
45             # based on the Interbase Data Definition Guide
46             # http://www.ibphoenix.com/downloads/60DataDef.zip
47             #
48             # Numeric types
49             'smallint'    => { data_type => 'smallint' },
50             'int'         => { data_type => 'integer' },
51             'integer'     => { data_type => 'integer' },
52             'bigint'      => { data_type => 'bigint' },
53             'float'       => { data_type => 'real' },
54             'double precision' =>
55                 {
56                     data_type => 'double precision' },
57             'real'        => { data_type => 'real' },
58
59             'float(2)'    => { data_type => 'real' },
60             'float(7)'    => { data_type => 'real' },
61             'float(8)'    => { data_type => 'double precision' },
62
63             'decimal'     => { data_type => 'decimal' },
64             'dec'         => { data_type => 'decimal' },
65             'numeric'     => { data_type => 'numeric' },
66
67             'decimal(3)'   => { data_type => 'decimal', size => [3,0] },
68
69             'decimal(3,3)' => { data_type => 'decimal', size => [3,3] },
70             'dec(3,3)'     => { data_type => 'decimal', size => [3,3] },
71             'numeric(3,3)' => { data_type => 'numeric', size => [3,3] },
72
73             'decimal(6,3)' => { data_type => 'decimal', size => [6,3] },
74             'numeric(6,3)' => { data_type => 'numeric', size => [6,3] },
75
76             'decimal(12,3)' => { data_type => 'decimal', size => [12,3] },
77             'numeric(12,3)' => { data_type => 'numeric', size => [12,3] },
78
79             'decimal(18,18)' => { data_type => 'decimal', size => [18,18] },
80             'dec(18,18)'     => { data_type => 'decimal', size => [18,18] },
81             'numeric(18,18)' => { data_type => 'numeric', size => [18,18] },
82
83             # Date and Time Types
84             'date'        => { data_type => 'date' },
85             'timestamp default current_timestamp'
86                 => { data_type => 'timestamp', default_value => \'current_timestamp' },
87             'time'        => { data_type => 'time' },
88
89             # String Types
90             'char'         => { data_type => 'char',      size => 1  },
91             'char(11)'     => { data_type => 'char',      size => 11 },
92             'varchar(20)'  => { data_type => 'varchar',   size => 20 },
93             'char(22) character set unicode_fss' =>
94                 => { data_type => 'char(x) character set unicode_fss', size => 22 },
95             'varchar(33) character set unicode_fss' =>
96                 => { data_type => 'varchar(x) character set unicode_fss', size => 33 },
97
98             # Blob types
99             'blob'        => { data_type => 'blob' },
100             'blob sub_type text'
101                 => { data_type => 'blob sub_type text' },
102             'blob sub_type text character set unicode_fss'
103                 => { data_type => 'blob sub_type text character set unicode_fss' },
104         },
105         extra => {
106             count  => 11,
107             create => [
108                 q{
109                 CREATE TABLE "Firebird_Loader_Test1" (
110                     "Id" INTEGER NOT NULL PRIMARY KEY,
111                     "Foo" INTEGER DEFAULT 42
112                 )
113             },
114                 q{
115                 CREATE GENERATOR "Gen_Firebird_Loader_Test1_Id"
116             },
117                 q{
118                 CREATE TRIGGER "Firebird_Loader_Test1_BI" for "Firebird_Loader_Test1"
119                 ACTIVE BEFORE INSERT POSITION 0
120                 AS
121                 BEGIN
122                  IF (NEW."Id" IS NULL) THEN
123                   NEW."Id" = GEN_ID("Gen_Firebird_Loader_Test1_Id",1);
124                 END
125             },
126                 q{
127                 CREATE VIEW firebird_loader_test2 AS SELECT * FROM "Firebird_Loader_Test1"
128             },
129             ],
130             pre_drop_ddl => [
131                 'DROP VIEW firebird_loader_test2',
132                 'DROP TRIGGER "Firebird_Loader_Test1_BI"',
133                 'DROP GENERATOR "Gen_Firebird_Loader_Test1_Id"',
134                 'DROP TABLE "Firebird_Loader_Test1"',
135             ],
136             run    => sub {
137                 my ($schema, $monikers, $classes, $self) = @_;
138
139                 my $dbh = $schema->storage->dbh;
140
141                 # create a mixed case table
142                 $dbh->do($_) for (
143                 );
144
145                 local $schema->loader->{preserve_case} = 1;
146                 $schema->loader->_setup;
147
148                 $self->rescan_without_warnings($schema);
149
150                 ok ((my $rsrc = eval { $schema->resultset('FirebirdLoaderTest1')->result_source }),
151                     'got rsrc for mixed case table');
152
153                 ok ((my $col_info = eval { $rsrc->column_info('Id') }),
154                     'got column_info for column Id');
155
156                 is $col_info->{accessor}, 'id', 'column Id has lowercase accessor "id"';
157
158                 is $col_info->{is_auto_increment}, 1, 'is_auto_increment detected for mixed case trigger';
159
160                 is $col_info->{sequence}, 'Gen_Firebird_Loader_Test1_Id', 'correct mixed case sequence name';
161
162                 is eval { $rsrc->column_info('Foo')->{default_value} }, 42, 'default_value detected for mixed case column';
163
164                 # test that views are marked as such
165                 my $view_source = $schema->resultset($monikers->{firebird_loader_test2})->result_source;
166                 isa_ok $view_source, 'DBIx::Class::ResultSource::View',
167                     'view result source';
168
169                 like $view_source->view_definition,
170                     qr/\A \s* select\b .* \bfrom \s+ (?-i:"Firebird_Loader_Test1") \s* \z/imsx,
171                     'view definition';
172
173                 # test the fixed up ->_dbh_type_info_type_name for the ODBC driver
174                 SKIP: {
175                     skip '_dbh_type_info_type_name test is only for DBD::ODBC', 3
176                         unless $schema->storage->_dbi_connect_info->[0] =~ /:ODBC:/i;
177
178                     my %truncated_types = (
179                         4 => 'INTEGER',
180                         -9 => 'VARCHAR(x) CHARACTER SET UNICODE_FSS',
181                         -10 => 'BLOB SUB_TYPE TEXT CHARACTER SET UNICODE_FSS',
182                     );
183
184                     for my $type_num (keys %truncated_types) {
185                         is $schema->loader->_dbh_type_info_type_name($type_num),
186                             $truncated_types{$type_num},
187                             "ODBC ->_dbh_type_info_type_name correct for '$truncated_types{$type_num}'";
188                     }
189                 }
190             },
191         },
192     );
193 }
194
195 1;
196
197 # vim:et sts=4 sw=4 tw=0: