Bit size can range from 1 to 64, test added for size greater than one
[dbsrgits/SQL-Translator.git] / t / 24yaml.t
1 use warnings;
2 use strict;
3 use Test::More;
4 use Test::Differences;
5 use Test::SQL::Translator qw(maybe_plan);
6 use SQL::Translator;
7 use FindBin '$Bin';
8
9 BEGIN {
10     maybe_plan(2,
11         'SQL::Translator::Parser::SQLite',
12         'SQL::Translator::Producer::YAML');
13 }
14
15 my $sqlt_version = $SQL::Translator::VERSION;
16 my $yaml = <<YAML;
17 ---
18 schema:
19   procedures: {}
20   tables:
21     person:
22       constraints:
23         - deferrable: 1
24           expression: ''
25           fields:
26             - person_id
27           match_type: ''
28           name: ''
29           on_delete: ''
30           on_update: ''
31           options: []
32           reference_fields: []
33           reference_table: ''
34           type: PRIMARY KEY
35         - deferrable: 1
36           expression: ''
37           fields:
38             - name
39           match_type: ''
40           name: u_name
41           on_delete: ''
42           on_update: ''
43           options: []
44           reference_fields: []
45           reference_table: ''
46           type: UNIQUE
47       fields:
48         age:
49           data_type: integer
50           default_value: ~
51           is_nullable: 1
52           is_primary_key: 0
53           is_unique: 0
54           name: age
55           order: 3
56           size:
57             - 0
58         description:
59           data_type: text
60           default_value: ~
61           is_nullable: 1
62           is_primary_key: 0
63           is_unique: 0
64           name: description
65           order: 6
66           size:
67             - 0
68         iq:
69           data_type: tinyint
70           default_value: 0
71           is_nullable: 1
72           is_primary_key: 0
73           is_unique: 0
74           name: iq
75           order: 5
76           size:
77             - 0
78         name:
79           data_type: varchar
80           default_value: ~
81           is_nullable: 0
82           is_primary_key: 0
83           is_unique: 1
84           name: name
85           order: 2
86           size:
87             - 20
88         person_id:
89           data_type: INTEGER
90           default_value: ~
91           is_auto_increment: 1
92           is_nullable: 0
93           is_primary_key: 1
94           is_unique: 0
95           name: person_id
96           order: 1
97           size:
98             - 0
99         weight:
100           data_type: double
101           default_value: ~
102           is_nullable: 1
103           is_primary_key: 0
104           is_unique: 0
105           name: weight
106           order: 4
107           size:
108             - 11
109             - 2
110       indices: []
111       name: person
112       options: []
113       order: 1
114     pet:
115       constraints:
116         - deferrable: 1
117           expression: ''
118           fields: []
119           match_type: ''
120           name: ''
121           on_delete: ''
122           on_update: ''
123           options: []
124           reference_fields: []
125           reference_table: ''
126           type: CHECK
127         - deferrable: 1
128           expression: ''
129           fields:
130             - pet_id
131             - person_id
132           match_type: ''
133           name: ''
134           on_delete: ''
135           on_update: ''
136           options: []
137           reference_fields: []
138           reference_table: ''
139           type: PRIMARY KEY
140         - deferrable: 1
141           expression: ''
142           fields:
143             - person_id
144           match_type: ''
145           name: ''
146           on_delete: ''
147           on_update: ''
148           options: []
149           reference_fields:
150             - person_id
151           reference_table: person
152           type: FOREIGN KEY
153       fields:
154         age:
155           data_type: int
156           default_value: ~
157           is_nullable: 1
158           is_primary_key: 0
159           is_unique: 0
160           name: age
161           order: 4
162           size:
163             - 0
164         name:
165           data_type: varchar
166           default_value: ~
167           is_nullable: 1
168           is_primary_key: 0
169           is_unique: 0
170           name: name
171           order: 3
172           size:
173             - 30
174         person_id:
175           data_type: int
176           default_value: ~
177           is_nullable: 0
178           is_primary_key: 1
179           is_unique: 0
180           name: person_id
181           order: 2
182           size:
183             - 0
184         pet_id:
185           data_type: int
186           default_value: ~
187           is_nullable: 0
188           is_primary_key: 1
189           is_unique: 0
190           name: pet_id
191           order: 1
192           size:
193             - 0
194       indices: []
195       name: pet
196       options: []
197       order: 2
198   triggers:
199     pet_trig:
200       action:
201         for_each: ~
202         steps:
203           - update pet set name=name
204         when: ~
205       database_events:
206         - insert
207       fields: ~
208       name: pet_trig
209       on_table: pet
210       order: 1
211       perform_action_when: after
212   views:
213     person_pet:
214       fields: ''
215       name: person_pet
216       order: 1
217       sql: |
218         select pr.person_id, pr.name as person_name, pt.name as pet_name
219           from   person pr, pet pt
220           where  person.person_id=pet.pet_id
221 translator:
222   add_drop_table: 0
223   filename: ~
224   no_comments: 0
225   parser_args: {}
226   parser_type: SQL::Translator::Parser::SQLite
227   producer_args: {}
228   producer_type: SQL::Translator::Producer::YAML
229   show_warnings: 0
230   trace: 0
231   version: $sqlt_version
232 YAML
233
234 my $file = "$Bin/data/sqlite/create.sql";
235 open FH, "<$file" or die "Can't read '$file': $!\n";
236 local $/;
237 my $data = <FH>;
238 my $tr   = SQL::Translator->new(
239     parser   => 'SQLite',
240     producer => 'YAML',
241     data     => $data,
242 );
243
244 my $out;
245 ok( $out = $tr->translate, 'Translate SQLite to YAML' );
246 eq_or_diff( $out, $yaml, 'YAML matches expected' );