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