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