d154cbd44aca06896421d3bbfbfd75ab6ec78237
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 18firebird_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     loader_options => { unquoted_ddl => 1 },
45     connect_info => [ ($dbd_interbase_dsn ? {
46             dsn         => $dbd_interbase_dsn,
47             user        => $dbd_interbase_user,
48             password    => $dbd_interbase_password,
49             connect_info_opts => { on_connect_call => 'use_softcommit' },
50         } : ()),
51         ($odbc_dsn ? {
52             dsn         => $odbc_dsn,
53             user        => $odbc_user,
54             password    => $odbc_password,
55         } : ()),
56     ],
57     data_types  => {
58         # based on the Interbase Data Definition Guide
59         # http://www.ibphoenix.com/downloads/60DataDef.zip
60         #
61         # Numeric types
62         'smallint'    => { data_type => 'smallint' },
63         'int'         => { data_type => 'integer' },
64         'integer'     => { data_type => 'integer' },
65         'bigint'      => { data_type => 'bigint' },
66         'float'       => { data_type => 'float' },
67         'double precision' =>
68                          { data_type => 'double precision' },
69         'real'        => { data_type => 'float' },
70
71         'float(2)'    => { data_type => 'float' },
72         'float(7)'    => { data_type => 'float' },
73         'float(8)'    => { data_type => 'double precision' },
74
75         'decimal'     => { data_type => 'decimal' },
76         'dec'         => { data_type => 'decimal' },
77         'numeric'     => { data_type => 'numeric' },
78
79         'decimal(3)'   => { data_type => 'decimal', size => [3,0] },
80
81         'decimal(3,3)' => { data_type => 'decimal', size => [3,3] },
82         'dec(3,3)'     => { data_type => 'decimal', size => [3,3] },
83         'numeric(3,3)' => { data_type => 'numeric', size => [3,3] },
84
85         'decimal(6,3)' => { data_type => 'decimal', size => [6,3] },
86         'numeric(6,3)' => { data_type => 'numeric', size => [6,3] },
87
88         'decimal(12,3)' => { data_type => 'decimal', size => [12,3] },
89         'numeric(12,3)' => { data_type => 'numeric', size => [12,3] },
90
91         'decimal(18,18)' => { data_type => 'decimal', size => [18,18] },
92         'dec(18,18)'     => { data_type => 'decimal', size => [18,18] },
93         'numeric(18,18)' => { data_type => 'numeric', size => [18,18] },
94
95         # Date and Time Types
96         'date'        => { data_type => 'date' },
97         'timestamp DEFAULT CURRENT_TIMESTAMP'
98                       => { data_type => 'timestamp', default_value => \"CURRENT_TIMESTAMP" },
99         'time'        => { data_type => 'time' },
100
101         # String Types
102         'char'         => { data_type => 'char',      size => 1  },
103         'char(11)'     => { data_type => 'char',      size => 11 },
104         'varchar(20)'  => { data_type => 'varchar',   size => 20 },
105
106         # Blob types
107         'blob'        => { data_type => 'blob' },
108         'blob sub_type text'
109                       => { data_type => 'blob sub_type text' },
110     },
111     extra => {
112         count  => 7,
113         run    => sub {
114             $schema = shift;
115
116             cleanup_extra();
117
118             my $dbh = $schema->storage->dbh;
119
120 # create a mixed case table
121             $dbh->do($_) for (
122 q{
123     CREATE TABLE "Firebird_Loader_Test1" (
124         "Id" INTEGER NOT NULL PRIMARY KEY,
125         "Foo" INTEGER DEFAULT 42
126     )
127 },
128 q{
129     CREATE GENERATOR "Gen_Firebird_Loader_Test1_Id"
130 },
131 q{
132     CREATE TRIGGER "Firebird_Loader_Test1_BI" for "Firebird_Loader_Test1"
133     ACTIVE BEFORE INSERT POSITION 0
134     AS
135     BEGIN
136      IF (NEW."Id" IS NULL) THEN
137       NEW."Id" = GEN_ID("Gen_Firebird_Loader_Test1_Id",1);
138     END
139 },
140             );
141
142             my $guard = Scope::Guard->new(\&cleanup_extra);
143
144             delete $schema->_loader->{unquoted_ddl};
145
146             my $warning;
147             {
148                 local $SIG{__WARN__} = sub { $warning = shift };
149                 $schema->_loader->_setup;
150             }
151             like $warning, qr/unquoted_ddl option/,
152                 'warning mentions unquoted_ddl option';
153
154             {
155                 local $SIG{__WARN__} = sub {};
156                 $schema->rescan;
157             }
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     },
174 );
175
176 if (not ($dbd_interbase_dsn || $odbc_dsn)) {
177     $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');
178 }
179 else {
180     # get rid of stupid warning from InterBase/GetInfo.pm
181     if ($dbd_interbase_dsn) {
182         local $SIG{__WARN__} = sub {};
183         require DBD::InterBase;
184         require DBD::InterBase::GetInfo;
185     }
186     $tester->run_tests();
187 }
188
189 sub cleanup_extra {
190     $schema->storage->disconnect;
191     my $dbh = $schema->storage->dbh;
192
193     foreach my $stmt (
194         'DROP TRIGGER "Firebird_Loader_Test1_BI"',
195         'DROP GENERATOR "Gen_Firebird_Loader_Test1_Id"',
196         'DROP TABLE "Firebird_Loader_Test1"',
197     ) {
198         eval { $dbh->do($stmt) };
199     }
200 }