Add author test for use strict/warnings
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 10_01sqlite_common.t
CommitLineData
a78e3fed 1use strict;
f8c2ca5e 2use warnings;
8763ffda 3use Test::More;
c2849787 4use lib qw(t/lib);
fbd83464 5use dbixcsl_common_tests;
c213fd3d 6use dbixcsl_test_dir qw/$tdir/;
a78e3fed 7
8eval { require DBD::SQLite };
9my $class = $@ ? 'SQLite2' : 'SQLite';
10
8763ffda 11my $tester = dbixcsl_common_tests->new(
12 vendor => 'SQLite',
13 auto_inc_pk => 'INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT',
c213fd3d 14 dsn => "dbi:$class:dbname=$tdir/sqlite_test",
8763ffda 15 user => '',
16 password => '',
b308dcca 17 connect_info_opts => {
be9f4d42 18 on_connect_do => [ 'PRAGMA foreign_keys = ON', 'PRAGMA synchronous = OFF', ]
b308dcca 19 },
eb040f78 20 loader_options => { preserve_case => 1 },
3b61a7ca 21 default_is_deferrable => 0,
22 default_on_clause => 'NO ACTION',
62bc1e5d 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' },
bc1cb85e 31
32 # test that type name is lowercased
33 'INTEGER' => { data_type => 'integer' },
34
62bc1e5d 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'
007e3511 58 => { data_type => 'timestamp', default_value => \'current_timestamp' },
62bc1e5d 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 },
8763ffda 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,
2a6dfbc9 72 "value" TEXT UNIQUE NOT NULL
8763ffda 73 )
68d650df 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 },
26da4cc3 85 # Wordy, newline-heavy SQL
68d650df 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 },
26da4cc3 97 # make sure views are picked up
98 q{
99 CREATE VIEW extra_loader_test5 AS SELECT * FROM extra_loader_test4
9dfbfb58 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 },
3b61a7ca 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 },
add8bcf0 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 },
8763ffda 138 ],
26da4cc3 139 pre_drop_ddl => [ 'DROP VIEW extra_loader_test5' ],
3b61a7ca 140 drop => [ qw/extra_loader_test1 extra_loader_test2 extra_loader_test3
141 extra_loader_test4 extra_loader_test6 extra_loader_test7
add8bcf0 142 extra_loader_test8 extra_loader_test9 extra_loader_test10 / ],
ce2f102a 143 count => 20,
8763ffda 144 run => sub {
145 my ($schema, $monikers, $classes) = @_;
a78e3fed 146
8763ffda 147 ok ((my $rs = $schema->resultset($monikers->{extra_loader_test1})),
148 'resultset for quoted table');
149
0fa48bf5 150 ok ((my $source = $rs->result_source), 'source');
151
152 is_deeply [ $source->columns ], [ qw/id value/ ],
8763ffda 153 'retrieved quoted column names from quoted table';
68d650df 154
3b134b29 155 ok ((exists $source->column_info('value')->{is_nullable}),
156 'is_nullable exists');
157
0fa48bf5 158 is $source->column_info('value')->{is_nullable}, 0,
3b134b29 159 'is_nullable is set correctly';
0fa48bf5 160
161 ok (($source = $schema->source($monikers->{extra_loader_test4})),
68d650df 162 'verbose table');
163
164 is_deeply [ $source->primary_columns ], [ qw/event_id person_id/ ],
165 'composite primary key';
166
68d650df 167 is ($source->relationships, 2,
168 '2 foreign key constraints found');
169
bf558f72 170 # test that columns for views are picked up
00805149 171 is $schema->resultset($monikers->{extra_loader_test5})->result_source->column_info('person_id')->{data_type}, 'integer',
bf558f72 172 'columns for views are introspected';
9dfbfb58 173
ce2f102a 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
9dfbfb58 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};
3b61a7ca 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';
add8bcf0 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';
8763ffda 208 },
209 },
210);
211
212$tester->run_tests();
a78e3fed 213
214END {
3b61a7ca 215 unlink "$tdir/sqlite_test" unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP};
a78e3fed 216}