Commit | Line | Data |
8c4efd11 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More; |
7 | use Test::Exception; |
8 | use Test::SQL::Translator qw(maybe_plan); |
9 | |
10 | use Data::Dumper; |
11 | use FindBin qw/$Bin/; |
12 | |
13 | # Testing 1,2,3,4... |
14 | #============================================================================= |
15 | |
16 | BEGIN { |
bc8e2aa1 |
17 | maybe_plan(13, |
8c4efd11 |
18 | 'SQL::Translator::Producer::PostgreSQL', |
19 | 'Test::Differences', |
20 | ) |
21 | } |
22 | use Test::Differences; |
23 | use SQL::Translator; |
24 | |
bc8e2aa1 |
25 | my $PRODUCER = \&SQL::Translator::Producer::PostgreSQL::create_field; |
8c4efd11 |
26 | |
27 | my $table = SQL::Translator::Schema::Table->new( name => 'mytable'); |
28 | |
29 | my $field1 = SQL::Translator::Schema::Field->new( name => 'myfield', |
30 | table => $table, |
31 | data_type => 'VARCHAR', |
32 | size => 10, |
33 | default_value => undef, |
34 | is_auto_increment => 0, |
35 | is_nullable => 1, |
36 | is_foreign_key => 0, |
37 | is_unique => 0 ); |
38 | |
39 | my $field1_sql = SQL::Translator::Producer::PostgreSQL::create_field($field1); |
40 | |
41 | is($field1_sql, 'myfield character varying(10)', 'Create field works'); |
42 | |
43 | my $field2 = SQL::Translator::Schema::Field->new( name => 'myfield', |
44 | table => $table, |
45 | data_type => 'VARCHAR', |
46 | size => 25, |
47 | default_value => undef, |
48 | is_auto_increment => 0, |
49 | is_nullable => 0, |
50 | is_foreign_key => 0, |
51 | is_unique => 0 ); |
52 | |
53 | my $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field1, |
54 | $field2); |
3406fd5b |
55 | is($alter_field, qq[ALTER TABLE mytable ALTER COLUMN myfield SET NOT NULL |
56 | ALTER TABLE mytable ALTER COLUMN myfield TYPE character varying(25)], |
8c4efd11 |
57 | 'Alter field works'); |
58 | |
59 | $field1->name('field3'); |
60 | my $add_field = SQL::Translator::Producer::PostgreSQL::add_field($field1); |
61 | |
3406fd5b |
62 | is($add_field, 'ALTER TABLE mytable ADD COLUMN field3 character varying(10)', 'Add field works'); |
8c4efd11 |
63 | |
64 | my $drop_field = SQL::Translator::Producer::PostgreSQL::drop_field($field2); |
3406fd5b |
65 | is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works'); |
8c4efd11 |
66 | |
e56dabb7 |
67 | my $field3 = SQL::Translator::Schema::Field->new( name => 'time_field', |
68 | table => $table, |
69 | data_type => 'TIME', |
70 | default_value => undef, |
71 | is_auto_increment => 0, |
72 | is_nullable => 0, |
73 | is_foreign_key => 0, |
74 | is_unique => 0 ); |
75 | |
76 | my $field3_sql = SQL::Translator::Producer::PostgreSQL::create_field($field3); |
77 | |
78 | is($field3_sql, 'time_field time NOT NULL', 'Create time field works'); |
79 | |
80 | my $field4 = SQL::Translator::Schema::Field->new( name => 'bytea_field', |
81 | table => $table, |
82 | data_type => 'bytea', |
83 | size => '16777215', |
84 | default_value => undef, |
85 | is_auto_increment => 0, |
86 | is_nullable => 0, |
87 | is_foreign_key => 0, |
88 | is_unique => 0 ); |
89 | |
90 | my $field4_sql = SQL::Translator::Producer::PostgreSQL::create_field($field4); |
8c4efd11 |
91 | |
e56dabb7 |
92 | is($field4_sql, 'bytea_field bytea NOT NULL', 'Create bytea field works'); |
5342f5c1 |
93 | |
94 | my $field5 = SQL::Translator::Schema::Field->new( name => 'enum_field', |
95 | table => $table, |
96 | data_type => 'enum', |
97 | extra => { list => [ 'Foo', 'Bar' ] }, |
98 | is_auto_increment => 0, |
99 | is_nullable => 0, |
100 | is_foreign_key => 0, |
101 | is_unique => 0 ); |
102 | |
103 | my $field5_sql = SQL::Translator::Producer::PostgreSQL::create_field($field5,{ postgres_version => 8.3 }); |
104 | |
105 | is($field5_sql, 'enum_field mytable_enum_field_type NOT NULL', 'Create real enum field works'); |
106 | |
bc8e2aa1 |
107 | { |
108 | # let's test default values! -- rjbs, 2008-09-30 |
109 | my %field = ( |
110 | table => $table, |
111 | data_type => 'VARCHAR', |
112 | size => 10, |
113 | is_auto_increment => 0, |
114 | is_nullable => 1, |
115 | is_foreign_key => 0, |
116 | is_unique => 0, |
117 | ); |
118 | |
119 | { |
120 | my $simple_default = SQL::Translator::Schema::Field->new( |
121 | %field, |
122 | name => 'str_default', |
123 | default_value => 'foo', |
124 | ); |
125 | |
126 | is( |
127 | $PRODUCER->($simple_default), |
128 | q{str_default character varying(10) DEFAULT 'foo'}, |
129 | 'default str', |
130 | ); |
131 | } |
132 | |
133 | { |
134 | my $null_default = SQL::Translator::Schema::Field->new( |
135 | %field, |
136 | name => 'null_default', |
137 | default_value => \'NULL', |
138 | ); |
139 | |
140 | is( |
141 | $PRODUCER->($null_default), |
142 | q{null_default character varying(10) DEFAULT NULL}, |
143 | 'default null', |
144 | ); |
145 | } |
146 | |
147 | { |
148 | my $null_default = SQL::Translator::Schema::Field->new( |
149 | %field, |
150 | name => 'null_default_2', |
151 | default_value => 'NULL', # XXX: this should go away |
152 | ); |
153 | |
154 | is( |
155 | $PRODUCER->($null_default), |
156 | q{null_default_2 character varying(10) DEFAULT NULL}, |
157 | 'default null from special cased string', |
158 | ); |
159 | } |
160 | |
161 | { |
162 | my $func_default = SQL::Translator::Schema::Field->new( |
163 | %field, |
164 | name => 'func_default', |
165 | default_value => \'func(funky)', |
166 | ); |
167 | |
168 | is( |
169 | $PRODUCER->($func_default), |
170 | q{func_default character varying(10) DEFAULT func(funky)}, |
171 | 'unquoted default from scalar ref', |
172 | ); |
173 | } |
174 | } |
175 | |
176 | |
296c2701 |
177 | my $view1 = SQL::Translator::Schema::View->new( |
178 | name => 'view_foo', |
179 | fields => [qw/id name/], |
180 | sql => 'SELECT id, name FROM thing', |
181 | ); |
182 | my $create_opts = { add_replace_view => 1, no_comments => 1 }; |
183 | my $view1_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view1, $create_opts); |
184 | |
a25ac5d2 |
185 | my $view_sql_replace = "CREATE VIEW view_foo ( id, name ) AS ( |
296c2701 |
186 | SELECT id, name FROM thing |
3406fd5b |
187 | )"; |
296c2701 |
188 | is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL'); |
189 | |
190 | my $view2 = SQL::Translator::Schema::View->new( |
191 | name => 'view_foo2', |
192 | sql => 'SELECT id, name FROM thing', |
193 | extra => { |
194 | 'temporary' => '1', |
195 | 'check_option' => 'cascaded', |
196 | }, |
197 | ); |
198 | my $create2_opts = { add_replace_view => 1, no_comments => 1 }; |
199 | my $view2_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view2, $create2_opts); |
200 | |
a25ac5d2 |
201 | my $view2_sql_replace = "CREATE TEMPORARY VIEW view_foo2 AS ( |
296c2701 |
202 | SELECT id, name FROM thing |
3406fd5b |
203 | ) WITH CASCADED CHECK OPTION"; |
296c2701 |
204 | is($view2_sql1, $view2_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL 2'); |