remove is_deferrable => 1 from default for belongs_to rels
[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 => { preserve_case => 0 },
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         # XXX rewrite low precision floats to 'real'
63         'smallint'    => { data_type => 'smallint' },
64         'int'         => { data_type => 'integer' },
65         'integer'     => { data_type => 'integer' },
66         'bigint'      => { data_type => 'bigint' },
67         'float'       => { data_type => 'float' },
68         'double precision' =>
69                          { data_type => 'double precision' },
70         'real'        => { data_type => 'float' },
71
72         'float(2)'    => { data_type => 'float' },
73         'float(7)'    => { data_type => 'float' },
74         'float(8)'    => { data_type => 'double precision' },
75
76         'decimal'     => { data_type => 'decimal' },
77         'dec'         => { data_type => 'decimal' },
78         'numeric'     => { data_type => 'numeric' },
79
80         'decimal(3)'   => { data_type => 'decimal', size => [3,0] },
81
82         'decimal(3,3)' => { data_type => 'decimal', size => [3,3] },
83         'dec(3,3)'     => { data_type => 'decimal', size => [3,3] },
84         'numeric(3,3)' => { data_type => 'numeric', size => [3,3] },
85
86         'decimal(6,3)' => { data_type => 'decimal', size => [6,3] },
87         'numeric(6,3)' => { data_type => 'numeric', size => [6,3] },
88
89         'decimal(12,3)' => { data_type => 'decimal', size => [12,3] },
90         'numeric(12,3)' => { data_type => 'numeric', size => [12,3] },
91
92         'decimal(18,18)' => { data_type => 'decimal', size => [18,18] },
93         'dec(18,18)'     => { data_type => 'decimal', size => [18,18] },
94         'numeric(18,18)' => { data_type => 'numeric', size => [18,18] },
95
96         # Date and Time Types
97         'date'        => { data_type => 'date' },
98         'timestamp default current_timestamp'
99                       => { data_type => 'timestamp', default_value => \'current_timestamp' },
100         'time'        => { data_type => 'time' },
101
102         # String Types
103         'char'         => { data_type => 'char',      size => 1  },
104         'char(11)'     => { data_type => 'char',      size => 11 },
105         'varchar(20)'  => { data_type => 'varchar',   size => 20 },
106
107         # Blob types
108         'blob'        => { data_type => 'blob' },
109         'blob sub_type text'
110                       => { data_type => 'blob sub_type text' },
111     },
112     extra => {
113         count  => 7,
114         run    => sub {
115             $schema = shift;
116
117             cleanup_extra();
118
119             my $dbh = $schema->storage->dbh;
120
121 # create a mixed case table
122             $dbh->do($_) for (
123 q{
124     CREATE TABLE "Firebird_Loader_Test1" (
125         "Id" INTEGER NOT NULL PRIMARY KEY,
126         "Foo" INTEGER DEFAULT 42
127     )
128 },
129 q{
130     CREATE GENERATOR "Gen_Firebird_Loader_Test1_Id"
131 },
132 q{
133     CREATE TRIGGER "Firebird_Loader_Test1_BI" for "Firebird_Loader_Test1"
134     ACTIVE BEFORE INSERT POSITION 0
135     AS
136     BEGIN
137      IF (NEW."Id" IS NULL) THEN
138       NEW."Id" = GEN_ID("Gen_Firebird_Loader_Test1_Id",1);
139     END
140 },
141             );
142
143             my $guard = Scope::Guard->new(\&cleanup_extra);
144
145             delete $schema->_loader->{preserve_case};
146
147             my $warning;
148             {
149                 local $SIG{__WARN__} = sub { $warning = shift };
150                 $schema->_loader->_setup;
151             }
152             like $warning, qr/'preserve_case' option/,
153                 'warning mentions preserve_case option';
154
155             {
156                 local $SIG{__WARN__} = sub {};
157                 $schema->rescan;
158             }
159
160             ok ((my $rsrc = eval { $schema->resultset('FirebirdLoaderTest1')->result_source }),
161                 'got rsrc for mixed case table');
162
163             ok ((my $col_info = eval { $rsrc->column_info('Id') }),
164                 'got column_info for column Id');
165
166             is $col_info->{accessor}, 'id', 'column Id has lowercase accessor "id"';
167
168             is $col_info->{is_auto_increment}, 1, 'is_auto_increment detected for mixed case trigger';
169
170             is $col_info->{sequence}, 'Gen_Firebird_Loader_Test1_Id', 'correct mixed case sequence name';
171
172             is eval { $rsrc->column_info('Foo')->{default_value} }, 42, 'default_value detected for mixed case column';
173         },
174     },
175 );
176
177 if (not ($dbd_interbase_dsn || $odbc_dsn)) {
178     $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');
179 }
180 else {
181     # get rid of stupid warning from InterBase/GetInfo.pm
182     if ($dbd_interbase_dsn) {
183         local $SIG{__WARN__} = sub {};
184         require DBD::InterBase;
185         require DBD::InterBase::GetInfo;
186     }
187     $tester->run_tests();
188 }
189
190 sub cleanup_extra {
191     $schema->storage->disconnect;
192     my $dbh = $schema->storage->dbh;
193
194     foreach my $stmt (
195         'DROP TRIGGER "Firebird_Loader_Test1_BI"',
196         'DROP GENERATOR "Gen_Firebird_Loader_Test1_Id"',
197         'DROP TABLE "Firebird_Loader_Test1"',
198     ) {
199         eval { $dbh->do($stmt) };
200     }
201 }