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 },
21 # SQLite ignores data types aside from INTEGER pks.
22 # We just test that they roundtrip sanely.
25 'smallint' => { data_type => 'smallint' },
26 'int' => { data_type => 'int' },
27 'integer' => { data_type => 'integer' },
29 # test that type name is lowercased
30 'INTEGER' => { data_type => 'integer' },
32 'bigint' => { data_type => 'bigint' },
33 'float' => { data_type => 'float' },
35 { data_type => 'double precision' },
36 'real' => { data_type => 'real' },
38 'float(2)' => { data_type => 'float', size => 2 },
39 'float(7)' => { data_type => 'float', size => 7 },
41 'decimal' => { data_type => 'decimal' },
42 'dec' => { data_type => 'dec' },
43 'numeric' => { data_type => 'numeric' },
45 'decimal(3)' => { data_type => 'decimal', size => 3 },
46 'numeric(3)' => { data_type => 'numeric', size => 3 },
48 'decimal(3,3)' => { data_type => 'decimal', size => [3,3] },
49 'dec(3,3)' => { data_type => 'dec', size => [3,3] },
50 'numeric(3,3)' => { data_type => 'numeric', size => [3,3] },
53 'date' => { data_type => 'date' },
54 'timestamp DEFAULT CURRENT_TIMESTAMP'
55 => { data_type => 'timestamp', default_value => \'current_timestamp' },
56 'time' => { data_type => 'time' },
59 'char' => { data_type => 'char' },
60 'char(11)' => { data_type => 'char', size => 11 },
61 'varchar(20)' => { data_type => 'varchar', size => 20 },
65 # 'sqlite_' is reserved, so we use 'extra_'
67 CREATE TABLE "extra_loader_test1" (
68 "id" NOT NULL PRIMARY KEY,
69 "value" TEXT UNIQUE NOT NULL
73 CREATE TABLE extra_loader_test2 (
74 event_id INTEGER PRIMARY KEY
78 CREATE TABLE extra_loader_test3 (
79 person_id INTEGER PRIMARY KEY
82 # Wordy, newline-heavy SQL
84 CREATE TABLE extra_loader_test4 (
85 event_id INTEGER NOT NULL
86 CONSTRAINT fk_event_id
87 REFERENCES extra_loader_test2(event_id),
88 person_id INTEGER NOT NULL
89 CONSTRAINT fk_person_id
90 REFERENCES extra_loader_test3 (person_id),
91 PRIMARY KEY (event_id, person_id)
94 # make sure views are picked up
96 CREATE VIEW extra_loader_test5 AS SELECT * FROM extra_loader_test4
98 # Compound primary keys can't be autoinc in the DBIC sense
100 CREATE TABLE extra_loader_test6 (
104 PRIMARY KEY (id1, id2)
108 CREATE TABLE extra_loader_test7 (
112 PRIMARY KEY (id1, id2)
116 pre_drop_ddl => [ 'DROP VIEW extra_loader_test5' ],
117 drop => [ qw/extra_loader_test1 extra_loader_test2 extra_loader_test3
118 extra_loader_test4 extra_loader_test6 extra_loader_test7/ ],
121 my ($schema, $monikers, $classes) = @_;
123 ok ((my $rs = $schema->resultset($monikers->{extra_loader_test1})),
124 'resultset for quoted table');
126 ok ((my $source = $rs->result_source), 'source');
128 is_deeply [ $source->columns ], [ qw/id value/ ],
129 'retrieved quoted column names from quoted table';
131 ok ((exists $source->column_info('value')->{is_nullable}),
132 'is_nullable exists');
134 is $source->column_info('value')->{is_nullable}, 0,
135 'is_nullable is set correctly';
137 ok (($source = $schema->source($monikers->{extra_loader_test4})),
140 is_deeply [ $source->primary_columns ], [ qw/event_id person_id/ ],
141 'composite primary key';
143 is ($source->relationships, 2,
144 '2 foreign key constraints found');
146 # test that columns for views are picked up
147 is $schema->resultset($monikers->{extra_loader_test5})->result_source->column_info('person_id')->{data_type}, 'integer',
148 'columns for views are introspected';
150 isnt $schema->resultset($monikers->{extra_loader_test6})->result_source->column_info('id1')->{is_auto_increment}, 1,
151 q{two integer PKs don't get marked autoinc};
153 isnt $schema->resultset($monikers->{extra_loader_test7})->result_source->column_info('id1')->{is_auto_increment}, 1,
154 q{composite integer PK with non-integer PK doesn't get marked autoinc};
159 $tester->run_tests();
162 unlink "$tdir/sqlite_test";