Install DBIC dev rel under CLEANTEST="false"
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 10_01sqlite_common.t
CommitLineData
a78e3fed 1use strict;
8763ffda 2use Test::More;
c2849787 3use lib qw(t/lib);
fbd83464 4use dbixcsl_common_tests;
c213fd3d 5use dbixcsl_test_dir qw/$tdir/;
a78e3fed 6
7eval { require DBD::SQLite };
8my $class = $@ ? 'SQLite2' : 'SQLite';
9
8763ffda 10my $tester = dbixcsl_common_tests->new(
11 vendor => 'SQLite',
12 auto_inc_pk => 'INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT',
c213fd3d 13 dsn => "dbi:$class:dbname=$tdir/sqlite_test",
8763ffda 14 user => '',
15 password => '',
b308dcca 16 connect_info_opts => {
be9f4d42 17 on_connect_do => [ 'PRAGMA foreign_keys = ON', 'PRAGMA synchronous = OFF', ]
b308dcca 18 },
eb040f78 19 loader_options => { preserve_case => 1 },
3b61a7ca 20 default_is_deferrable => 0,
21 default_on_clause => 'NO ACTION',
62bc1e5d 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' },
bc1cb85e 30
31 # test that type name is lowercased
32 'INTEGER' => { data_type => 'integer' },
33
62bc1e5d 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'
007e3511 57 => { data_type => 'timestamp', default_value => \'current_timestamp' },
62bc1e5d 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 },
8763ffda 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,
2a6dfbc9 71 "value" TEXT UNIQUE NOT NULL
8763ffda 72 )
68d650df 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 },
26da4cc3 84 # Wordy, newline-heavy SQL
68d650df 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 },
26da4cc3 96 # make sure views are picked up
97 q{
98 CREATE VIEW extra_loader_test5 AS SELECT * FROM extra_loader_test4
9dfbfb58 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 },
3b61a7ca 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 },
add8bcf0 130 # test inline constraint
131 q{
132 create table extra_loader_test10 (
133 id integer primary key,
134 eight_id int references extra_loader_test8(id) on delete restrict on update set null deferrable
135 )
136 },
8763ffda 137 ],
26da4cc3 138 pre_drop_ddl => [ 'DROP VIEW extra_loader_test5' ],
3b61a7ca 139 drop => [ qw/extra_loader_test1 extra_loader_test2 extra_loader_test3
140 extra_loader_test4 extra_loader_test6 extra_loader_test7
add8bcf0 141 extra_loader_test8 extra_loader_test9 extra_loader_test10 / ],
ce2f102a 142 count => 20,
8763ffda 143 run => sub {
144 my ($schema, $monikers, $classes) = @_;
a78e3fed 145
8763ffda 146 ok ((my $rs = $schema->resultset($monikers->{extra_loader_test1})),
147 'resultset for quoted table');
148
0fa48bf5 149 ok ((my $source = $rs->result_source), 'source');
150
151 is_deeply [ $source->columns ], [ qw/id value/ ],
8763ffda 152 'retrieved quoted column names from quoted table';
68d650df 153
3b134b29 154 ok ((exists $source->column_info('value')->{is_nullable}),
155 'is_nullable exists');
156
0fa48bf5 157 is $source->column_info('value')->{is_nullable}, 0,
3b134b29 158 'is_nullable is set correctly';
0fa48bf5 159
160 ok (($source = $schema->source($monikers->{extra_loader_test4})),
68d650df 161 'verbose table');
162
163 is_deeply [ $source->primary_columns ], [ qw/event_id person_id/ ],
164 'composite primary key';
165
68d650df 166 is ($source->relationships, 2,
167 '2 foreign key constraints found');
168
bf558f72 169 # test that columns for views are picked up
00805149 170 is $schema->resultset($monikers->{extra_loader_test5})->result_source->column_info('person_id')->{data_type}, 'integer',
bf558f72 171 'columns for views are introspected';
9dfbfb58 172
ce2f102a 173 # test that views are marked as such
174 isa_ok $schema->resultset($monikers->{extra_loader_test5})->result_source, 'DBIx::Class::ResultSource::View',
175 'views have table_class set correctly';
176
9dfbfb58 177 isnt $schema->resultset($monikers->{extra_loader_test6})->result_source->column_info('id1')->{is_auto_increment}, 1,
178 q{two integer PKs don't get marked autoinc};
179
180 isnt $schema->resultset($monikers->{extra_loader_test7})->result_source->column_info('id1')->{is_auto_increment}, 1,
181 q{composite integer PK with non-integer PK doesn't get marked autoinc};
3b61a7ca 182
183 # test on delete/update fk clause introspection
184 ok ((my $rel_info = $schema->source('ExtraLoaderTest9')->relationship_info('eight')),
185 'got rel info');
186
187 is $rel_info->{attrs}{on_delete}, 'RESTRICT',
188 'ON DELETE clause introspected correctly';
189
190 is $rel_info->{attrs}{on_update}, 'SET NULL',
191 'ON UPDATE clause introspected correctly';
192
193 is $rel_info->{attrs}{is_deferrable}, 1,
194 'DEFERRABLE clause introspected correctly';
add8bcf0 195
196 ok (($rel_info = $schema->source('ExtraLoaderTest10')->relationship_info('eight')),
197 'got rel info');
198
199 is $rel_info->{attrs}{on_delete}, 'RESTRICT',
200 'ON DELETE clause introspected correctly for inline FK';
201
202 is $rel_info->{attrs}{on_update}, 'SET NULL',
203 'ON UPDATE clause introspected correctly for inline FK';
204
205 is $rel_info->{attrs}{is_deferrable}, 1,
206 'DEFERRABLE clause introspected correctly for inline FK';
8763ffda 207 },
208 },
209);
210
211$tester->run_tests();
a78e3fed 212
213END {
3b61a7ca 214 unlink "$tdir/sqlite_test" unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP};
a78e3fed 215}