1#!/usr/bin/perl
2# vim: set ft=perl:
3
4use strict;
5use Test::More;
6use Test::SQL::Translator qw(maybe_plan);
7use FindBin qw/$Bin/;
8
9use SQL::Translator;
10use SQL::Translator::Schema::Constants;
11
12BEGIN {
13    maybe_plan(25,
14        'SQL::Translator::Parser::SQLite');
15}
16SQL::Translator::Parser::SQLite->import('parse');
17
18my $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 @fields = $t1->get_fields;
36    is( scalar @fields, 6, 'Six fields in "person" table');
37    my $fld1 = shift @fields;
38    is( $fld1->name, 'person_id', 'First field is "person_id"');
39    is( $fld1->is_auto_increment, 1, 'Is an autoincrement field');
40
41    my $t2 = shift @tables;
42    is( $t2->name, 'pet', "'Pet' table" );
43
44    my @constraints = $t2->get_constraints;
45    is( scalar @constraints, 3, '3 constraints on pet' );
46
47    my $c1 = pop @constraints;
48    is( $c1->type, 'FOREIGN KEY', 'FK constraint' );
49    is( $c1->reference_table, 'person', 'References person table' );
50    is( join(',', $c1->reference_fields), 'person_id',
51        'References person_id field' );
52
53    my @views = $schema->get_views;
54    is( scalar @views, 1, 'Parsed one views' );
55
56    my @triggers = $schema->get_triggers;
57    is( scalar @triggers, 1, 'Parsed one triggers' );
58}
59
60$file = "$Bin/data/sqlite/named.sql";
61{
62    local $/;
63    open my $fh, "<$file" or die "Can't read file '$file': $!\n";
64    my $data = <$fh>;
65    my $t = SQL::Translator->new;
66    parse($t, $data);
67
68    my $schema = $t->schema;
69
70    my @tables = $schema->get_tables;
71    is( scalar @tables, 1, 'Parsed one table' );
72
73    my $t1 = shift @tables;
74    is( $t1->name, 'pet', "'Pet' table" );
75
76    my @constraints = $t1->get_constraints;
77    is( scalar @constraints, 5, '5 constraints on pet' );
78
79    my $c1 = $constraints[2];
80    is( $c1->type, 'FOREIGN KEY', 'FK constraint' );
81    is( $c1->reference_table, 'person', 'References person table' );
82    is( $c1->name, 'fk_person_id', 'Constraint name fk_person_id' );
83    is( $c1->on_delete, 'RESTRICT', 'On delete restrict' );
84    is( $c1->on_update, 'CASCADE', 'On update cascade' );
85    is( join(',', $c1->reference_fields), 'person_id',
86        'References person_id field' );
87
88    my $c2 = $constraints[3];
89    is( $c2->on_delete, 'SET DEFAULT', 'On delete set default' );
90    is( $c2->on_update, 'SET NULL', 'On update set null' );
91
92    my $c3 = $constraints[4];
93    is( $c3->on_update, 'NO ACTION', 'On update no action' );
94    is( $c3->on_delete, '', 'On delete not defined' );
95
96}
97