Commit | Line | Data |
5fcc85af |
1 | #!/usr/bin/perl |
2 | # vim: set ft=perl: |
3 | |
4 | use strict; |
2d691ec1 |
5 | use Test::More; |
6 | use Test::SQL::Translator qw(maybe_plan); |
153b2e77 |
7 | use FindBin qw/$Bin/; |
8 | |
5fcc85af |
9 | use SQL::Translator; |
5fcc85af |
10 | use SQL::Translator::Schema::Constants; |
11 | |
2d691ec1 |
12 | BEGIN { |
565563b9 |
13 | maybe_plan(25, |
2d691ec1 |
14 | 'SQL::Translator::Parser::SQLite'); |
15 | } |
16 | SQL::Translator::Parser::SQLite->import('parse'); |
17 | |
153b2e77 |
18 | my $file = "$Bin/data/sqlite/create.sql"; |
19 | |
5fcc85af |
20 | { |
153b2e77 |
21 | local $/; |
22 | open my $fh, "<$file" or die "Can't read file '$file': $!\n"; |
23 | my $data = <$fh>; |
5fcc85af |
24 | my $t = SQL::Translator->new; |
153b2e77 |
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 | |
03d12def |
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 | |
153b2e77 |
41 | my $t2 = shift @tables; |
42 | is( $t2->name, 'pet', "'Pet' table" ); |
43 | |
7d8f0a61 |
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' ); |
aee4b66e |
50 | is( join(',', $c1->reference_fields), 'person_id', |
7d8f0a61 |
51 | 'References person_id field' ); |
52 | |
153b2e77 |
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' ); |
5fcc85af |
58 | } |
7179cb09 |
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; |
565563b9 |
77 | is( scalar @constraints, 5, '5 constraints on pet' ); |
7179cb09 |
78 | |
565563b9 |
79 | my $c1 = $constraints[2]; |
7179cb09 |
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' ); |
ea4a3ecc |
83 | is( $c1->on_delete, 'RESTRICT', 'On delete restrict' ); |
84 | is( $c1->on_update, 'CASCADE', 'On update cascade' ); |
aee4b66e |
85 | is( join(',', $c1->reference_fields), 'person_id', |
7179cb09 |
86 | 'References person_id field' ); |
87 | |
565563b9 |
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 | |
7179cb09 |
96 | } |