Commit failing test for composite PKs get marked is_auto_increment
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 10sqlite_common.t
1 use strict;
2 use Test::More;
3 use lib qw(t/lib);
4 use dbixcsl_common_tests;
5
6 eval { require DBD::SQLite };
7 my $class = $@ ? 'SQLite2' : 'SQLite';
8
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        => '',
15     connect_info_opts => {
16         on_connect_do => 'PRAGMA foreign_keys = ON',
17     },
18     loader_options  => { preserve_case => 1 },
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' },
27
28         # test that type name is lowercased
29         'INTEGER'     => { data_type => 'integer' },
30
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'
54                       => { data_type => 'timestamp', default_value => \'current_timestamp' },
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     },
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,
68                     "value" TEXT UNIQUE NOT NULL
69                 )
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             },
81             # Wordy, newline-heavy SQL
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             },
93             # make sure views are picked up
94             q{
95                 CREATE VIEW extra_loader_test5 AS SELECT * FROM extra_loader_test4
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             },
114         ],
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/ ],
118         count => 11,
119         run   => sub {
120             my ($schema, $monikers, $classes) = @_;
121
122             ok ((my $rs = $schema->resultset($monikers->{extra_loader_test1})),
123                 'resultset for quoted table');
124
125             ok ((my $source = $rs->result_source), 'source');
126
127             is_deeply [ $source->columns ], [ qw/id value/ ],
128                 'retrieved quoted column names from quoted table';
129
130             ok ((exists $source->column_info('value')->{is_nullable}),
131                 'is_nullable exists');
132
133             is $source->column_info('value')->{is_nullable}, 0,
134                 'is_nullable is set correctly';
135
136             ok (($source = $schema->source($monikers->{extra_loader_test4})),
137                 'verbose table');
138
139             is_deeply [ $source->primary_columns ], [ qw/event_id person_id/ ],
140                 'composite primary key';
141
142             is ($source->relationships, 2,
143                 '2 foreign key constraints found');
144
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';
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};
154         },
155     },
156 );
157
158 $tester->run_tests();
159
160 END {
161     unlink './t/sqlite_test';
162 }