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