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