4 use dbixcsl_common_tests;
5 use dbixcsl_test_dir qw/$tdir/;
7 eval { require DBD::SQLite };
8 my $class = $@ ? 'SQLite2' : 'SQLite';
10 my $tester = dbixcsl_common_tests->new(
12 auto_inc_pk => 'INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT',
13 dsn => "dbi:$class:dbname=$tdir/sqlite_test",
16 connect_info_opts => {
17 on_connect_do => [ 'PRAGMA foreign_keys = ON', 'PRAGMA synchronous = OFF', ]
19 loader_options => { preserve_case => 1 },
20 default_is_deferrable => 0,
21 default_on_clause => 'NO ACTION',
23 # SQLite ignores data types aside from INTEGER pks.
24 # We just test that they roundtrip sanely.
27 'smallint' => { data_type => 'smallint' },
28 'int' => { data_type => 'int' },
29 'integer' => { data_type => 'integer' },
31 # test that type name is lowercased
32 'INTEGER' => { data_type => 'integer' },
34 'bigint' => { data_type => 'bigint' },
35 'float' => { data_type => 'float' },
37 { data_type => 'double precision' },
38 'real' => { data_type => 'real' },
40 'float(2)' => { data_type => 'float', size => 2 },
41 'float(7)' => { data_type => 'float', size => 7 },
43 'decimal' => { data_type => 'decimal' },
44 'dec' => { data_type => 'dec' },
45 'numeric' => { data_type => 'numeric' },
47 'decimal(3)' => { data_type => 'decimal', size => 3 },
48 'numeric(3)' => { data_type => 'numeric', size => 3 },
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] },
55 'date' => { data_type => 'date' },
56 'timestamp DEFAULT CURRENT_TIMESTAMP'
57 => { data_type => 'timestamp', default_value => \'current_timestamp' },
58 'time' => { data_type => 'time' },
61 'char' => { data_type => 'char' },
62 'char(11)' => { data_type => 'char', size => 11 },
63 'varchar(20)' => { data_type => 'varchar', size => 20 },
67 # 'sqlite_' is reserved, so we use 'extra_'
69 CREATE TABLE "extra_loader_test1" (
70 "id" NOT NULL PRIMARY KEY,
71 "value" TEXT UNIQUE NOT NULL
75 CREATE TABLE extra_loader_test2 (
76 event_id INTEGER PRIMARY KEY
80 CREATE TABLE extra_loader_test3 (
81 person_id INTEGER PRIMARY KEY
84 # Wordy, newline-heavy SQL
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)
96 # make sure views are picked up
98 CREATE VIEW extra_loader_test5 AS SELECT * FROM extra_loader_test4
100 # Compound primary keys can't be autoinc in the DBIC sense
102 CREATE TABLE extra_loader_test6 (
106 PRIMARY KEY (id1, id2)
110 CREATE TABLE extra_loader_test7 (
114 PRIMARY KEY (id1, id2)
118 create table extra_loader_test8 (
119 id integer primary key
123 create table extra_loader_test9 (
124 id integer primary key,
126 foreign key (eight_id) references extra_loader_test8(id)
127 on delete restrict on update set null deferrable
130 # test inline constraint
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
138 pre_drop_ddl => [ 'DROP VIEW extra_loader_test5' ],
139 drop => [ qw/extra_loader_test1 extra_loader_test2 extra_loader_test3
140 extra_loader_test4 extra_loader_test6 extra_loader_test7
141 extra_loader_test8 extra_loader_test9 extra_loader_test10 / ],
144 my ($schema, $monikers, $classes) = @_;
146 ok ((my $rs = $schema->resultset($monikers->{extra_loader_test1})),
147 'resultset for quoted table');
149 ok ((my $source = $rs->result_source), 'source');
151 is_deeply [ $source->columns ], [ qw/id value/ ],
152 'retrieved quoted column names from quoted table';
154 ok ((exists $source->column_info('value')->{is_nullable}),
155 'is_nullable exists');
157 is $source->column_info('value')->{is_nullable}, 0,
158 'is_nullable is set correctly';
160 ok (($source = $schema->source($monikers->{extra_loader_test4})),
163 is_deeply [ $source->primary_columns ], [ qw/event_id person_id/ ],
164 'composite primary key';
166 is ($source->relationships, 2,
167 '2 foreign key constraints found');
169 # test that columns for views are picked up
170 is $schema->resultset($monikers->{extra_loader_test5})->result_source->column_info('person_id')->{data_type}, 'integer',
171 'columns for views are introspected';
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';
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};
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};
183 # test on delete/update fk clause introspection
184 ok ((my $rel_info = $schema->source('ExtraLoaderTest9')->relationship_info('eight')),
187 is $rel_info->{attrs}{on_delete}, 'RESTRICT',
188 'ON DELETE clause introspected correctly';
190 is $rel_info->{attrs}{on_update}, 'SET NULL',
191 'ON UPDATE clause introspected correctly';
193 is $rel_info->{attrs}{is_deferrable}, 1,
194 'DEFERRABLE clause introspected correctly';
196 ok (($rel_info = $schema->source('ExtraLoaderTest10')->relationship_info('eight')),
199 is $rel_info->{attrs}{on_delete}, 'RESTRICT',
200 'ON DELETE clause introspected correctly for inline FK';
202 is $rel_info->{attrs}{on_update}, 'SET NULL',
203 'ON UPDATE clause introspected correctly for inline FK';
205 is $rel_info->{attrs}{is_deferrable}, 1,
206 'DEFERRABLE clause introspected correctly for inline FK';
211 $tester->run_tests();
214 unlink "$tdir/sqlite_test" unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP};