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