4 use Test::More 'no_plan'; #tests => 105;
6 use SQL::Translator::Schema::Constants;
7 use SQL::Translator::Parser::Oracle qw(parse);
9 my $t = SQL::Translator->new( trace => 0 );
11 CREATE TABLE qtl_trait_category
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 )
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!';
22 CREATE TABLE qtl_trait
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
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,
45 UNIQUE ( qtl_accession_id ),
46 FOREIGN KEY ( qtl_trait_id ) REFERENCES qtl_trait
49 CREATE TABLE qtl_trait_synonym
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
62 my $data = parse( $t, $sql );
63 my $schema = $t->schema;
66 #print Dumper($schema), "\n";
68 isa_ok( $schema, 'SQL::Translator::Schema', 'Schema object' );
69 my @tables = $schema->get_tables;
70 is( scalar @tables, 4, 'Found four tables' );
75 my $t1 = shift @tables;
76 is( $t1->name, 'qtl_trait_category', 'First table is "qtl_trait_category"' );
77 is( $t1->comments, 'hey, hey, hey, hey', 'Comment = "hey, hey, hey, hey"' );
79 my @t1_fields = $t1->get_fields;
80 is( scalar @t1_fields, 2, '2 fields in table' );
82 my $f1 = shift @t1_fields;
83 is( $f1->name, 'qtl_trait_category_id',
84 'First field is "qtl_trait_category_id"' );
85 is( $f1->data_type, 'number', 'Field is a number' );
86 is( $f1->size, 11, 'Size is "11"' );
87 is( $f1->is_nullable, 0, 'Field cannot be null' );
88 is( $f1->default_value, undef, 'Default value is undefined' );
89 is( $f1->is_primary_key, 1, 'Field is PK' );
90 is( $f1->comments, 'the primary key!', 'Comment = "the primary key!"' );
92 my $f2 = shift @t1_fields;
93 is( $f2->name, 'trait_category', 'Second field is "trait_category"' );
94 is( $f2->data_type, 'varchar2', 'Field is a varchar2' );
95 is( $f2->size, 100, 'Size is "100"' );
96 is( $f2->is_nullable, 0, 'Field cannot be null' );
97 is( $f2->default_value, undef, 'Default value is undefined' );
98 is( $f2->is_primary_key, 0, 'Field is not PK' );
100 my @t1_indices = $t1->get_indices;
101 is( scalar @t1_indices, 0, '0 indices on table' );
103 my @t1_constraints = $t1->get_constraints;
104 is( scalar @t1_constraints, 2, '2 constraints on table' );
106 my $c1 = $t1_constraints[0];
107 is( $c1->name, 'pk_qtl_trait_category',
108 'Constraint name is "pk_qtl_trait_category"' );
109 is( $c1->type, PRIMARY_KEY, 'First constraint is PK' );
110 is( join(',', $c1->fields), 'qtl_trait_category_id',
111 'Constraint is on field "qtl_trait_category_id"' );
113 my $c2 = $t1_constraints[1];
114 is( $c2->type, UNIQUE, 'Second constraint is unique' );
115 is( join(',', $c2->fields), 'trait_category',
116 'Constraint is on field "trait_category"' );
121 my $t2 = shift @tables;
122 is( $t2->name, 'qtl_trait', 'Table "qtl_trait" exists' );
124 my @t2_fields = $t2->get_fields;
125 is( scalar @t2_fields, 4, '4 fields in table' );
127 my $t2_f1 = shift @t2_fields;
128 is( $t2_f1->name, 'qtl_trait_id', 'First field is "qtl_trait_id"' );
129 is( $t2_f1->data_type, 'number', 'Field is a number' );
130 is( $t2_f1->size, 11, 'Size is "11"' );
131 is( $t2_f1->is_nullable, 0, 'Field cannot be null' );
132 is( $t2_f1->default_value, undef, 'Default value is undefined' );
133 is( $t2_f1->is_primary_key, 1, 'Field is PK' );
135 my $t2_f2 = shift @t2_fields;
136 is( $t2_f2->name, 'trait_symbol', 'Second field is "trait_symbol"' );
137 is( $t2_f2->data_type, 'varchar2', 'Field is a varchar2' );
138 is( $t2_f2->size, 100, 'Size is "100"' );
139 is( $t2_f2->is_nullable, 0, 'Field cannot be null' );
140 is( $t2_f2->is_foreign_key, 0, 'Field is not a FK' );
142 my $t2_f3 = shift @t2_fields;
143 is( $t2_f3->name, 'trait_name', 'Third field is "trait_name"' );
144 is( $t2_f3->data_type, 'varchar2', 'Field is a varchar2' );
145 is( $t2_f3->size, 200, 'Size is "200"' );
146 is( $t2_f3->is_nullable, 0, 'Field cannot be null' );
147 is( $t2_f3->is_foreign_key, 0, 'Field is not a FK' );
149 my $t2_f4 = shift @t2_fields;
150 is( $t2_f4->name, 'qtl_trait_category_id',
151 'Fourth field is "qtl_trait_category_id"' );
152 is( $t2_f4->data_type, 'number', 'Field is a number' );
153 is( $t2_f4->size, 11, 'Size is "11"' );
154 is( $t2_f4->is_nullable, 0, 'Field cannot be null' );
155 is( $t2_f4->is_foreign_key, 1, 'Field is a FK' );
156 my $f4_fk = $t2_f4->foreign_key_reference;
157 isa_ok( $f4_fk, 'SQL::Translator::Schema::Constraint', 'FK' );
158 is( $f4_fk->reference_table, 'qtl_trait_category',
159 'FK references table "qtl_trait_category"' );
160 is( join(',', $f4_fk->reference_fields), 'qtl_trait_category_id',
161 'FK references field "qtl_trait_category_id"' );
163 my @t2_constraints = $t2->get_constraints;
164 is( scalar @t2_constraints, 4, '4 constraints on table' );
166 my $t2_c1 = shift @t2_constraints;
167 is( $t2_c1->type, PRIMARY_KEY, 'First constraint is PK' );
168 is( $t2_c1->name, 'pk_qtl_trait', 'Name is "pk_qtl_trait"' );
169 is( join(',', $t2_c1->fields), 'qtl_trait_id', 'Fields = "qtl_trait_id"' );
171 my $t2_c2 = shift @t2_constraints;
172 is( $t2_c2->type, UNIQUE, 'Second constraint is unique' );
173 is( $t2_c2->name, '', 'No name' );
174 is( join(',', $t2_c2->fields), 'trait_symbol', 'Fields = "trait_symbol"' );
176 my $t2_c3 = shift @t2_constraints;
177 is( $t2_c3->type, UNIQUE, 'Third constraint is unique' );
178 is( $t2_c3->name, '', 'No name' );
179 is( join(',', $t2_c3->fields), 'trait_name', 'Fields = "trait_name"' );
181 my $t2_c4 = shift @t2_constraints;
182 is( $t2_c4->type, FOREIGN_KEY, 'Fourth constraint is FK' );
183 is( $t2_c4->name, '', 'No name' );
184 is( join(',', $t2_c4->fields), 'qtl_trait_category_id',
185 'Fields = "qtl_trait_category_id"' );
186 is( $t2_c4->reference_table, 'qtl_trait_category',
187 'Reference table = "qtl_trait_category"' );
188 is( join(',', $t2_c4->reference_fields), 'qtl_trait_category_id',
189 'Reference fields = "qtl_trait_category_id"' );
195 my $t3 = shift @tables;
196 is( $t3->name, 'qtl', 'Table "qtl" exists' );
198 my @t3_fields = $t3->get_fields;
199 is( scalar @t3_fields, 8, '8 fields in table' );
201 my @t3_constraints = $t3->get_constraints;
202 is( scalar @t3_constraints, 3, '3 constraints on table' );
207 my $t4 = shift @tables;
208 is( $t4->name, 'qtl_trait_synonym', 'Table "qtl_trait_synonym" exists' );
210 my @t4_fields = $t4->get_fields;
211 is( scalar @t4_fields, 3, '3 fields in table' );
213 my @t4_constraints = $t4->get_constraints;
214 is( scalar @t4_constraints, 3, '3 constraints on table' );