From: Justin Hunter Date: Sat, 5 Sep 2009 16:47:22 +0000 (-0700) Subject: SQLite DDL parser test & data X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6019cb1caf6876293f8c3dfb2a1a1635dc268b02;p=dbsrgits%2FSQL-Translator-2.0-ish.git SQLite DDL parser test & data --- diff --git a/t/27sqlite-parser.t b/t/27sqlite-parser.t new file mode 100644 index 0000000..37d8d0a --- /dev/null +++ b/t/27sqlite-parser.t @@ -0,0 +1,55 @@ +#!/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::Constants qw(:sqlt_types :sqlt_constants); + +BEGIN { + maybe_plan(12, 'SQL::Translator::Parser::DDL::SQLite'); +} + +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({ from => 'SQLite' }); + $t->parse($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' ); +} diff --git a/t/data/sqlite/create.sql b/t/data/sqlite/create.sql new file mode 100644 index 0000000..f7a397f --- /dev/null +++ b/t/data/sqlite/create.sql @@ -0,0 +1,31 @@ +create table person ( + person_id INTEGER PRIMARY KEY AUTOINCREMENT, + 'name' varchar(20) not null, + 'age' integer, + weight double(11,2), + iq tinyint default '0', + description text +); + +create unique index u_name on person (name); + +create table pet ( + "pet_id" int, + "person_id" int references person (person_id), + "name" varchar(30), + "age" int, + check ( age < 100 ), + primary key (pet_id, person_id) +); + +create trigger pet_trig after insert on pet + begin + update pet set name=name; + end +; + +create view person_pet as + select pr.person_id, pr.name as person_name, pt.name as pet_name + from person pr, pet pt + where person.person_id=pet.pet_id +;