054044924f38c45383b4fa884615975b02727d84
[dbsrgits/SQL-Translator.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
9 use SQL::Translator;
10 use SQL::Translator::Schema::Constants;
11
12 BEGIN {
13     maybe_plan(9,
14         'SQL::Translator::Parser::SQLite');
15 }
16 SQL::Translator::Parser::SQLite->import('parse');
17
18 my $file = "$Bin/data/sqlite/create.sql";
19
20 {
21     local $/;
22     open my $fh, "<$file" or die "Can't read file '$file': $!\n";
23     my $data = <$fh>;
24     my $t = SQL::Translator->new;
25     parse($t, $data);
26
27     my $schema = $t->schema;
28
29     my @tables = $schema->get_tables;
30     is( scalar @tables, 2, 'Parsed two tables' );
31
32     my $t1 = shift @tables;
33     is( $t1->name, 'person', "'Person' table" );
34
35     my $t2 = shift @tables;
36     is( $t2->name, 'pet', "'Pet' table" );
37
38     my @constraints = $t2->get_constraints;
39     is( scalar @constraints, 3, '3 constraints on pet' );
40
41     my $c1 = pop @constraints;
42     is( $c1->type, 'FOREIGN KEY', 'FK constraint' );
43     is( $c1->reference_table, 'person', 'References person table' );
44     is( join(',', $c1->reference_fields), 'person_id', 
45         'References person_id field' );
46
47     my @views = $schema->get_views;
48     is( scalar @views, 1, 'Parsed one views' );
49
50     my @triggers = $schema->get_triggers;
51     is( scalar @triggers, 1, 'Parsed one triggers' );
52 }