Added mysql-parser-version command-line option
[dbsrgits/SQL-Translator.git] / t / 15oracle-parser.t
CommitLineData
75484db3 1#!/usr/bin/perl
fbc0552f 2# vim: set ft=perl:
75484db3 3
4use strict;
fbc0552f 5use Test::More;
75484db3 6use SQL::Translator;
7use SQL::Translator::Schema::Constants;
2d691ec1 8use Test::SQL::Translator qw(maybe_plan);
fbc0552f 9
ae8a4ce3 10maybe_plan(89, 'SQL::Translator::Parser::Oracle');
2d691ec1 11SQL::Translator::Parser::Oracle->import('parse');
75484db3 12
13my $t = SQL::Translator->new( trace => 0 );
14my $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 );
d2f0a9f6 22 COMMENT ON TABLE qtl_trait_category IS
23 'hey, hey, hey, hey';
75484db3 24 comment on column qtl_trait_category.qtl_trait_category_id
25 is 'the primary key!';
26
b03b4951 27 -- foo bar comment
75484db3 28 CREATE TABLE qtl_trait
29 (
30 qtl_trait_id NUMBER(11) NOT NULL
31 CONSTRAINT pk_qtl_trait PRIMARY KEY,
7f6504d3 32 trait_symbol VARCHAR2(100 BYTE) NOT NULL,
33 trait_name VARCHAR2(200 CHAR) NOT NULL,
75484db3 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
2eb042e6 40 /* qtl table comment */
75484db3 41 CREATE TABLE qtl
42 (
2eb042e6 43 /* qtl_id comment */
75484db3 44 qtl_id NUMBER(11) NOT NULL
45 CONSTRAINT pk_qtl PRIMARY KEY,
2eb042e6 46 qtl_accession_id VARCHAR2(20) NOT NULL /* accession comment */,
75484db3 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,
75484db3 53 FOREIGN KEY ( qtl_trait_id ) REFERENCES qtl_trait
54 );
55
321cd178 56 CREATE UNIQUE INDEX qtl_accession ON qtl ( qtl_accession_id );
7f6504d3 57 CREATE UNIQUE INDEX qtl_accession_upper ON qtl ( UPPER(qtl_accession_id) );
ae8a4ce3 58 CREATE INDEX qtl_index ON qtl ( qtl_accession_id );
321cd178 59
75484db3 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 ),
188a97b5 67 FOREIGN KEY ( qtl_trait_id ) REFERENCES qtl_trait ON DELETE SET NULL
75484db3 68 );
69];
70
71$| = 1;
72
73my $data = parse( $t, $sql );
74my $schema = $t->schema;
75
75484db3 76isa_ok( $schema, 'SQL::Translator::Schema', 'Schema object' );
77my @tables = $schema->get_tables;
78is( scalar @tables, 4, 'Found four tables' );
79
80#
81# qtl_trait_category
82#
83my $t1 = shift @tables;
84is( $t1->name, 'qtl_trait_category', 'First table is "qtl_trait_category"' );
85is( $t1->comments, 'hey, hey, hey, hey', 'Comment = "hey, hey, hey, hey"' );
86
87my @t1_fields = $t1->get_fields;
88is( scalar @t1_fields, 2, '2 fields in table' );
89
90my $f1 = shift @t1_fields;
91is( $f1->name, 'qtl_trait_category_id',
92 'First field is "qtl_trait_category_id"' );
93is( $f1->data_type, 'number', 'Field is a number' );
94is( $f1->size, 11, 'Size is "11"' );
95is( $f1->is_nullable, 0, 'Field cannot be null' );
96is( $f1->default_value, undef, 'Default value is undefined' );
97is( $f1->is_primary_key, 1, 'Field is PK' );
a378a96f 98is( join(',', $f1->comments), 'the primary key!', 'Comment = "the primary key!"' );
75484db3 99
100my $f2 = shift @t1_fields;
101is( $f2->name, 'trait_category', 'Second field is "trait_category"' );
102is( $f2->data_type, 'varchar2', 'Field is a varchar2' );
103is( $f2->size, 100, 'Size is "100"' );
104is( $f2->is_nullable, 0, 'Field cannot be null' );
105is( $f2->default_value, undef, 'Default value is undefined' );
106is( $f2->is_primary_key, 0, 'Field is not PK' );
107
108my @t1_indices = $t1->get_indices;
109is( scalar @t1_indices, 0, '0 indices on table' );
110
111my @t1_constraints = $t1->get_constraints;
112is( scalar @t1_constraints, 2, '2 constraints on table' );
113
114my $c1 = $t1_constraints[0];
115is( $c1->name, 'pk_qtl_trait_category',
116 'Constraint name is "pk_qtl_trait_category"' );
117is( $c1->type, PRIMARY_KEY, 'First constraint is PK' );
118is( join(',', $c1->fields), 'qtl_trait_category_id',
119 'Constraint is on field "qtl_trait_category_id"' );
120
121my $c2 = $t1_constraints[1];
122is( $c2->type, UNIQUE, 'Second constraint is unique' );
123is( join(',', $c2->fields), 'trait_category',
124 'Constraint is on field "trait_category"' );
125
126#
127# qtl_trait
128#
129my $t2 = shift @tables;
130is( $t2->name, 'qtl_trait', 'Table "qtl_trait" exists' );
b03b4951 131is( $t2->comments, 'foo bar comment', 'Comment "foo bar" exists' );
75484db3 132
133my @t2_fields = $t2->get_fields;
134is( scalar @t2_fields, 4, '4 fields in table' );
135
136my $t2_f1 = shift @t2_fields;
137is( $t2_f1->name, 'qtl_trait_id', 'First field is "qtl_trait_id"' );
138is( $t2_f1->data_type, 'number', 'Field is a number' );
139is( $t2_f1->size, 11, 'Size is "11"' );
140is( $t2_f1->is_nullable, 0, 'Field cannot be null' );
141is( $t2_f1->default_value, undef, 'Default value is undefined' );
142is( $t2_f1->is_primary_key, 1, 'Field is PK' );
143
144my $t2_f2 = shift @t2_fields;
145is( $t2_f2->name, 'trait_symbol', 'Second field is "trait_symbol"' );
146is( $t2_f2->data_type, 'varchar2', 'Field is a varchar2' );
147is( $t2_f2->size, 100, 'Size is "100"' );
148is( $t2_f2->is_nullable, 0, 'Field cannot be null' );
149is( $t2_f2->is_foreign_key, 0, 'Field is not a FK' );
150
151my $t2_f3 = shift @t2_fields;
152is( $t2_f3->name, 'trait_name', 'Third field is "trait_name"' );
153is( $t2_f3->data_type, 'varchar2', 'Field is a varchar2' );
154is( $t2_f3->size, 200, 'Size is "200"' );
155is( $t2_f3->is_nullable, 0, 'Field cannot be null' );
156is( $t2_f3->is_foreign_key, 0, 'Field is not a FK' );
157
158my $t2_f4 = shift @t2_fields;
159is( $t2_f4->name, 'qtl_trait_category_id',
160 'Fourth field is "qtl_trait_category_id"' );
161is( $t2_f4->data_type, 'number', 'Field is a number' );
162is( $t2_f4->size, 11, 'Size is "11"' );
163is( $t2_f4->is_nullable, 0, 'Field cannot be null' );
164is( $t2_f4->is_foreign_key, 1, 'Field is a FK' );
165my $f4_fk = $t2_f4->foreign_key_reference;
166isa_ok( $f4_fk, 'SQL::Translator::Schema::Constraint', 'FK' );
167is( $f4_fk->reference_table, 'qtl_trait_category',
168 'FK references table "qtl_trait_category"' );
169is( join(',', $f4_fk->reference_fields), 'qtl_trait_category_id',
170 'FK references field "qtl_trait_category_id"' );
171
172my @t2_constraints = $t2->get_constraints;
173is( scalar @t2_constraints, 4, '4 constraints on table' );
174
175my $t2_c1 = shift @t2_constraints;
176is( $t2_c1->type, PRIMARY_KEY, 'First constraint is PK' );
177is( $t2_c1->name, 'pk_qtl_trait', 'Name is "pk_qtl_trait"' );
178is( join(',', $t2_c1->fields), 'qtl_trait_id', 'Fields = "qtl_trait_id"' );
179
180my $t2_c2 = shift @t2_constraints;
181is( $t2_c2->type, UNIQUE, 'Second constraint is unique' );
182is( $t2_c2->name, '', 'No name' );
183is( join(',', $t2_c2->fields), 'trait_symbol', 'Fields = "trait_symbol"' );
184
185my $t2_c3 = shift @t2_constraints;
186is( $t2_c3->type, UNIQUE, 'Third constraint is unique' );
187is( $t2_c3->name, '', 'No name' );
188is( join(',', $t2_c3->fields), 'trait_name', 'Fields = "trait_name"' );
189
190my $t2_c4 = shift @t2_constraints;
191is( $t2_c4->type, FOREIGN_KEY, 'Fourth constraint is FK' );
192is( $t2_c4->name, '', 'No name' );
193is( join(',', $t2_c4->fields), 'qtl_trait_category_id',
194 'Fields = "qtl_trait_category_id"' );
195is( $t2_c4->reference_table, 'qtl_trait_category',
196 'Reference table = "qtl_trait_category"' );
197is( join(',', $t2_c4->reference_fields), 'qtl_trait_category_id',
198 'Reference fields = "qtl_trait_category_id"' );
199
200
201#
202# qtl
203#
204my $t3 = shift @tables;
205is( $t3->name, 'qtl', 'Table "qtl" exists' );
206
207my @t3_fields = $t3->get_fields;
208is( scalar @t3_fields, 8, '8 fields in table' );
209
210my @t3_constraints = $t3->get_constraints;
7f6504d3 211is( scalar @t3_constraints, 4, '4 constraints on table' );
c4222efd 212my $t3_c4 = $t3_constraints[3];
213is( $t3_c4->type, UNIQUE, 'Fourth constraint is unique' );
214is( $t3_c4->name, 'qtl_accession_upper', 'Name = "qtl_accession_upper"' );
215is( join(',', $t3_c4->fields), 'UPPER(qtl_accession_id)', 'Fields = "UPPER(qtl_accession_id)"' );
75484db3 216
2eb042e6 217is( $t3->comments, 'qtl table comment', 'Comment "qtl table comment" exists' );
218
219my $t3_f1 = shift @t3_fields;
220is( $t3_f1->comments, 'qtl_id comment', 'Comment "qtl_id comment" exists' );
221
222my $t3_f2 = shift @t3_fields;
223is( $t3_f2->comments, 'accession comment',
224 'Comment "accession comment" exists' );
225
ae8a4ce3 226my @t3_indices = $t3->get_indices;
227is( scalar @t3_indices, 1, '1 index on table' );
228
229my $t3_i1 = shift @t3_indices;
230is( $t3_i1->type, 'NORMAL', 'First index is normal' );
231is( $t3_i1->name, 'qtl_index', 'Name is "qtl_index"' );
232is( join(',', $t3_i1->fields), 'qtl_accession_id', 'Fields = "qtl_accession_id"' );
233
75484db3 234#
235# qtl_trait_synonym
236#
237my $t4 = shift @tables;
238is( $t4->name, 'qtl_trait_synonym', 'Table "qtl_trait_synonym" exists' );
239
240my @t4_fields = $t4->get_fields;
241is( scalar @t4_fields, 3, '3 fields in table' );
242
243my @t4_constraints = $t4->get_constraints;
244is( scalar @t4_constraints, 3, '3 constraints on table' );
188a97b5 245my $t4_c3 = $t4_constraints[2];
246is( $t4_c3->type, FOREIGN_KEY, 'Third constraint is FK' );
247is( $t4_c3->name, '', 'No name' );
248is( join(',', $t4_c3->fields), 'qtl_trait_id',
249 'Fields = "qtl_trait_id"' );
250is( $t4_c3->reference_table, 'qtl_trait',
251 'Reference table = "qtl_trait"' );
252is( join(',', $t4_c3->reference_fields), 'qtl_trait_id',
253 'Reference fields = "qtl_trait_id"' );
254is( $t4_c3->on_delete, 'SET NULL',
255 'on_delete = "SET NULL"' );