9d71c1cd36637771a45d2a1d7ce465fb94ae85a4
[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     extra => {
58         count  => 7,
59         run    => sub {
60             $schema = shift;
61
62             cleanup_extra();
63
64             my $dbh = $schema->storage->dbh;
65
66 # create a mixed case table
67             $dbh->do($_) for (
68 q{
69     CREATE TABLE "Firebird_Loader_Test1" (
70         "Id" INTEGER NOT NULL PRIMARY KEY,
71         "Foo" INTEGER DEFAULT 42
72     )
73 },
74 q{
75     CREATE GENERATOR "Gen_Firebird_Loader_Test1_Id"
76 },
77 q{
78     CREATE TRIGGER "Firebird_Loader_Test1_BI" for "Firebird_Loader_Test1"
79     ACTIVE BEFORE INSERT POSITION 0
80     AS
81     BEGIN
82      IF (NEW."Id" IS NULL) THEN
83       NEW."Id" = GEN_ID("Gen_Firebird_Loader_Test1_Id",1);
84     END
85 },
86             );
87
88             my $guard = Scope::Guard->new(\&cleanup_extra);
89
90             delete $schema->_loader->{unquoted_ddl};
91
92             my $warning;
93             {
94                 local $SIG{__WARN__} = sub { $warning = shift };
95                 $schema->_loader->_setup;
96             }
97             like $warning, qr/unquoted_ddl option/,
98                 'warning mentions unquoted_ddl option';
99
100             {
101                 local $SIG{__WARN__} = sub {};
102                 $schema->rescan;
103             }
104
105             ok ((my $rsrc = eval { $schema->resultset('FirebirdLoaderTest1')->result_source }),
106                 'got rsrc for mixed case table');
107
108             ok ((my $col_info = eval { $rsrc->column_info('Id') }),
109                 'got column_info for column Id');
110
111             is $col_info->{accessor}, 'id', 'column Id has lowercase accessor "id"';
112
113             is $col_info->{is_auto_increment}, 1, 'is_auto_increment detected for mixed case trigger';
114
115             is $col_info->{sequence}, 'Gen_Firebird_Loader_Test1_Id', 'correct mixed case sequence name';
116
117             is eval { $rsrc->column_info('Foo')->{default_value} }, 42, 'default_value detected for mixed case column';
118         },
119     },
120 );
121
122 if (not ($dbd_interbase_dsn || $odbc_dsn)) {
123     $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');
124 }
125 else {
126     # get rid of stupid warning from InterBase/GetInfo.pm
127     if ($dbd_interbase_dsn) {
128         local $SIG{__WARN__} = sub {};
129         require DBD::InterBase;
130         require DBD::InterBase::GetInfo;
131     }
132     $tester->run_tests();
133 }
134
135 sub cleanup_extra {
136     $schema->storage->disconnect;
137     my $dbh = $schema->storage->dbh;
138
139     foreach my $stmt (
140         'DROP TRIGGER "Firebird_Loader_Test1_BI"',
141         'DROP GENERATOR "Gen_Firebird_Loader_Test1_Id"',
142         'DROP TABLE "Firebird_Loader_Test1"',
143     ) {
144         eval { $dbh->do($stmt) };
145     }
146 }