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