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