6 use Test::SQL::Translator qw(maybe_plan);
10 use SQL::Translator::Schema::Constants;
14 'SQL::Translator::Parser::SQLite');
16 SQL::Translator::Parser::SQLite->import('parse');
18 my $file = "$Bin/data/sqlite/create.sql";
22 open my $fh, "<$file" or die "Can't read file '$file': $!\n";
24 my $t = SQL::Translator->new;
27 my $schema = $t->schema;
29 my @tables = $schema->get_tables;
30 is( scalar @tables, 2, 'Parsed two tables' );
32 my $t1 = shift @tables;
33 is( $t1->name, 'person', "'Person' table" );
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');
41 my $t2 = shift @tables;
42 is( $t2->name, 'pet', "'Pet' table" );
44 my @constraints = $t2->get_constraints;
45 is( scalar @constraints, 3, '3 constraints on pet' );
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' );
53 my @views = $schema->get_views;
54 is( scalar @views, 1, 'Parsed one views' );
56 my @triggers = $schema->get_triggers;
57 is( scalar @triggers, 1, 'Parsed one triggers' );
60 $file = "$Bin/data/sqlite/named.sql";
63 open my $fh, "<$file" or die "Can't read file '$file': $!\n";
65 my $t = SQL::Translator->new;
68 my $schema = $t->schema;
70 my @tables = $schema->get_tables;
71 is( scalar @tables, 1, 'Parsed one table' );
73 my $t1 = shift @tables;
74 is( $t1->name, 'pet', "'Pet' table" );
76 my @constraints = $t1->get_constraints;
77 is( scalar @constraints, 5, '5 constraints on pet' );
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' );
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' );
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' );