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