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