8676761550825678259d7d26f2f112191f2785a3
[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     extra           => {
19         create => [
20             # 'sqlite_' is reserved, so we use 'extra_'
21             q{
22                 CREATE TABLE "extra_loader_test1" (
23                     "id" NOT NULL PRIMARY KEY,
24                     "value" TEXT UNIQUE NOT NULL
25                 )
26             },
27             q{
28                 CREATE TABLE extra_loader_test2 (
29                     event_id INTEGER PRIMARY KEY
30                 )
31             },
32             q{
33                 CREATE TABLE extra_loader_test3 (
34                     person_id INTEGER PRIMARY KEY
35                 )
36             },
37             # Wordy, newline-heavy SQL
38             q{
39                 CREATE TABLE extra_loader_test4 (
40                     event_id INTEGER NOT NULL
41                         CONSTRAINT fk_event_id
42                         REFERENCES extra_loader_test2(event_id),
43                     person_id INTEGER NOT NULL
44                         CONSTRAINT fk_person_id
45                         REFERENCES extra_loader_test3 (person_id),
46                     PRIMARY KEY (event_id, person_id)
47                 )
48             },
49             # make sure views are picked up
50             q{
51                 CREATE VIEW extra_loader_test5 AS SELECT * FROM extra_loader_test4
52             }
53         ],
54         pre_drop_ddl => [ 'DROP VIEW extra_loader_test5' ],
55         drop  => [ qw/extra_loader_test1 extra_loader_test2 extra_loader_test3 extra_loader_test4 / ],
56         count => 9,
57         run   => sub {
58             my ($schema, $monikers, $classes) = @_;
59
60             ok ((my $rs = $schema->resultset($monikers->{extra_loader_test1})),
61                 'resultset for quoted table');
62
63             ok ((my $source = $rs->result_source), 'source');
64
65             is_deeply [ $source->columns ], [ qw/id value/ ],
66                 'retrieved quoted column names from quoted table';
67
68             ok ((exists $source->column_info('value')->{is_nullable}),
69                 'is_nullable exists');
70
71             is $source->column_info('value')->{is_nullable}, 0,
72                 'is_nullable is set correctly';
73
74             ok (($source = $schema->source($monikers->{extra_loader_test4})),
75                 'verbose table');
76
77             is_deeply [ $source->primary_columns ], [ qw/event_id person_id/ ],
78                 'composite primary key';
79
80             is ($source->relationships, 2,
81                 '2 foreign key constraints found');
82
83             # test that columns for views are picked up
84             is $schema->resultset($monikers->{extra_loader_test5})->result_source->column_info('person_id')->{data_type}, 'INTEGER',
85                 'columns for views are introspected';
86         },
87     },
88 );
89
90 $tester->run_tests();
91
92 END {
93     unlink './t/sqlite_test';
94 }