Added tests for check constraint.
[dbsrgits/SQL-Translator.git] / t / 15oracle-parser.t
1 #!/usr/bin/perl
2 # vim: set ft=perl:
3
4 use strict;
5 use Test::More;
6 use SQL::Translator;
7 use SQL::Translator::Schema::Constants;
8 use Test::SQL::Translator qw(maybe_plan);
9
10 maybe_plan(99, 'SQL::Translator::Parser::Oracle');
11 SQL::Translator::Parser::Oracle->import('parse');
12
13 my $t   = SQL::Translator->new( trace => 0 );
14 my $sql = q[
15     CREATE TABLE qtl_trait_category
16     (
17         qtl_trait_category_id       NUMBER(11)      NOT NULL    
18             CONSTRAINT pk_qtl_trait_category PRIMARY KEY,
19         trait_category              VARCHAR2(100)   NOT NULL,
20         CONSTRAINT AVCON_4287_PARAM_000 CHECK 
21             (trait_category IN ('S', 'A', 'E')) ENABLE,
22         UNIQUE ( trait_category )
23     );
24     COMMENT ON TABLE qtl_trait_category IS 
25     'hey, hey, hey, hey';
26     comment on column qtl_trait_category.qtl_trait_category_id 
27         is 'the primary key!';
28
29     -- foo bar comment
30     CREATE TABLE qtl_trait
31     (
32         qtl_trait_id            NUMBER(11)      NOT NULL    
33             CONSTRAINT pk_qtl_trait PRIMARY KEY,
34         trait_symbol            VARCHAR2(100 BYTE)   NOT NULL,
35         trait_name              VARCHAR2(200 CHAR)   NOT NULL,
36         qtl_trait_category_id   NUMBER(11)      NOT NULL,
37         UNIQUE ( trait_symbol ),
38         UNIQUE ( trait_name ),
39         FOREIGN KEY ( qtl_trait_category_id ) REFERENCES qtl_trait_category
40     );
41
42     /* qtl table comment */
43     CREATE TABLE qtl
44     (
45         /* qtl_id comment */
46         qtl_id              NUMBER(11)      NOT NULL    
47             CONSTRAINT pk_qtl PRIMARY KEY,
48         qtl_accession_id    VARCHAR2(20)    NOT NULL /* accession comment */,
49         published_symbol    VARCHAR2(100),
50         qtl_trait_id        NUMBER(11)      NOT NULL,
51         linkage_group       VARCHAR2(32)    NOT NULL,
52         start_position      NUMBER(11,2)    NOT NULL,
53         stop_position       NUMBER(11,2)    NOT NULL,
54         comments            long,
55         FOREIGN KEY ( qtl_trait_id ) REFERENCES qtl_trait
56     );
57
58     CREATE UNIQUE INDEX qtl_accession ON qtl ( qtl_accession_id );
59     CREATE UNIQUE INDEX qtl_accession_upper ON qtl ( UPPER(qtl_accession_id) );
60     CREATE INDEX qtl_index ON qtl ( qtl_accession_id );
61
62     CREATE TABLE qtl_trait_synonym
63     (
64         qtl_trait_synonym_id    NUMBER(11)      NOT NULL    
65             CONSTRAINT pk_qtl_trait_synonym PRIMARY KEY,
66         trait_synonym           VARCHAR2(200)   NOT NULL,
67         qtl_trait_id            NUMBER(11)      NOT NULL,
68         UNIQUE( qtl_trait_id, trait_synonym ),
69         FOREIGN KEY ( qtl_trait_id ) REFERENCES qtl_trait ON DELETE SET NULL
70     );
71
72 -- View and procedure testing
73         CREATE OR REPLACE PROCEDURE CMDOMAIN_LATEST.P_24_HOUR_EVENT_SUMMARY
74         IS
75                     ldate                   varchar2(10);
76                     user_added              INT;
77                     user_deleted            INT;
78                     workingsets_created     INT;
79                     change_executed         INT;
80                     change_detected         INT;
81                     reports_run             INT;
82                     backup_complete         INT;
83                     backup_failed           INT;
84                     devices_in_inventory    INT;
85         
86         
87         BEGIN
88         
89                    select CAST(TO_CHAR(sysdate,'MM/DD/YYYY') AS varchar2(10))  INTO ldate  from  dual;
90         END;
91 /
92         
93         CREATE OR REPLACE FORCE VIEW CMDOMAIN_MIG.VS_ASSET (ASSET_ID, FQ_NAME, FOLDER_NAME, ASSET_NAME, ANNOTATION, ASSET_TYPE, FOREIGN_ASSET_ID, FOREIGN_ASSET_ID2, DATE_CREATED, DATE_MODIFIED, CONTAINER_ID, CREATOR_ID, MODIFIER_ID, USER_ACCESS) AS
94           SELECT
95             a.asset_id, a.fq_name,
96             ap_extract_folder(a.fq_name) AS folder_name,
97             ap_extract_asset(a.fq_name)  AS asset_name,
98             a.annotation,
99             a.asset_type,
100             a.foreign_asset_id,
101             a.foreign_asset_id2,
102             a.dateCreated AS date_created,
103             a.dateModified AS date_modified,
104             a.container_id,
105             a.creator_id,
106             a.modifier_id,
107             m.user_id AS user_access
108         from asset a
109         JOIN M_ACCESS_CONTROL m on a.acl_id = m.acl_id;
110
111 ];
112
113 $| = 1;
114
115 my $data   = parse( $t, $sql );
116 my $schema = $t->schema;
117
118 isa_ok( $schema, 'SQL::Translator::Schema', 'Schema object' );
119 my @tables = $schema->get_tables;
120 is( scalar @tables, 4, 'Found four tables' );
121
122 #
123 # qtl_trait_category
124 #
125 my $t1 = shift @tables;
126 is( $t1->name, 'qtl_trait_category', 'First table is "qtl_trait_category"' );
127 is( $t1->comments, 'hey, hey, hey, hey', 'Comment = "hey, hey, hey, hey"' );
128
129 my @t1_fields = $t1->get_fields;
130 is( scalar @t1_fields, 2, '2 fields in table' );
131
132 my $f1 = shift @t1_fields;
133 is( $f1->name, 'qtl_trait_category_id', 
134     'First field is "qtl_trait_category_id"' );
135 is( $f1->data_type, 'number', 'Field is a number' );
136 is( $f1->size, 11, 'Size is "11"' );
137 is( $f1->is_nullable, 0, 'Field cannot be null' );
138 is( $f1->default_value, undef, 'Default value is undefined' );
139 is( $f1->is_primary_key, 1, 'Field is PK' );
140 is( join(',', $f1->comments), 'the primary key!', 'Comment = "the primary key!"' );
141
142 my $f2 = shift @t1_fields;
143 is( $f2->name, 'trait_category', 'Second field is "trait_category"' );
144 is( $f2->data_type, 'varchar2', 'Field is a varchar2' );
145 is( $f2->size, 100, 'Size is "100"' );
146 is( $f2->is_nullable, 0, 'Field cannot be null' );
147 is( $f2->default_value, undef, 'Default value is undefined' );
148 is( $f2->is_primary_key, 0, 'Field is not PK' );
149
150 my @t1_indices = $t1->get_indices;
151 is( scalar @t1_indices, 0, '0 indices on table' );
152
153 my @t1_constraints = $t1->get_constraints;
154 #use Data::Dumper;
155 #print STDERR Dumper(\@t1_constraints), "\n";
156 is( scalar @t1_constraints, 3, '3 constraints on table' );
157
158 my $c1 = $t1_constraints[0];
159 is( $c1->name, 'pk_qtl_trait_category', 
160     'Constraint name is "pk_qtl_trait_category"' );
161 is( $c1->type, PRIMARY_KEY, 'First constraint is PK' );
162 is( join(',', $c1->fields), 'qtl_trait_category_id', 
163     'Constraint is on field "qtl_trait_category_id"' );
164
165 my $c2 = $t1_constraints[1];
166 is( $c2->type, CHECK_C, 'Second constraint is a check' );
167 is( $c2->expression, 
168     "( trait_category IN ('S', 'A', 'E') ) ENABLE",
169     'Constraint is on field "trait_category"' );
170
171 my $c3 = $t1_constraints[2];
172 is( $c3->type, UNIQUE, 'Third constraint is unique' );
173 is( join(',', $c3->fields), 'trait_category', 
174     'Constraint is on field "trait_category"' );
175
176 #
177 # qtl_trait
178 #
179 my $t2 = shift @tables;
180 is( $t2->name, 'qtl_trait', 'Table "qtl_trait" exists' );
181 is( $t2->comments, 'foo bar comment', 'Comment "foo bar" exists' );
182
183 my @t2_fields = $t2->get_fields;
184 is( scalar @t2_fields, 4, '4 fields in table' );
185
186 my $t2_f1 = shift @t2_fields;
187 is( $t2_f1->name, 'qtl_trait_id', 'First field is "qtl_trait_id"' );
188 is( $t2_f1->data_type, 'number', 'Field is a number' );
189 is( $t2_f1->size, 11, 'Size is "11"' );
190 is( $t2_f1->is_nullable, 0, 'Field cannot be null' );
191 is( $t2_f1->default_value, undef, 'Default value is undefined' );
192 is( $t2_f1->is_primary_key, 1, 'Field is PK' );
193
194 my $t2_f2 = shift @t2_fields;
195 is( $t2_f2->name, 'trait_symbol', 'Second field is "trait_symbol"' );
196 is( $t2_f2->data_type, 'varchar2', 'Field is a varchar2' );
197 is( $t2_f2->size, 100, 'Size is "100"' );
198 is( $t2_f2->is_nullable, 0, 'Field cannot be null' );
199 is( $t2_f2->is_foreign_key, 0, 'Field is not a FK' );
200
201 my $t2_f3 = shift @t2_fields;
202 is( $t2_f3->name, 'trait_name', 'Third field is "trait_name"' );
203 is( $t2_f3->data_type, 'varchar2', 'Field is a varchar2' );
204 is( $t2_f3->size, 200, 'Size is "200"' );
205 is( $t2_f3->is_nullable, 0, 'Field cannot be null' );
206 is( $t2_f3->is_foreign_key, 0, 'Field is not a FK' );
207
208 my $t2_f4 = shift @t2_fields;
209 is( $t2_f4->name, 'qtl_trait_category_id', 
210     'Fourth field is "qtl_trait_category_id"' );
211 is( $t2_f4->data_type, 'number', 'Field is a number' );
212 is( $t2_f4->size, 11, 'Size is "11"' );
213 is( $t2_f4->is_nullable, 0, 'Field cannot be null' );
214 is( $t2_f4->is_foreign_key, 1, 'Field is a FK' );
215 my $f4_fk = $t2_f4->foreign_key_reference;
216 isa_ok( $f4_fk, 'SQL::Translator::Schema::Constraint', 'FK' );
217 is( $f4_fk->reference_table, 'qtl_trait_category', 
218     'FK references table "qtl_trait_category"' );
219 is( join(',', $f4_fk->reference_fields), 'qtl_trait_category_id', 
220     'FK references field "qtl_trait_category_id"' );
221
222 my @t2_constraints = $t2->get_constraints;
223 is( scalar @t2_constraints, 4, '4 constraints on table' );
224
225 my $t2_c1 = shift @t2_constraints;
226 is( $t2_c1->type, PRIMARY_KEY, 'First constraint is PK' );
227 is( $t2_c1->name, 'pk_qtl_trait', 'Name is "pk_qtl_trait"' );
228 is( join(',', $t2_c1->fields), 'qtl_trait_id', 'Fields = "qtl_trait_id"' );
229
230 my $t2_c2 = shift @t2_constraints;
231 is( $t2_c2->type, UNIQUE, 'Second constraint is unique' );
232 is( $t2_c2->name, '', 'No name' );
233 is( join(',', $t2_c2->fields), 'trait_symbol', 'Fields = "trait_symbol"' );
234
235 my $t2_c3 = shift @t2_constraints;
236 is( $t2_c3->type, UNIQUE, 'Third constraint is unique' );
237 is( $t2_c3->name, '', 'No name' );
238 is( join(',', $t2_c3->fields), 'trait_name', 'Fields = "trait_name"' );
239
240 my $t2_c4 = shift @t2_constraints;
241 is( $t2_c4->type, FOREIGN_KEY, 'Fourth constraint is FK' );
242 is( $t2_c4->name, '', 'No name' );
243 is( join(',', $t2_c4->fields), 'qtl_trait_category_id', 
244     'Fields = "qtl_trait_category_id"' );
245 is( $t2_c4->reference_table, 'qtl_trait_category', 
246     'Reference table = "qtl_trait_category"' );
247 is( join(',', $t2_c4->reference_fields), 'qtl_trait_category_id', 
248     'Reference fields = "qtl_trait_category_id"' );
249
250
251 #
252 # qtl
253 #
254 my $t3 = shift @tables;
255 is( $t3->name, 'qtl', 'Table "qtl" exists' );
256
257 my @t3_fields = $t3->get_fields;
258 is( scalar @t3_fields, 8, '8 fields in table' );
259
260 my @t3_constraints = $t3->get_constraints;
261 is( scalar @t3_constraints, 4, '4 constraints on table' );
262 my $t3_c4 = $t3_constraints[3];
263 is( $t3_c4->type, UNIQUE, 'Fourth constraint is unique' );
264 is( $t3_c4->name, 'qtl_accession_upper', 'Name = "qtl_accession_upper"' );
265 is( join(',', $t3_c4->fields), 'UPPER(qtl_accession_id)', 'Fields = "UPPER(qtl_accession_id)"' );
266
267 is( $t3->comments, 'qtl table comment', 'Comment "qtl table comment" exists' );
268
269 my $t3_f1     = shift @t3_fields;
270 is( $t3_f1->comments, 'qtl_id comment', 'Comment "qtl_id comment" exists' );
271
272 my $t3_f2     = shift @t3_fields;
273 is( $t3_f2->comments, 'accession comment', 
274     'Comment "accession comment" exists' );
275
276 my @t3_indices = $t3->get_indices;
277 is( scalar @t3_indices, 1, '1 index on table' );
278
279 my $t3_i1 = shift @t3_indices;
280 is( $t3_i1->type, 'NORMAL', 'First index is normal' );
281 is( $t3_i1->name, 'qtl_index', 'Name is "qtl_index"' );
282 is( join(',', $t3_i1->fields), 'qtl_accession_id', 'Fields = "qtl_accession_id"' );
283
284 #
285 # qtl_trait_synonym
286 #
287 my $t4 = shift @tables;
288 is( $t4->name, 'qtl_trait_synonym', 'Table "qtl_trait_synonym" exists' );
289
290 my @t4_fields = $t4->get_fields;
291 is( scalar @t4_fields, 3, '3 fields in table' );
292
293 my @t4_constraints = $t4->get_constraints;
294 is( scalar @t4_constraints, 3, '3 constraints on table' );
295 my $t4_c3 = $t4_constraints[2];
296 is( $t4_c3->type, FOREIGN_KEY, 'Third constraint is FK' );
297 is( $t4_c3->name, '', 'No name' );
298 is( join(',', $t4_c3->fields), 'qtl_trait_id', 
299     'Fields = "qtl_trait_id"' );
300 is( $t4_c3->reference_table, 'qtl_trait', 
301     'Reference table = "qtl_trait"' );
302 is( join(',', $t4_c3->reference_fields), 'qtl_trait_id', 
303     'Reference fields = "qtl_trait_id"' );
304 is( $t4_c3->on_delete, 'SET NULL', 
305     'on_delete = "SET NULL"' );
306
307 my @views = $schema->get_views;
308 is( scalar @views, 1, 'Right number of views (1)' );
309 my $view1 = shift @views;
310 is( $view1->name, 'VS_ASSET', 'Found "VS_ASSET" view' );
311 like($view1->sql, qr/VS_ASSET/, "Detected view VS_ASSET");
312 unlike($view1->sql, qr/CMDOMAIN_MIG/, "Did not detect CMDOMAIN_MIG");
313     
314 my @procs = $schema->get_procedures;
315 is( scalar @procs, 1, 'Right number of procedures (1)' );
316 my $proc1 = shift @procs;
317 is( $proc1->name, 'P_24_HOUR_EVENT_SUMMARY', 'Found "P_24_HOUR_EVENT_SUMMARY" procedure' );
318 like($proc1->sql, qr/P_24_HOUR_EVENT_SUMMARY/, "Detected procedure P_24_HOUR_EVENT_SUMMARY");
319 unlike($proc1->sql, qr/CMDOMAIN_MIG/, "Did not detect CMDOMAIN_MIG");