Combined patches from RT#70734 and RT#44769
[dbsrgits/SQL-Translator.git] / t / 27sqlite-parser.t
CommitLineData
5fcc85af 1#!/usr/bin/perl
2# vim: set ft=perl:
3
4use strict;
2d691ec1 5use Test::More;
6use Test::SQL::Translator qw(maybe_plan);
153b2e77 7use FindBin qw/$Bin/;
8
5fcc85af 9use SQL::Translator;
5fcc85af 10use SQL::Translator::Schema::Constants;
11
2d691ec1 12BEGIN {
ea4a3ecc 13 maybe_plan(21,
2d691ec1 14 'SQL::Translator::Parser::SQLite');
15}
16SQL::Translator::Parser::SQLite->import('parse');
17
153b2e77 18my $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;
77 is( scalar @constraints, 3, '3 constraints on pet' );
78
79 my $c1 = pop @constraints;
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
88}