4 use dbixcsl_common_tests;
6 eval { require DBD::SQLite };
7 my $class = $@ ? 'SQLite2' : 'SQLite';
9 my $tester = dbixcsl_common_tests->new(
11 auto_inc_pk => 'INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT',
12 dsn => "dbi:$class:dbname=./t/sqlite_test",
15 connect_info_opts => {
16 on_connect_do => [ 'PRAGMA foreign_keys = ON', 'PRAGMA synchronous = OFF', ]
18 loader_options => { preserve_case => 1 },
20 # SQLite ignores data types aside from INTEGER pks.
21 # We just test that they roundtrip sanely.
24 'smallint' => { data_type => 'smallint' },
25 'int' => { data_type => 'int' },
26 'integer' => { data_type => 'integer' },
28 # test that type name is lowercased
29 'INTEGER' => { data_type => 'integer' },
31 'bigint' => { data_type => 'bigint' },
32 'float' => { data_type => 'float' },
34 { data_type => 'double precision' },
35 'real' => { data_type => 'real' },
37 'float(2)' => { data_type => 'float', size => 2 },
38 'float(7)' => { data_type => 'float', size => 7 },
40 'decimal' => { data_type => 'decimal' },
41 'dec' => { data_type => 'dec' },
42 'numeric' => { data_type => 'numeric' },
44 'decimal(3)' => { data_type => 'decimal', size => 3 },
45 'numeric(3)' => { data_type => 'numeric', size => 3 },
47 'decimal(3,3)' => { data_type => 'decimal', size => [3,3] },
48 'dec(3,3)' => { data_type => 'dec', size => [3,3] },
49 'numeric(3,3)' => { data_type => 'numeric', size => [3,3] },
52 'date' => { data_type => 'date' },
53 'timestamp DEFAULT CURRENT_TIMESTAMP'
54 => { data_type => 'timestamp', default_value => \'current_timestamp' },
55 'time' => { data_type => 'time' },
58 'char' => { data_type => 'char' },
59 'char(11)' => { data_type => 'char', size => 11 },
60 'varchar(20)' => { data_type => 'varchar', size => 20 },
64 # 'sqlite_' is reserved, so we use 'extra_'
66 CREATE TABLE "extra_loader_test1" (
67 "id" NOT NULL PRIMARY KEY,
68 "value" TEXT UNIQUE NOT NULL
72 CREATE TABLE extra_loader_test2 (
73 event_id INTEGER PRIMARY KEY
77 CREATE TABLE extra_loader_test3 (
78 person_id INTEGER PRIMARY KEY
81 # Wordy, newline-heavy SQL
83 CREATE TABLE extra_loader_test4 (
84 event_id INTEGER NOT NULL
85 CONSTRAINT fk_event_id
86 REFERENCES extra_loader_test2(event_id),
87 person_id INTEGER NOT NULL
88 CONSTRAINT fk_person_id
89 REFERENCES extra_loader_test3 (person_id),
90 PRIMARY KEY (event_id, person_id)
93 # make sure views are picked up
95 CREATE VIEW extra_loader_test5 AS SELECT * FROM extra_loader_test4
97 # Compound primary keys can't be autoinc in the DBIC sense
99 CREATE TABLE extra_loader_test6 (
103 PRIMARY KEY (id1, id2)
107 CREATE TABLE extra_loader_test7 (
111 PRIMARY KEY (id1, id2)
115 pre_drop_ddl => [ 'DROP VIEW extra_loader_test5' ],
116 drop => [ qw/extra_loader_test1 extra_loader_test2 extra_loader_test3
117 extra_loader_test4 extra_loader_test6 extra_loader_test7/ ],
120 my ($schema, $monikers, $classes) = @_;
122 ok ((my $rs = $schema->resultset($monikers->{extra_loader_test1})),
123 'resultset for quoted table');
125 ok ((my $source = $rs->result_source), 'source');
127 is_deeply [ $source->columns ], [ qw/id value/ ],
128 'retrieved quoted column names from quoted table';
130 ok ((exists $source->column_info('value')->{is_nullable}),
131 'is_nullable exists');
133 is $source->column_info('value')->{is_nullable}, 0,
134 'is_nullable is set correctly';
136 ok (($source = $schema->source($monikers->{extra_loader_test4})),
139 is_deeply [ $source->primary_columns ], [ qw/event_id person_id/ ],
140 'composite primary key';
142 is ($source->relationships, 2,
143 '2 foreign key constraints found');
145 # test that columns for views are picked up
146 is $schema->resultset($monikers->{extra_loader_test5})->result_source->column_info('person_id')->{data_type}, 'integer',
147 'columns for views are introspected';
149 isnt $schema->resultset($monikers->{extra_loader_test6})->result_source->column_info('id1')->{is_auto_increment}, 1,
150 q{two integer PKs don't get marked autoinc};
152 isnt $schema->resultset($monikers->{extra_loader_test7})->result_source->column_info('id1')->{is_auto_increment}, 1,
153 q{composite integer PK with non-integer PK doesn't get marked autoinc};
158 $tester->run_tests();
161 unlink './t/sqlite_test';