Create unit test for ::Parser::DBI::PostgreSQL, fix parser namespace lookup
[dbsrgits/SQL-Translator.git] / t / 66-postgres-dbi-parser.t
CommitLineData
122353c5 1#!/usr/bin/perl
2# vim: set ft=perl:
3
4use strict;
5use Test::More;
6use SQL::Translator;
7use SQL::Translator::Schema::Constants;
8use Test::SQL::Translator qw(maybe_plan table_ok);
9
10BEGIN {
11 maybe_plan(49, 'SQL::Translator::Parser::DBI::PostgreSQL');
12 SQL::Translator::Parser::DBI::PostgreSQL->import('parse');
13}
14
15use_ok('SQL::Translator::Parser::DBI::PostgreSQL');
16
17my @dsn =
18 $ENV{DBICTEST_PG_DSN} ? @ENV{ map { "DBICTEST_PG_$_" } qw/DSN USER PASS/ }
19: $ENV{DBI_DSN} ? @ENV{ map { "DBI_$_" } qw/DSN USER PASS/ }
20: ( "dbi:Pg:dbname=postgres", '', '' );
21
22my $dbh = eval {
23 DBI->connect(@dsn, {AutoCommit => 1, RaiseError=>1,PrintError => 1} );
24};
25
26SKIP: {
27 if (my $err = ($@ || $DBI::err )) {
28 chomp $err;
29 skip "No connection to test db. DBI says '$err'", 48;
30 }
31
32 ok($dbh, "dbh setup correctly");
33 $dbh->do('SET client_min_messages=WARNING');
34
35my $t = SQL::Translator->new( trace => 0 );
36my $sql = q[
37 drop table if exists sqlt_test2;
38 drop table if exists sqlt_test1;
39 drop table if exists sqlt_products_1;
40
41 create table sqlt_test1 (
42 f_serial serial NOT NULL primary key,
43 f_varchar character varying (255),
44 f_text text default 'FOO'
45 );
46
47
48 create table sqlt_test2 (
49 f_id integer NOT NULL,
50 f_int smallint,
51 primary key (f_id),
52 f_fk1 integer NOT NULL references sqlt_test1 (f_serial)
53 );
54
55 CREATE TABLE sqlt_products_1 (
56 product_no integer,
57 name text,
58 price numeric
59 );
60];
61
62$| = 1;
63
64$dbh->do($sql);
65
66my $data = SQL::Translator::Parser::DBI::PostgreSQL::parse( $t, $dbh );
67my $schema = $t->schema;
68
69isa_ok( $schema, 'SQL::Translator::Schema', 'Schema object' );
70my @tables = $schema->get_tables;
71
72my $t1 = $schema->get_table("sqlt_test1");
73is( $t1->name, 'sqlt_test1', 'Table sqlt_test1 exists' );
74
75my @t1_fields = $t1->get_fields;
76is( scalar @t1_fields, 3, '3 fields in sqlt_test1' );
77
78my $f1 = shift @t1_fields;
79is( $f1->name, 'f_serial', 'First field is "f_serial"' );
80#FIXME: it should better be 'INTEGER' instead of 'int4'
81is( $f1->data_type, 'int4', 'Field is an integer' );
82is( $f1->is_nullable, 0, 'Field cannot be null' );
83is( $f1->default_value, "nextval('sqlt_test1_f_serial_seq'::regclass)", 'Default value is nextval()' );
84is( $f1->is_primary_key, 1, 'Field is PK' );
85#FIXME: not set to auto-increment? maybe we can guess auto-increment behavior by looking at the default_value (i.e. it call function nextval() )
86#is( $f1->is_auto_increment, 1, 'Field is auto increment' );
87
88my $f2 = shift @t1_fields;
89is( $f2->name, 'f_varchar', 'Second field is "f_varchar"' );
90is( $f2->data_type, 'varchar', 'Field is a varchar' );
91is( $f2->is_nullable, 1, 'Field can be null' );
92#FIXME: should not be 255?
93is( $f2->size, 259, 'Size is "259"' );
94is( $f2->default_value, undef, 'Default value is undefined' );
95is( $f2->is_primary_key, 0, 'Field is not PK' );
96is( $f2->is_auto_increment, 0, 'Field is not auto increment' );
97
98my $f3 = shift @t1_fields;
99is( $f3->name, 'f_text', 'Third field is "f_text"' );
100is( $f3->data_type, 'text', 'Field is a text' );
101is( $f3->is_nullable, 1, 'Field can be null' );
102is( $f3->size, 0, 'Size is 0' );
103is( $f3->default_value, "'FOO'::text", 'Default value is "FOO"' );
104is( $f3->is_primary_key, 0, 'Field is not PK' );
105is( $f3->is_auto_increment, 0, 'Field is not auto increment' );
106
107#TODO: no 'NOT NULL' constraint not set
108
109my $t2 = $schema->get_table("sqlt_test2");
110is( $t2->name, 'sqlt_test2', 'Table sqlt_test2 exists' );
111
112my @t2_fields = $t2->get_fields;
113is( scalar @t2_fields, 3, '3 fields in sqlt_test2' );
114
115my $t2_f1 = shift @t2_fields;
116is( $t2_f1->name, 'f_id', 'First field is "f_id"' );
117is( $t2_f1->data_type, 'int4', 'Field is an integer' );
118is( $t2_f1->is_nullable, 0, 'Field cannot be null' );
119is( $t2_f1->size, 0, 'Size is "0"' );
120is( $t2_f1->default_value, undef, 'Default value is undefined' );
121is( $t2_f1->is_primary_key, 1, 'Field is PK' );
122
123my $t2_f2= shift @t2_fields;
124is( $t2_f2->name, 'f_int', 'Third field is "f_int"' );
125is( $t2_f2->data_type, 'int2', 'Field is an integer' );
126is( $t2_f2->is_nullable, 1, 'Field can be null' );
127is( $t2_f2->size, 0, 'Size is "0"' );
128is( $t2_f2->default_value, undef, 'Default value is undefined' );
129is( $t2_f2->is_primary_key, 0, 'Field is not PK' );
130
131my $t2_f3 = shift @t2_fields;
132is( $t2_f3->name, 'f_fk1', 'Third field is "f_fk1"' );
133is( $t2_f3->data_type, 'int4', 'Field is an integer' );
134is( $t2_f3->is_nullable, 0, 'Field cannot be null' );
135is( $t2_f3->size, 0, 'Size is "0"' );
136is( $t2_f3->default_value, undef, 'Default value is undefined' );
137is( $t2_f3->is_primary_key, 0, 'Field is not PK' );
138is( $t2_f3->is_foreign_key, 1, 'Field is a FK' );
139my $fk_ref1 = $t2_f3->foreign_key_reference;
140isa_ok( $fk_ref1, 'SQL::Translator::Schema::Constraint', 'FK' );
141is( $fk_ref1->reference_table, 'sqlt_test1', 'FK is to "sqlt_test1" table' );
142
143my @t2_constraints = $t2->get_constraints;
144is( scalar @t2_constraints, 1, "One constraint on table" );
145
146my $t2_c1 = shift @t2_constraints;
147is( $t2_c1->type, FOREIGN_KEY, "Constraint is a FK" );
148
149$dbh->disconnect;
150} # end of SKIP block
151
152END {
153 if ($dbh) {
154 for (
155 'drop table if exists sqlt_test2',
156 'drop table if exists sqlt_test1',
157 'drop table if exists sqlt_products_1',
158 ) {
159 eval { $dbh->do($_) };
160 }
161 }
162}