Some updates to tests to skip when required dependencies are missing.
[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(76, '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)   NOT NULL,
33         trait_name              VARCHAR2(200)   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
58     CREATE TABLE qtl_trait_synonym
59     (
60         qtl_trait_synonym_id    NUMBER(11)      NOT NULL    
61             CONSTRAINT pk_qtl_trait_synonym PRIMARY KEY,
62         trait_synonym           VARCHAR2(200)   NOT NULL,
63         qtl_trait_id            NUMBER(11)      NOT NULL,
64         UNIQUE( qtl_trait_id, trait_synonym ),
65         FOREIGN KEY ( qtl_trait_id ) REFERENCES qtl_trait
66     );
67 ];
68
69 $| = 1;
70
71 my $data   = parse( $t, $sql );
72 my $schema = $t->schema;
73
74 isa_ok( $schema, 'SQL::Translator::Schema', 'Schema object' );
75 my @tables = $schema->get_tables;
76 is( scalar @tables, 4, 'Found four tables' );
77
78 #
79 # qtl_trait_category
80 #
81 my $t1 = shift @tables;
82 is( $t1->name, 'qtl_trait_category', 'First table is "qtl_trait_category"' );
83 is( $t1->comments, 'hey, hey, hey, hey', 'Comment = "hey, hey, hey, hey"' );
84
85 my @t1_fields = $t1->get_fields;
86 is( scalar @t1_fields, 2, '2 fields in table' );
87
88 my $f1 = shift @t1_fields;
89 is( $f1->name, 'qtl_trait_category_id', 
90     'First field is "qtl_trait_category_id"' );
91 is( $f1->data_type, 'number', 'Field is a number' );
92 is( $f1->size, 11, 'Size is "11"' );
93 is( $f1->is_nullable, 0, 'Field cannot be null' );
94 is( $f1->default_value, undef, 'Default value is undefined' );
95 is( $f1->is_primary_key, 1, 'Field is PK' );
96 is( join(',', $f1->comments), 'the primary key!', 'Comment = "the primary key!"' );
97
98 my $f2 = shift @t1_fields;
99 is( $f2->name, 'trait_category', 'Second field is "trait_category"' );
100 is( $f2->data_type, 'varchar2', 'Field is a varchar2' );
101 is( $f2->size, 100, 'Size is "100"' );
102 is( $f2->is_nullable, 0, 'Field cannot be null' );
103 is( $f2->default_value, undef, 'Default value is undefined' );
104 is( $f2->is_primary_key, 0, 'Field is not PK' );
105
106 my @t1_indices = $t1->get_indices;
107 is( scalar @t1_indices, 0, '0 indices on table' );
108
109 my @t1_constraints = $t1->get_constraints;
110 is( scalar @t1_constraints, 2, '2 constraints on table' );
111
112 my $c1 = $t1_constraints[0];
113 is( $c1->name, 'pk_qtl_trait_category', 
114     'Constraint name is "pk_qtl_trait_category"' );
115 is( $c1->type, PRIMARY_KEY, 'First constraint is PK' );
116 is( join(',', $c1->fields), 'qtl_trait_category_id', 
117     'Constraint is on field "qtl_trait_category_id"' );
118
119 my $c2 = $t1_constraints[1];
120 is( $c2->type, UNIQUE, 'Second constraint is unique' );
121 is( join(',', $c2->fields), 'trait_category', 
122     'Constraint is on field "trait_category"' );
123
124 #
125 # qtl_trait
126 #
127 my $t2 = shift @tables;
128 is( $t2->name, 'qtl_trait', 'Table "qtl_trait" exists' );
129 is( $t2->comments, 'foo bar comment', 'Comment "foo bar" exists' );
130
131 my @t2_fields = $t2->get_fields;
132 is( scalar @t2_fields, 4, '4 fields in table' );
133
134 my $t2_f1 = shift @t2_fields;
135 is( $t2_f1->name, 'qtl_trait_id', 'First field is "qtl_trait_id"' );
136 is( $t2_f1->data_type, 'number', 'Field is a number' );
137 is( $t2_f1->size, 11, 'Size is "11"' );
138 is( $t2_f1->is_nullable, 0, 'Field cannot be null' );
139 is( $t2_f1->default_value, undef, 'Default value is undefined' );
140 is( $t2_f1->is_primary_key, 1, 'Field is PK' );
141
142 my $t2_f2 = shift @t2_fields;
143 is( $t2_f2->name, 'trait_symbol', 'Second field is "trait_symbol"' );
144 is( $t2_f2->data_type, 'varchar2', 'Field is a varchar2' );
145 is( $t2_f2->size, 100, 'Size is "100"' );
146 is( $t2_f2->is_nullable, 0, 'Field cannot be null' );
147 is( $t2_f2->is_foreign_key, 0, 'Field is not a FK' );
148
149 my $t2_f3 = shift @t2_fields;
150 is( $t2_f3->name, 'trait_name', 'Third field is "trait_name"' );
151 is( $t2_f3->data_type, 'varchar2', 'Field is a varchar2' );
152 is( $t2_f3->size, 200, 'Size is "200"' );
153 is( $t2_f3->is_nullable, 0, 'Field cannot be null' );
154 is( $t2_f3->is_foreign_key, 0, 'Field is not a FK' );
155
156 my $t2_f4 = shift @t2_fields;
157 is( $t2_f4->name, 'qtl_trait_category_id', 
158     'Fourth field is "qtl_trait_category_id"' );
159 is( $t2_f4->data_type, 'number', 'Field is a number' );
160 is( $t2_f4->size, 11, 'Size is "11"' );
161 is( $t2_f4->is_nullable, 0, 'Field cannot be null' );
162 is( $t2_f4->is_foreign_key, 1, 'Field is a FK' );
163 my $f4_fk = $t2_f4->foreign_key_reference;
164 isa_ok( $f4_fk, 'SQL::Translator::Schema::Constraint', 'FK' );
165 is( $f4_fk->reference_table, 'qtl_trait_category', 
166     'FK references table "qtl_trait_category"' );
167 is( join(',', $f4_fk->reference_fields), 'qtl_trait_category_id', 
168     'FK references field "qtl_trait_category_id"' );
169
170 my @t2_constraints = $t2->get_constraints;
171 is( scalar @t2_constraints, 4, '4 constraints on table' );
172
173 my $t2_c1 = shift @t2_constraints;
174 is( $t2_c1->type, PRIMARY_KEY, 'First constraint is PK' );
175 is( $t2_c1->name, 'pk_qtl_trait', 'Name is "pk_qtl_trait"' );
176 is( join(',', $t2_c1->fields), 'qtl_trait_id', 'Fields = "qtl_trait_id"' );
177
178 my $t2_c2 = shift @t2_constraints;
179 is( $t2_c2->type, UNIQUE, 'Second constraint is unique' );
180 is( $t2_c2->name, '', 'No name' );
181 is( join(',', $t2_c2->fields), 'trait_symbol', 'Fields = "trait_symbol"' );
182
183 my $t2_c3 = shift @t2_constraints;
184 is( $t2_c3->type, UNIQUE, 'Third constraint is unique' );
185 is( $t2_c3->name, '', 'No name' );
186 is( join(',', $t2_c3->fields), 'trait_name', 'Fields = "trait_name"' );
187
188 my $t2_c4 = shift @t2_constraints;
189 is( $t2_c4->type, FOREIGN_KEY, 'Fourth constraint is FK' );
190 is( $t2_c4->name, '', 'No name' );
191 is( join(',', $t2_c4->fields), 'qtl_trait_category_id', 
192     'Fields = "qtl_trait_category_id"' );
193 is( $t2_c4->reference_table, 'qtl_trait_category', 
194     'Reference table = "qtl_trait_category"' );
195 is( join(',', $t2_c4->reference_fields), 'qtl_trait_category_id', 
196     'Reference fields = "qtl_trait_category_id"' );
197
198
199 #
200 # qtl
201 #
202 my $t3 = shift @tables;
203 is( $t3->name, 'qtl', 'Table "qtl" exists' );
204
205 my @t3_fields = $t3->get_fields;
206 is( scalar @t3_fields, 8, '8 fields in table' );
207
208 my @t3_constraints = $t3->get_constraints;
209 is( scalar @t3_constraints, 3, '3 constraints on table' );
210
211 is( $t3->comments, 'qtl table comment', 'Comment "qtl table comment" exists' );
212
213 my $t3_f1     = shift @t3_fields;
214 is( $t3_f1->comments, 'qtl_id comment', 'Comment "qtl_id comment" exists' );
215
216 my $t3_f2     = shift @t3_fields;
217 is( $t3_f2->comments, 'accession comment', 
218     'Comment "accession comment" exists' );
219
220 #
221 # qtl_trait_synonym
222 #
223 my $t4 = shift @tables;
224 is( $t4->name, 'qtl_trait_synonym', 'Table "qtl_trait_synonym" exists' );
225
226 my @t4_fields = $t4->get_fields;
227 is( scalar @t4_fields, 3, '3 fields in table' );
228
229 my @t4_constraints = $t4->get_constraints;
230 is( scalar @t4_constraints, 3, '3 constraints on table' );