523bd0c5c7eb02b1dd961e8072933c86f39b94d0
[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         ],
98         pre_drop_ddl => [ 'DROP VIEW extra_loader_test5' ],
99         drop  => [ qw/extra_loader_test1 extra_loader_test2 extra_loader_test3 extra_loader_test4 / ],
100         count => 9,
101         run   => sub {
102             my ($schema, $monikers, $classes) = @_;
103
104             ok ((my $rs = $schema->resultset($monikers->{extra_loader_test1})),
105                 'resultset for quoted table');
106
107             ok ((my $source = $rs->result_source), 'source');
108
109             is_deeply [ $source->columns ], [ qw/id value/ ],
110                 'retrieved quoted column names from quoted table';
111
112             ok ((exists $source->column_info('value')->{is_nullable}),
113                 'is_nullable exists');
114
115             is $source->column_info('value')->{is_nullable}, 0,
116                 'is_nullable is set correctly';
117
118             ok (($source = $schema->source($monikers->{extra_loader_test4})),
119                 'verbose table');
120
121             is_deeply [ $source->primary_columns ], [ qw/event_id person_id/ ],
122                 'composite primary key';
123
124             is ($source->relationships, 2,
125                 '2 foreign key constraints found');
126
127             # test that columns for views are picked up
128             is $schema->resultset($monikers->{extra_loader_test5})->result_source->column_info('person_id')->{data_type}, 'integer',
129                 'columns for views are introspected';
130         },
131     },
132 );
133
134 $tester->run_tests();
135
136 END {
137     unlink './t/sqlite_test';
138 }