Add author test for use strict/warnings
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 10_01sqlite_common.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use lib qw(t/lib);
5 use dbixcsl_common_tests;
6 use dbixcsl_test_dir qw/$tdir/;
7
8 eval { require DBD::SQLite };
9 my $class = $@ ? 'SQLite2' : 'SQLite';
10
11 my $tester = dbixcsl_common_tests->new(
12     vendor          => 'SQLite',
13     auto_inc_pk     => 'INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT',
14     dsn             => "dbi:$class:dbname=$tdir/sqlite_test",
15     user            => '',
16     password        => '',
17     connect_info_opts => {
18         on_connect_do => [ 'PRAGMA foreign_keys = ON', 'PRAGMA synchronous = OFF', ]
19     },
20     loader_options  => { preserve_case => 1 },
21     default_is_deferrable => 0,
22     default_on_clause => 'NO ACTION',
23     data_types  => {
24         # SQLite ignores data types aside from INTEGER pks.
25         # We just test that they roundtrip sanely.
26         #
27         # Numeric types
28         'smallint'    => { data_type => 'smallint' },
29         'int'         => { data_type => 'int' },
30         'integer'     => { data_type => 'integer' },
31
32         # test that type name is lowercased
33         'INTEGER'     => { data_type => 'integer' },
34
35         'bigint'      => { data_type => 'bigint' },
36         'float'       => { data_type => 'float' },
37         'double precision' =>
38                          { data_type => 'double precision' },
39         'real'        => { data_type => 'real' },
40
41         'float(2)'    => { data_type => 'float', size => 2 },
42         'float(7)'    => { data_type => 'float', size => 7 },
43
44         'decimal'     => { data_type => 'decimal' },
45         'dec'         => { data_type => 'dec' },
46         'numeric'     => { data_type => 'numeric' },
47
48         'decimal(3)'   => { data_type => 'decimal', size => 3 },
49         'numeric(3)'   => { data_type => 'numeric', size => 3 },
50
51         'decimal(3,3)' => { data_type => 'decimal', size => [3,3] },
52         'dec(3,3)'     => { data_type => 'dec', size => [3,3] },
53         'numeric(3,3)' => { data_type => 'numeric', size => [3,3] },
54
55         # Date and Time Types
56         'date'        => { data_type => 'date' },
57         'timestamp DEFAULT CURRENT_TIMESTAMP'
58                       => { data_type => 'timestamp', default_value => \'current_timestamp' },
59         'time'        => { data_type => 'time' },
60
61         # String Types
62         'char'         => { data_type => 'char' },
63         'char(11)'     => { data_type => 'char',    size => 11 },
64         'varchar(20)'  => { data_type => 'varchar', size => 20 },
65     },
66     extra           => {
67         create => [
68             # 'sqlite_' is reserved, so we use 'extra_'
69             q{
70                 CREATE TABLE "extra_loader_test1" (
71                     "id" NOT NULL PRIMARY KEY,
72                     "value" TEXT UNIQUE NOT NULL
73                 )
74             },
75             q{
76                 CREATE TABLE extra_loader_test2 (
77                     event_id INTEGER PRIMARY KEY
78                 )
79             },
80             q{
81                 CREATE TABLE extra_loader_test3 (
82                     person_id INTEGER PRIMARY KEY
83                 )
84             },
85             # Wordy, newline-heavy SQL
86             q{
87                 CREATE TABLE extra_loader_test4 (
88                     event_id INTEGER NOT NULL
89                         CONSTRAINT fk_event_id
90                         REFERENCES extra_loader_test2(event_id),
91                     person_id INTEGER NOT NULL
92                         CONSTRAINT fk_person_id
93                         REFERENCES extra_loader_test3 (person_id),
94                     PRIMARY KEY (event_id, person_id)
95                 )
96             },
97             # make sure views are picked up
98             q{
99                 CREATE VIEW extra_loader_test5 AS SELECT * FROM extra_loader_test4
100             },
101             # Compound primary keys can't be autoinc in the DBIC sense
102             q{
103                 CREATE TABLE extra_loader_test6 (
104                   id1 INTEGER,
105                   id2 INTEGER,
106                   value INTEGER,
107                   PRIMARY KEY (id1, id2)
108                 )
109             },
110             q{
111                 CREATE TABLE extra_loader_test7 (
112                   id1 INTEGER,
113                   id2 TEXT,
114                   value DECIMAL,
115                   PRIMARY KEY (id1, id2)
116                 )
117             },
118             q{
119                 create table extra_loader_test8 (
120                     id integer primary key
121                 )
122             },
123             q{
124                 create table extra_loader_test9 (
125                     id integer primary key,
126                     eight_id int,
127                     foreign key (eight_id) references extra_loader_test8(id)
128                         on delete restrict on update set null deferrable
129                 )
130             },
131             # test inline constraint
132             q{
133                 create table extra_loader_test10 (
134                     id integer primary key,
135                     eight_id int references extra_loader_test8(id) on delete restrict on update set null deferrable
136                 )
137             },
138         ],
139         pre_drop_ddl => [ 'DROP VIEW extra_loader_test5' ],
140         drop  => [ qw/extra_loader_test1 extra_loader_test2 extra_loader_test3
141                       extra_loader_test4 extra_loader_test6 extra_loader_test7
142                       extra_loader_test8 extra_loader_test9 extra_loader_test10 / ],
143         count => 20,
144         run   => sub {
145             my ($schema, $monikers, $classes) = @_;
146
147             ok ((my $rs = $schema->resultset($monikers->{extra_loader_test1})),
148                 'resultset for quoted table');
149
150             ok ((my $source = $rs->result_source), 'source');
151
152             is_deeply [ $source->columns ], [ qw/id value/ ],
153                 'retrieved quoted column names from quoted table';
154
155             ok ((exists $source->column_info('value')->{is_nullable}),
156                 'is_nullable exists');
157
158             is $source->column_info('value')->{is_nullable}, 0,
159                 'is_nullable is set correctly';
160
161             ok (($source = $schema->source($monikers->{extra_loader_test4})),
162                 'verbose table');
163
164             is_deeply [ $source->primary_columns ], [ qw/event_id person_id/ ],
165                 'composite primary key';
166
167             is ($source->relationships, 2,
168                 '2 foreign key constraints found');
169
170             # test that columns for views are picked up
171             is $schema->resultset($monikers->{extra_loader_test5})->result_source->column_info('person_id')->{data_type}, 'integer',
172                 'columns for views are introspected';
173
174             # test that views are marked as such
175             isa_ok $schema->resultset($monikers->{extra_loader_test5})->result_source, 'DBIx::Class::ResultSource::View',
176                 'views have table_class set correctly';
177
178             isnt $schema->resultset($monikers->{extra_loader_test6})->result_source->column_info('id1')->{is_auto_increment}, 1,
179                 q{two integer PKs don't get marked autoinc};
180
181             isnt $schema->resultset($monikers->{extra_loader_test7})->result_source->column_info('id1')->{is_auto_increment}, 1,
182                 q{composite integer PK with non-integer PK doesn't get marked autoinc};
183
184             # test on delete/update fk clause introspection
185             ok ((my $rel_info = $schema->source('ExtraLoaderTest9')->relationship_info('eight')),
186                 'got rel info');
187
188             is $rel_info->{attrs}{on_delete}, 'RESTRICT',
189                 'ON DELETE clause introspected correctly';
190
191             is $rel_info->{attrs}{on_update}, 'SET NULL',
192                 'ON UPDATE clause introspected correctly';
193
194             is $rel_info->{attrs}{is_deferrable}, 1,
195                 'DEFERRABLE clause introspected correctly';
196
197             ok (($rel_info = $schema->source('ExtraLoaderTest10')->relationship_info('eight')),
198                 'got rel info');
199
200             is $rel_info->{attrs}{on_delete}, 'RESTRICT',
201                 'ON DELETE clause introspected correctly for inline FK';
202
203             is $rel_info->{attrs}{on_update}, 'SET NULL',
204                 'ON UPDATE clause introspected correctly for inline FK';
205
206             is $rel_info->{attrs}{is_deferrable}, 1,
207                 'DEFERRABLE clause introspected correctly for inline FK';
208         },
209     },
210 );
211
212 $tester->run_tests();
213
214 END {
215     unlink "$tdir/sqlite_test" unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP};
216 }