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