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