5 use dbixcsl_common_tests;
6 use dbixcsl_test_dir qw/$tdir/;
8 eval { require DBD::SQLite };
9 my $class = $@ ? 'SQLite2' : 'SQLite';
11 my $tester = dbixcsl_common_tests->new(
13 auto_inc_pk => 'INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT',
14 dsn => "dbi:$class:dbname=$tdir/sqlite_test.db",
17 connect_info_opts => {
18 on_connect_do => [ 'PRAGMA foreign_keys = ON', 'PRAGMA synchronous = OFF', ]
20 loader_options => { preserve_case => 1 },
21 default_is_deferrable => 0,
22 default_on_clause => 'NO ACTION',
24 # SQLite ignores data types aside from INTEGER pks.
25 # We just test that they roundtrip sanely.
28 'smallint' => { data_type => 'smallint' },
29 'int' => { data_type => 'int' },
30 'integer' => { data_type => 'integer' },
32 # test that type name is lowercased
33 'INTEGER' => { data_type => 'integer' },
35 'bigint' => { data_type => 'bigint' },
36 'float' => { data_type => 'float' },
38 { data_type => 'double precision' },
39 'real' => { data_type => 'real' },
41 'float(2)' => { data_type => 'float', size => 2 },
42 'float(7)' => { data_type => 'float', size => 7 },
44 'decimal' => { data_type => 'decimal' },
45 'dec' => { data_type => 'dec' },
46 'numeric' => { data_type => 'numeric' },
48 'decimal(3)' => { data_type => 'decimal', size => 3 },
49 'numeric(3)' => { data_type => 'numeric', size => 3 },
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] },
56 'date' => { data_type => 'date' },
57 'timestamp DEFAULT CURRENT_TIMESTAMP'
58 => { data_type => 'timestamp', default_value => \'current_timestamp' },
59 'time' => { data_type => 'time' },
62 'char' => { data_type => 'char' },
63 'char(11)' => { data_type => 'char', size => 11 },
64 'varchar(20)' => { data_type => 'varchar', size => 20 },
68 # 'sqlite_' is reserved, so we use 'extra_'
70 CREATE TABLE "extra_loader_test1" (
71 "id" NOT NULL PRIMARY KEY,
72 "value" TEXT UNIQUE NOT NULL
76 CREATE TABLE extra_loader_test2 (
77 event_id INTEGER PRIMARY KEY
81 CREATE TABLE extra_loader_test3 (
82 person_id INTEGER PRIMARY KEY
85 # Wordy, newline-heavy SQL
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)
97 # make sure views are picked up
99 CREATE VIEW extra_loader_test5 AS SELECT * FROM extra_loader_test4
101 # Compound primary keys can't be autoinc in the DBIC sense
103 CREATE TABLE extra_loader_test6 (
107 PRIMARY KEY (id1, id2)
111 CREATE TABLE extra_loader_test7 (
115 PRIMARY KEY (id1, id2)
119 create table extra_loader_test8 (
120 id integer primary key
124 create table extra_loader_test9 (
125 id integer primary key,
127 foreign key (eight_id) references extra_loader_test8(id)
128 on delete restrict on update set null deferrable
131 # test inline constraint
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
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 / ],
145 my ($schema, $monikers, $classes) = @_;
147 ok ((my $rs = $schema->resultset($monikers->{extra_loader_test1})),
148 'resultset for quoted table');
150 ok ((my $source = $rs->result_source), 'source');
152 is_deeply [ $source->columns ], [ qw/id value/ ],
153 'retrieved quoted column names from quoted table';
155 ok ((exists $source->column_info('value')->{is_nullable}),
156 'is_nullable exists');
158 is $source->column_info('value')->{is_nullable}, 0,
159 'is_nullable is set correctly';
161 ok (($source = $schema->source($monikers->{extra_loader_test4})),
164 is_deeply [ $source->primary_columns ], [ qw/event_id person_id/ ],
165 'composite primary key';
167 is ($source->relationships, 2,
168 '2 foreign key constraints found');
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';
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';
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};
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};
184 # test on delete/update fk clause introspection
185 ok ((my $rel_info = $schema->source('ExtraLoaderTest9')->relationship_info('eight')),
188 is $rel_info->{attrs}{on_delete}, 'RESTRICT',
189 'ON DELETE clause introspected correctly';
191 is $rel_info->{attrs}{on_update}, 'SET NULL',
192 'ON UPDATE clause introspected correctly';
194 is $rel_info->{attrs}{is_deferrable}, 1,
195 'DEFERRABLE clause introspected correctly';
197 ok (($rel_info = $schema->source('ExtraLoaderTest10')->relationship_info('eight')),
200 is $rel_info->{attrs}{on_delete}, 'RESTRICT',
201 'ON DELETE clause introspected correctly for inline FK';
203 is $rel_info->{attrs}{on_update}, 'SET NULL',
204 'ON UPDATE clause introspected correctly for inline FK';
206 is $rel_info->{attrs}{is_deferrable}, 1,
207 'DEFERRABLE clause introspected correctly for inline FK';
212 $tester->run_tests();
215 unlink "$tdir/sqlite_test.db" unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP};