Updated YAML test.
[dbsrgits/SQL-Translator.git] / t / 14postgres-parser.t
CommitLineData
f4eb75f3 1#!/usr/bin/perl
2
3use strict;
668c5039 4use Test::More tests => 117;
f4eb75f3 5use SQL::Translator;
07b82495 6use SQL::Translator::Schema::Constants;
f4eb75f3 7use SQL::Translator::Parser::PostgreSQL qw(parse);
8
9my $t = SQL::Translator->new( trace => 0 );
10my $sql = q[
11 create table t_test1 (
07b82495 12 f_serial serial NOT NULL default '0' primary key,
f4eb75f3 13 f_varchar character varying (255),
14 f_double double precision,
07b82495 15 f_bigint bigint not null,
f4eb75f3 16 f_char character(10),
17 f_bool boolean,
07b82495 18 f_bin bytea,
f4eb75f3 19 f_tz timestamp,
20 f_text text,
21 f_fk1 integer not null references t_test2 (f_id),
22 f_fk2 integer
23 );
24
25 create table t_test2 (
26 f_id integer NOT NULL,
27 f_varchar varchar(25),
668c5039 28 f_int smallint,
29 primary key (f_id),
30 check (f_int between 1 and 5)
f4eb75f3 31 );
32
33 alter table only t_test1 add constraint c_u1 unique (f_varchar);
34
07b82495 35 alter table only t_test1 add constraint "c_fk2" foreign key (f_fk2)
f4eb75f3 36 references t_test2 (f_id) on update no action on delete cascade;
37];
38
39$| = 1;
40
07b82495 41my $data = parse( $t, $sql );
42my $schema = $t->schema;
43
44isa_ok( $schema, 'SQL::Translator::Schema', 'Schema object' );
45my @tables = $schema->get_tables;
46is( scalar @tables, 2, 'Two tables' );
47
48my $t1 = shift @tables;
49is( $t1->name, 't_test1', 'Table t_test1 exists' );
50
51my @t1_fields = $t1->get_fields;
52is( scalar @t1_fields, 11, '11 fields in t_test1' );
53
54my $f1 = shift @t1_fields;
55is( $f1->name, 'f_serial', 'First field is "f_serial"' );
56is( $f1->data_type, 'integer', 'Field is an integer' );
57is( $f1->is_nullable, 0, 'Field cannot be null' );
9bcfe960 58is( $f1->size, 11, 'Size is "11"' );
07b82495 59is( $f1->default_value, '0', 'Default value is "0"' );
60is( $f1->is_primary_key, 1, 'Field is PK' );
6d278664 61is( $f1->is_auto_increment, 1, 'Field is auto increment' );
07b82495 62
63my $f2 = shift @t1_fields;
64is( $f2->name, 'f_varchar', 'Second field is "f_varchar"' );
65is( $f2->data_type, 'varchar', 'Field is a varchar' );
66is( $f2->is_nullable, 1, 'Field can be null' );
67is( $f2->size, 255, 'Size is "255"' );
68is( $f2->default_value, undef, 'Default value is undefined' );
69is( $f2->is_primary_key, 0, 'Field is not PK' );
6d278664 70is( $f2->is_auto_increment, 0, 'Field is not auto increment' );
07b82495 71
72my $f3 = shift @t1_fields;
73is( $f3->name, 'f_double', 'Third field is "f_double"' );
74is( $f3->data_type, 'float', 'Field is a float' );
75is( $f3->is_nullable, 1, 'Field can be null' );
9bcfe960 76is( $f3->size, 20, 'Size is "20"' );
07b82495 77is( $f3->default_value, undef, 'Default value is undefined' );
78is( $f3->is_primary_key, 0, 'Field is not PK' );
79
80my $f4 = shift @t1_fields;
81is( $f4->name, 'f_bigint', 'Fourth field is "f_bigint"' );
82is( $f4->data_type, 'integer', 'Field is an integer' );
83is( $f4->is_nullable, 0, 'Field cannot be null' );
9bcfe960 84is( $f4->size, 20, 'Size is "20"' );
07b82495 85is( $f4->default_value, undef, 'Default value is undefined' );
86is( $f4->is_primary_key, 0, 'Field is not PK' );
87
88my $f5 = shift @t1_fields;
89is( $f5->name, 'f_char', 'Fifth field is "f_char"' );
90is( $f5->data_type, 'char', 'Field is char' );
91is( $f5->is_nullable, 1, 'Field can be null' );
92is( $f5->size, 10, 'Size is "10"' );
93is( $f5->default_value, undef, 'Default value is undefined' );
94is( $f5->is_primary_key, 0, 'Field is not PK' );
95
96my $f6 = shift @t1_fields;
97is( $f6->name, 'f_bool', 'Sixth field is "f_bool"' );
98is( $f6->data_type, 'boolean', 'Field is a boolean' );
99is( $f6->is_nullable, 1, 'Field can be null' );
100is( $f6->size, 0, 'Size is "0"' );
101is( $f6->default_value, undef, 'Default value is undefined' );
102is( $f6->is_primary_key, 0, 'Field is not PK' );
103
104my $f7 = shift @t1_fields;
105is( $f7->name, 'f_bin', 'Seventh field is "f_bin"' );
106is( $f7->data_type, 'bytea', 'Field is bytea' );
107is( $f7->is_nullable, 1, 'Field can be null' );
108is( $f7->size, 0, 'Size is "0"' );
109is( $f7->default_value, undef, 'Default value is undefined' );
110is( $f7->is_primary_key, 0, 'Field is not PK' );
111
112my $f8 = shift @t1_fields;
113is( $f8->name, 'f_tz', 'Eighth field is "f_tz"' );
114is( $f8->data_type, 'timestamp', 'Field is a timestamp' );
115is( $f8->is_nullable, 1, 'Field can be null' );
116is( $f8->size, 0, 'Size is "0"' );
117is( $f8->default_value, undef, 'Default value is undefined' );
118is( $f8->is_primary_key, 0, 'Field is not PK' );
119
120my $f9 = shift @t1_fields;
121is( $f9->name, 'f_text', 'Ninth field is "f_text"' );
122is( $f9->data_type, 'text', 'Field is text' );
123is( $f9->is_nullable, 1, 'Field can be null' );
60e3ded0 124is( $f9->size, 64000, 'Size is "64,000"' );
07b82495 125is( $f9->default_value, undef, 'Default value is undefined' );
126is( $f9->is_primary_key, 0, 'Field is not PK' );
127
128my $f10 = shift @t1_fields;
129is( $f10->name, 'f_fk1', 'Tenth field is "f_fk1"' );
130is( $f10->data_type, 'integer', 'Field is an integer' );
131is( $f10->is_nullable, 0, 'Field cannot be null' );
9bcfe960 132is( $f10->size, 10, 'Size is "10"' );
07b82495 133is( $f10->default_value, undef, 'Default value is undefined' );
134is( $f10->is_primary_key, 0, 'Field is not PK' );
135is( $f10->is_foreign_key, 1, 'Field is a FK' );
136my $fk_ref1 = $f10->foreign_key_reference;
137isa_ok( $fk_ref1, 'SQL::Translator::Schema::Constraint', 'FK' );
138is( $fk_ref1->reference_table, 't_test2', 'FK is to "t_test2" table' );
139
140my $f11 = shift @t1_fields;
141is( $f11->name, 'f_fk2', 'Eleventh field is "f_fk2"' );
142is( $f11->data_type, 'integer', 'Field is an integer' );
143is( $f11->is_nullable, 1, 'Field can be null' );
9bcfe960 144is( $f11->size, 10, 'Size is "10"' );
07b82495 145is( $f11->default_value, undef, 'Default value is undefined' );
146is( $f11->is_primary_key, 0, 'Field is not PK' );
147is( $f11->is_foreign_key, 1, 'Field is a FK' );
148my $fk_ref2 = $f11->foreign_key_reference;
149isa_ok( $fk_ref2, 'SQL::Translator::Schema::Constraint', 'FK' );
150is( $fk_ref2->reference_table, 't_test2', 'FK is to "t_test2" table' );
151
152my @t1_constraints = $t1->get_constraints;
8b714ee5 153is( scalar @t1_constraints, 7, '7 constraints on t_test1' );
07b82495 154
155my $c1 = $t1_constraints[0];
156is( $c1->type, PRIMARY_KEY, 'First constraint is PK' );
157is( join(',', $c1->fields), 'f_serial', 'Constraint is on field "f_serial"' );
158
8b714ee5 159my $c2 = $t1_constraints[4];
07b82495 160is( $c2->type, FOREIGN_KEY, 'Second constraint is foreign key' );
161is( join(',', $c2->fields), 'f_fk1', 'Constraint is on field "f_fk1"' );
162is( $c2->reference_table, 't_test2', 'Constraint is to table "t_test2"' );
163is( join(',', $c2->reference_fields), 'f_id', 'Constraint is to field "f_id"' );
164
8b714ee5 165my $c3 = $t1_constraints[5];
07b82495 166is( $c3->type, UNIQUE, 'Third constraint is unique' );
167is( join(',', $c3->fields), 'f_varchar', 'Constraint is on field "f_varchar"' );
168
8b714ee5 169my $c4 = $t1_constraints[6];
07b82495 170is( $c4->type, FOREIGN_KEY, 'Fourth constraint is foreign key' );
171is( join(',', $c4->fields), 'f_fk2', 'Constraint is on field "f_fk2"' );
172is( $c4->reference_table, 't_test2', 'Constraint is to table "t_test2"' );
173is( join(',', $c4->reference_fields), 'f_id', 'Constraint is to field "f_id"' );
174is( $c4->on_delete, 'cascade', 'On delete: cascade' );
175is( $c4->on_update, 'no_action', 'On delete: no action' );
176
177my $t2 = shift @tables;
178is( $t2->name, 't_test2', 'Table t_test2 exists' );
179
180my @t2_fields = $t2->get_fields;
668c5039 181is( scalar @t2_fields, 3, '3 fields in t_test2' );
07b82495 182
183my $t2_f1 = shift @t2_fields;
184is( $t2_f1->name, 'f_id', 'First field is "f_id"' );
185is( $t2_f1->data_type, 'integer', 'Field is an integer' );
186is( $t2_f1->is_nullable, 0, 'Field cannot be null' );
9bcfe960 187is( $t2_f1->size, 10, 'Size is "10"' );
07b82495 188is( $t2_f1->default_value, undef, 'Default value is undefined' );
189is( $t2_f1->is_primary_key, 1, 'Field is PK' );
190
191my $t2_f2 = shift @t2_fields;
668c5039 192is( $t2_f2->name, 'f_varchar', 'Second field is "f_varchar"' );
07b82495 193is( $t2_f2->data_type, 'varchar', 'Field is an varchar' );
194is( $t2_f2->is_nullable, 1, 'Field can be null' );
195is( $t2_f2->size, 25, 'Size is "25"' );
196is( $t2_f2->default_value, undef, 'Default value is undefined' );
197is( $t2_f2->is_primary_key, 0, 'Field is not PK' );
668c5039 198
199my $t2_f3 = shift @t2_fields;
200is( $t2_f3->name, 'f_int', 'Third field is "f_int"' );
201is( $t2_f3->data_type, 'integer', 'Field is an integer' );
202is( $t2_f3->is_nullable, 1, 'Field can be null' );
203is( $t2_f3->size, 5, 'Size is "5"' );
204is( $t2_f3->default_value, undef, 'Default value is undefined' );
205is( $t2_f3->is_primary_key, 0, 'Field is not PK' );
206
207my @t2_constraints = $t2->get_constraints;
208is( scalar @t2_constraints, 3, "Three constraints on table" );
209
210my $t2_c1 = shift @t2_constraints;
211is( $t2_c1->type, NOT_NULL, "Constraint is NOT NULL" );
212
213my $t2_c2 = shift @t2_constraints;
214is( $t2_c2->type, PRIMARY_KEY, "Constraint is a PK" );
215
216my $t2_c3 = shift @t2_constraints;
217is( $t2_c3->type, CHECK_C, "Constraint is a 'CHECK'" );