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