SQLite DDL parser test & data
[dbsrgits/SQL-Translator-2.0-ish.git] / t / 27sqlite-parser.t
1 #!/usr/bin/perl
2 # vim: set ft=perl:
3
4 use strict;
5 use Test::More;
6 use Test::SQL::Translator qw(maybe_plan);
7 use FindBin qw/$Bin/;
8 use SQL::Translator;
9 use SQL::Translator::Constants qw(:sqlt_types :sqlt_constants);
10
11 BEGIN {
12     maybe_plan(12, 'SQL::Translator::Parser::DDL::SQLite');
13 }
14
15 my $file = "$Bin/data/sqlite/create.sql";
16
17 {
18     local $/;
19     open my $fh, "<$file" or die "Can't read file '$file': $!\n";
20     my $data = <$fh>;
21     my $t = SQL::Translator->new({ from => 'SQLite' });
22     $t->parse($data);
23
24     my $schema = $t->schema;
25
26     my @tables = $schema->get_tables;
27     is( scalar @tables, 2, 'Parsed two tables' );
28
29     my $t1 = shift @tables;
30     is( $t1->name, 'person', "'Person' table" );
31
32     my @fields = $t1->get_fields;
33     is( scalar @fields, 6, 'Six fields in "person" table');
34     my $fld1 = shift @fields;
35     is( $fld1->name, 'person_id', 'First field is "person_id"');
36     is( $fld1->is_auto_increment, 1, 'Is an autoincrement field');
37
38     my $t2 = shift @tables;
39     is( $t2->name, 'pet', "'Pet' table" );
40
41     my @constraints = $t2->get_constraints;
42     is( scalar @constraints, 3, '3 constraints on pet' );
43
44     my $c1 = pop @constraints;
45     is( $c1->type, 'FOREIGN KEY', 'FK constraint' );
46     is( $c1->reference_table, 'person', 'References person table' );
47     is( join(',', $c1->reference_fields), 'person_id', 
48         'References person_id field' );
49
50     my @views = $schema->get_views;
51     is( scalar @views, 1, 'Parsed one views' );
52
53     my @triggers = $schema->get_triggers;
54     is( scalar @triggers, 1, 'Parsed one triggers' );
55 }