File: 27sqlite-parser.t

package info (click to toggle)
libsql-translator-perl 0.11011-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,380 kB
  • sloc: perl: 251,748; sql: 3,805; xml: 233; makefile: 7
file content (88 lines) | stat: -rw-r--r-- 2,512 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/perl
# vim: set ft=perl:

use strict;
use Test::More;
use Test::SQL::Translator qw(maybe_plan);
use FindBin qw/$Bin/;

use SQL::Translator;
use SQL::Translator::Schema::Constants;

BEGIN {
    maybe_plan(21,
        'SQL::Translator::Parser::SQLite');
}
SQL::Translator::Parser::SQLite->import('parse');

my $file = "$Bin/data/sqlite/create.sql";

{
    local $/;
    open my $fh, "<$file" or die "Can't read file '$file': $!\n";
    my $data = <$fh>;
    my $t = SQL::Translator->new;
    parse($t, $data);

    my $schema = $t->schema;

    my @tables = $schema->get_tables;
    is( scalar @tables, 2, 'Parsed two tables' );

    my $t1 = shift @tables;
    is( $t1->name, 'person', "'Person' table" );

    my @fields = $t1->get_fields;
    is( scalar @fields, 6, 'Six fields in "person" table');
    my $fld1 = shift @fields;
    is( $fld1->name, 'person_id', 'First field is "person_id"');
    is( $fld1->is_auto_increment, 1, 'Is an autoincrement field');

    my $t2 = shift @tables;
    is( $t2->name, 'pet', "'Pet' table" );

    my @constraints = $t2->get_constraints;
    is( scalar @constraints, 3, '3 constraints on pet' );

    my $c1 = pop @constraints;
    is( $c1->type, 'FOREIGN KEY', 'FK constraint' );
    is( $c1->reference_table, 'person', 'References person table' );
    is( join(',', $c1->reference_fields), 'person_id',
        'References person_id field' );

    my @views = $schema->get_views;
    is( scalar @views, 1, 'Parsed one views' );

    my @triggers = $schema->get_triggers;
    is( scalar @triggers, 1, 'Parsed one triggers' );
}

$file = "$Bin/data/sqlite/named.sql";
{
    local $/;
    open my $fh, "<$file" or die "Can't read file '$file': $!\n";
    my $data = <$fh>;
    my $t = SQL::Translator->new;
    parse($t, $data);

    my $schema = $t->schema;

    my @tables = $schema->get_tables;
    is( scalar @tables, 1, 'Parsed one table' );

    my $t1 = shift @tables;
    is( $t1->name, 'pet', "'Pet' table" );

    my @constraints = $t1->get_constraints;
    is( scalar @constraints, 3, '3 constraints on pet' );

    my $c1 = pop @constraints;
    is( $c1->type, 'FOREIGN KEY', 'FK constraint' );
    is( $c1->reference_table, 'person', 'References person table' );
    is( $c1->name, 'fk_person_id', 'Constraint name fk_person_id' );
    is( $c1->on_delete, 'RESTRICT', 'On delete restrict' );
    is( $c1->on_update, 'CASCADE', 'On update cascade' );
    is( join(',', $c1->reference_fields), 'person_id',
        'References person_id field' );

}