rename vendor tests
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 10_01sqlite_common.t
CommitLineData
a78e3fed 1use strict;
8763ffda 2use Test::More;
c2849787 3use lib qw(t/lib);
fbd83464 4use dbixcsl_common_tests;
c213fd3d 5use dbixcsl_test_dir qw/$tdir/;
a78e3fed 6
7eval { require DBD::SQLite };
8my $class = $@ ? 'SQLite2' : 'SQLite';
9
8763ffda 10my $tester = dbixcsl_common_tests->new(
11 vendor => 'SQLite',
12 auto_inc_pk => 'INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT',
c213fd3d 13 dsn => "dbi:$class:dbname=$tdir/sqlite_test",
8763ffda 14 user => '',
15 password => '',
b308dcca 16 connect_info_opts => {
be9f4d42 17 on_connect_do => [ 'PRAGMA foreign_keys = ON', 'PRAGMA synchronous = OFF', ]
b308dcca 18 },
eb040f78 19 loader_options => { preserve_case => 1 },
62bc1e5d 20 data_types => {
21 # SQLite ignores data types aside from INTEGER pks.
22 # We just test that they roundtrip sanely.
23 #
24 # Numeric types
25 'smallint' => { data_type => 'smallint' },
26 'int' => { data_type => 'int' },
27 'integer' => { data_type => 'integer' },
bc1cb85e 28
29 # test that type name is lowercased
30 'INTEGER' => { data_type => 'integer' },
31
62bc1e5d 32 'bigint' => { data_type => 'bigint' },
33 'float' => { data_type => 'float' },
34 'double precision' =>
35 { data_type => 'double precision' },
36 'real' => { data_type => 'real' },
37
38 'float(2)' => { data_type => 'float', size => 2 },
39 'float(7)' => { data_type => 'float', size => 7 },
40
41 'decimal' => { data_type => 'decimal' },
42 'dec' => { data_type => 'dec' },
43 'numeric' => { data_type => 'numeric' },
44
45 'decimal(3)' => { data_type => 'decimal', size => 3 },
46 'numeric(3)' => { data_type => 'numeric', size => 3 },
47
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] },
51
52 # Date and Time Types
53 'date' => { data_type => 'date' },
54 'timestamp DEFAULT CURRENT_TIMESTAMP'
007e3511 55 => { data_type => 'timestamp', default_value => \'current_timestamp' },
62bc1e5d 56 'time' => { data_type => 'time' },
57
58 # String Types
59 'char' => { data_type => 'char' },
60 'char(11)' => { data_type => 'char', size => 11 },
61 'varchar(20)' => { data_type => 'varchar', size => 20 },
62 },
8763ffda 63 extra => {
64 create => [
65 # 'sqlite_' is reserved, so we use 'extra_'
66 q{
67 CREATE TABLE "extra_loader_test1" (
68 "id" NOT NULL PRIMARY KEY,
2a6dfbc9 69 "value" TEXT UNIQUE NOT NULL
8763ffda 70 )
68d650df 71 },
72 q{
73 CREATE TABLE extra_loader_test2 (
74 event_id INTEGER PRIMARY KEY
75 )
76 },
77 q{
78 CREATE TABLE extra_loader_test3 (
79 person_id INTEGER PRIMARY KEY
80 )
81 },
26da4cc3 82 # Wordy, newline-heavy SQL
68d650df 83 q{
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)
92 )
93 },
26da4cc3 94 # make sure views are picked up
95 q{
96 CREATE VIEW extra_loader_test5 AS SELECT * FROM extra_loader_test4
9dfbfb58 97 },
98 # Compound primary keys can't be autoinc in the DBIC sense
99 q{
100 CREATE TABLE extra_loader_test6 (
101 id1 INTEGER,
102 id2 INTEGER,
103 value INTEGER,
104 PRIMARY KEY (id1, id2)
105 )
106 },
107 q{
108 CREATE TABLE extra_loader_test7 (
109 id1 INTEGER,
110 id2 TEXT,
111 value DECIMAL,
112 PRIMARY KEY (id1, id2)
113 )
114 },
8763ffda 115 ],
26da4cc3 116 pre_drop_ddl => [ 'DROP VIEW extra_loader_test5' ],
9dfbfb58 117 drop => [ qw/extra_loader_test1 extra_loader_test2 extra_loader_test3
118 extra_loader_test4 extra_loader_test6 extra_loader_test7/ ],
119 count => 11,
8763ffda 120 run => sub {
121 my ($schema, $monikers, $classes) = @_;
a78e3fed 122
8763ffda 123 ok ((my $rs = $schema->resultset($monikers->{extra_loader_test1})),
124 'resultset for quoted table');
125
0fa48bf5 126 ok ((my $source = $rs->result_source), 'source');
127
128 is_deeply [ $source->columns ], [ qw/id value/ ],
8763ffda 129 'retrieved quoted column names from quoted table';
68d650df 130
3b134b29 131 ok ((exists $source->column_info('value')->{is_nullable}),
132 'is_nullable exists');
133
0fa48bf5 134 is $source->column_info('value')->{is_nullable}, 0,
3b134b29 135 'is_nullable is set correctly';
0fa48bf5 136
137 ok (($source = $schema->source($monikers->{extra_loader_test4})),
68d650df 138 'verbose table');
139
140 is_deeply [ $source->primary_columns ], [ qw/event_id person_id/ ],
141 'composite primary key';
142
68d650df 143 is ($source->relationships, 2,
144 '2 foreign key constraints found');
145
bf558f72 146 # test that columns for views are picked up
00805149 147 is $schema->resultset($monikers->{extra_loader_test5})->result_source->column_info('person_id')->{data_type}, 'integer',
bf558f72 148 'columns for views are introspected';
9dfbfb58 149
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};
152
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};
8763ffda 155 },
156 },
157);
158
159$tester->run_tests();
a78e3fed 160
161END {
c213fd3d 162 unlink "$tdir/sqlite_test";
a78e3fed 163}