fix clasing test dbs
[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 lib qw(t/lib);
6 use dbixcsl_common_tests;
7
8 my $dbd_interbase_dsn      = $ENV{DBICTEST_FIREBIRD_DSN} || '';
9 my $dbd_interbase_user     = $ENV{DBICTEST_FIREBIRD_USER} || '';
10 my $dbd_interbase_password = $ENV{DBICTEST_FIREBIRD_PASS} || '';
11
12 my $odbc_dsn      = $ENV{DBICTEST_FIREBIRD_ODBC_DSN} || '';
13 my $odbc_user     = $ENV{DBICTEST_FIREBIRD_ODBC_USER} || '';
14 my $odbc_password = $ENV{DBICTEST_FIREBIRD_ODBC_PASS} || '';
15
16 my $schema;
17
18 my $tester = dbixcsl_common_tests->new(
19     vendor      => 'Firebird',
20     auto_inc_pk => 'INTEGER NOT NULL PRIMARY KEY',
21     auto_inc_cb => sub {
22         my ($table, $col) = @_;
23         return (
24             qq{ CREATE GENERATOR gen_${table}_${col} },
25             qq{
26                 CREATE TRIGGER ${table}_bi FOR $table
27                 ACTIVE BEFORE INSERT POSITION 0
28                 AS
29                 BEGIN
30                  IF (NEW.$col IS NULL) THEN
31                   NEW.$col = GEN_ID(gen_${table}_${col},1);
32                 END
33             }
34         );
35     },
36     auto_inc_drop_cb => sub {
37         my ($table, $col) = @_;
38         return (
39             qq{ DROP TRIGGER ${table}_bi },
40             qq{ DROP GENERATOR gen_${table}_${col} },
41         );
42     },
43     null        => '',
44     preserve_case_mode_is_exclusive => 1,
45     quote_char                      => '"',
46     warnings => [ qr/'preserve_case' option/ ],
47     connect_info => [ ($dbd_interbase_dsn ? {
48             dsn         => $dbd_interbase_dsn,
49             user        => $dbd_interbase_user,
50             password    => $dbd_interbase_password,
51             connect_info_opts => { on_connect_call => 'use_softcommit' },
52         } : ()),
53         ($odbc_dsn ? {
54             dsn         => $odbc_dsn,
55             user        => $odbc_user,
56             password    => $odbc_password,
57         } : ()),
58     ],
59     data_types  => {
60         # based on the Interbase Data Definition Guide
61         # http://www.ibphoenix.com/downloads/60DataDef.zip
62         #
63         # Numeric types
64         'smallint'    => { data_type => 'smallint' },
65         'int'         => { data_type => 'integer' },
66         'integer'     => { data_type => 'integer' },
67         'bigint'      => { data_type => 'bigint' },
68         'float'       => { data_type => 'real' },
69         'double precision' =>
70                          { data_type => 'double precision' },
71         'real'        => { data_type => 'real' },
72
73         'float(2)'    => { data_type => 'real' },
74         'float(7)'    => { data_type => 'real' },
75         'float(8)'    => { data_type => 'double precision' },
76
77         'decimal'     => { data_type => 'decimal' },
78         'dec'         => { data_type => 'decimal' },
79         'numeric'     => { data_type => 'numeric' },
80
81         'decimal(3)'   => { data_type => 'decimal', size => [3,0] },
82
83         'decimal(3,3)' => { data_type => 'decimal', size => [3,3] },
84         'dec(3,3)'     => { data_type => 'decimal', size => [3,3] },
85         'numeric(3,3)' => { data_type => 'numeric', size => [3,3] },
86
87         'decimal(6,3)' => { data_type => 'decimal', size => [6,3] },
88         'numeric(6,3)' => { data_type => 'numeric', size => [6,3] },
89
90         'decimal(12,3)' => { data_type => 'decimal', size => [12,3] },
91         'numeric(12,3)' => { data_type => 'numeric', size => [12,3] },
92
93         'decimal(18,18)' => { data_type => 'decimal', size => [18,18] },
94         'dec(18,18)'     => { data_type => 'decimal', size => [18,18] },
95         'numeric(18,18)' => { data_type => 'numeric', size => [18,18] },
96
97         # Date and Time Types
98         'date'        => { data_type => 'date' },
99         'timestamp default current_timestamp'
100                       => { data_type => 'timestamp', default_value => \'current_timestamp' },
101         'time'        => { data_type => 'time' },
102
103         # String Types
104         'char'         => { data_type => 'char',      size => 1  },
105         'char(11)'     => { data_type => 'char',      size => 11 },
106         'varchar(20)'  => { data_type => 'varchar',   size => 20 },
107         'char(22) character set unicode_fss' =>
108                        => { data_type => 'char(x) character set unicode_fss', size => 22 },
109         'varchar(33) character set unicode_fss' =>
110                        => { data_type => 'varchar(x) character set unicode_fss', size => 33 },
111
112
113         # Blob types
114         'blob'        => { data_type => 'blob' },
115         'blob sub_type text'
116                       => { data_type => 'blob sub_type text' },
117         'blob sub_type text character set unicode_fss'
118                       => { data_type => 'blob sub_type text character set unicode_fss' },
119     },
120     extra => {
121         count  => 9,
122         run    => sub {
123             $schema = shift;
124             my ($monikers, $classes, $self) = @_;
125
126             cleanup_extra();
127
128             my $dbh = $schema->storage->dbh;
129
130 # create a mixed case table
131             $dbh->do($_) for (
132 q{
133     CREATE TABLE "Firebird_Loader_Test1" (
134         "Id" INTEGER NOT NULL PRIMARY KEY,
135         "Foo" INTEGER DEFAULT 42
136     )
137 },
138 q{
139     CREATE GENERATOR "Gen_Firebird_Loader_Test1_Id"
140 },
141 q{
142     CREATE TRIGGER "Firebird_Loader_Test1_BI" for "Firebird_Loader_Test1"
143     ACTIVE BEFORE INSERT POSITION 0
144     AS
145     BEGIN
146      IF (NEW."Id" IS NULL) THEN
147       NEW."Id" = GEN_ID("Gen_Firebird_Loader_Test1_Id",1);
148     END
149 },
150             );
151
152             my $guard = Scope::Guard->new(\&cleanup_extra);
153
154             local $schema->_loader->{preserve_case} = 1;
155             $schema->_loader->_setup;
156
157             $self->rescan_without_warnings($schema);
158
159             ok ((my $rsrc = eval { $schema->resultset('FirebirdLoaderTest1')->result_source }),
160                 'got rsrc for mixed case table');
161
162             ok ((my $col_info = eval { $rsrc->column_info('Id') }),
163                 'got column_info for column Id');
164
165             is $col_info->{accessor}, 'id', 'column Id has lowercase accessor "id"';
166
167             is $col_info->{is_auto_increment}, 1, 'is_auto_increment detected for mixed case trigger';
168
169             is $col_info->{sequence}, 'Gen_Firebird_Loader_Test1_Id', 'correct mixed case sequence name';
170
171             is eval { $rsrc->column_info('Foo')->{default_value} }, 42, 'default_value detected for mixed case column';
172
173             # test the fixed up ->_dbh_type_info_type_name for the ODBC driver
174             if ($schema->storage->_dbi_connect_info->[0] =~ /:ODBC:/i) {
175                 my %truncated_types = (
176                       4 => 'INTEGER',
177                      -9 => 'VARCHAR(x) CHARACTER SET UNICODE_FSS',
178                     -10 => 'BLOB SUB_TYPE TEXT CHARACTER SET UNICODE_FSS',
179                 );
180
181                 for my $type_num (keys %truncated_types) {
182                     is $schema->_loader->_dbh_type_info_type_name($type_num),
183                         $truncated_types{$type_num},
184                         "ODBC ->_dbh_type_info_type_name correct for '$truncated_types{$type_num}'";
185                 }
186             }
187             else {
188                 my $tb = Test::More->builder;
189                 $tb->skip('not testing _dbh_type_info_type_name on DBD::InterBase') for 1..3;
190             }
191         },
192     },
193 );
194
195 if (not ($dbd_interbase_dsn || $odbc_dsn)) {
196     $tester->skip_tests('You need to set the DBICTEST_FIREBIRD_DSN, _USER and _PASS and/or the DBICTEST_FIREBIRD_ODBC_DSN, _USER and _PASS environment variables');
197 }
198 else {
199     # get rid of stupid warning from InterBase/GetInfo.pm
200     if ($dbd_interbase_dsn) {
201         local $SIG{__WARN__} = sub { warn @_
202             unless $_[0] =~ m|^Use of uninitialized value in sprintf at \S+DBD/InterBase/GetInfo\.pm line \d+\.$| };
203         require DBD::InterBase;
204         require DBD::InterBase::GetInfo;
205     }
206     $tester->run_tests();
207 }
208
209 sub cleanup_extra {
210     $schema->storage->disconnect;
211     my $dbh = $schema->storage->dbh;
212
213     foreach my $stmt (
214         'DROP TRIGGER "Firebird_Loader_Test1_BI"',
215         'DROP GENERATOR "Gen_Firebird_Loader_Test1_Id"',
216         'DROP TABLE "Firebird_Loader_Test1"',
217     ) {
218         eval { $dbh->do($stmt) };
219     }
220 }
221 # vim:et sts=4 sw=4 tw=0: