added preserve_case option
[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     data_types  => {
19         # SQLite ignores data types aside from INTEGER pks.
20         # We just test that they roundtrip sanely.
21         #
22         # Numeric types
23         'smallint'    => { data_type => 'smallint' },
24         'int'         => { data_type => 'int' },
25         'integer'     => { data_type => 'integer' },
26
27         # test that type name is lowercased
28         'INTEGER'     => { data_type => 'integer' },
29
30         'bigint'      => { data_type => 'bigint' },
31         'float'       => { data_type => 'float' },
32         'double precision' =>
33                          { data_type => 'double precision' },
34         'real'        => { data_type => 'real' },
35
36         'float(2)'    => { data_type => 'float', size => 2 },
37         'float(7)'    => { data_type => 'float', size => 7 },
38
39         'decimal'     => { data_type => 'decimal' },
40         'dec'         => { data_type => 'dec' },
41         'numeric'     => { data_type => 'numeric' },
42
43         'decimal(3)'   => { data_type => 'decimal', size => 3 },
44         'numeric(3)'   => { data_type => 'numeric', size => 3 },
45
46         'decimal(3,3)' => { data_type => 'decimal', size => [3,3] },
47         'dec(3,3)'     => { data_type => 'dec', size => [3,3] },
48         'numeric(3,3)' => { data_type => 'numeric', size => [3,3] },
49
50         # Date and Time Types
51         'date'        => { data_type => 'date' },
52         'timestamp DEFAULT CURRENT_TIMESTAMP'
53                       => { data_type => 'timestamp', default_value => \'CURRENT_TIMESTAMP' },
54         'time'        => { data_type => 'time' },
55
56         # String Types
57         'char'         => { data_type => 'char' },
58         'char(11)'     => { data_type => 'char',    size => 11 },
59         'varchar(20)'  => { data_type => 'varchar', size => 20 },
60     },
61     extra           => {
62         create => [
63             # 'sqlite_' is reserved, so we use 'extra_'
64             q{
65                 CREATE TABLE "extra_loader_test1" (
66                     "id" NOT NULL PRIMARY KEY,
67                     "value" TEXT UNIQUE NOT NULL
68                 )
69             },
70             q{
71                 CREATE TABLE extra_loader_test2 (
72                     event_id INTEGER PRIMARY KEY
73                 )
74             },
75             q{
76                 CREATE TABLE extra_loader_test3 (
77                     person_id INTEGER PRIMARY KEY
78                 )
79             },
80             # Wordy, newline-heavy SQL
81             q{
82                 CREATE TABLE extra_loader_test4 (
83                     event_id INTEGER NOT NULL
84                         CONSTRAINT fk_event_id
85                         REFERENCES extra_loader_test2(event_id),
86                     person_id INTEGER NOT NULL
87                         CONSTRAINT fk_person_id
88                         REFERENCES extra_loader_test3 (person_id),
89                     PRIMARY KEY (event_id, person_id)
90                 )
91             },
92             # make sure views are picked up
93             q{
94                 CREATE VIEW extra_loader_test5 AS SELECT * FROM extra_loader_test4
95             }
96         ],
97         pre_drop_ddl => [ 'DROP VIEW extra_loader_test5' ],
98         drop  => [ qw/extra_loader_test1 extra_loader_test2 extra_loader_test3 extra_loader_test4 / ],
99         count => 9,
100         run   => sub {
101             my ($schema, $monikers, $classes) = @_;
102
103             ok ((my $rs = $schema->resultset($monikers->{extra_loader_test1})),
104                 'resultset for quoted table');
105
106             ok ((my $source = $rs->result_source), 'source');
107
108             is_deeply [ $source->columns ], [ qw/id value/ ],
109                 'retrieved quoted column names from quoted table';
110
111             ok ((exists $source->column_info('value')->{is_nullable}),
112                 'is_nullable exists');
113
114             is $source->column_info('value')->{is_nullable}, 0,
115                 'is_nullable is set correctly';
116
117             ok (($source = $schema->source($monikers->{extra_loader_test4})),
118                 'verbose table');
119
120             is_deeply [ $source->primary_columns ], [ qw/event_id person_id/ ],
121                 'composite primary key';
122
123             is ($source->relationships, 2,
124                 '2 foreign key constraints found');
125
126             # test that columns for views are picked up
127             is $schema->resultset($monikers->{extra_loader_test5})->result_source->column_info('person_id')->{data_type}, 'integer',
128                 'columns for views are introspected';
129         },
130     },
131 );
132
133 $tester->run_tests();
134
135 END {
136     unlink './t/sqlite_test';
137 }