Teach YAML producer to encode extra attributes
[dbsrgits/SQL-Translator.git] / t / 39-filter-globals.t
1 #!/usr/bin/perl -w
2 # vim:filetype=perl
3
4 # Before `make install' is performed this script should be runnable with
5 # `make test'. After `make install' it should work as `perl test.pl'
6
7 use strict;
8 use Test::More;
9 use Test::Exception;
10 use Test::SQL::Translator qw(maybe_plan);
11
12 use Data::Dumper;
13
14 BEGIN {
15     maybe_plan(4, 'YAML', 'Test::Differences')
16 }
17 use Test::Differences;
18 use SQL::Translator;
19
20 my $sqlt_version = $SQL::Translator::VERSION;
21
22 # The _GLOBAL_ table should be removed and its fields copied onto all other
23 # tables.
24 my $in_yaml = qq{---
25 schema:
26   tables:
27     _GLOBAL_:
28       name: _GLOBAL_
29       fields:
30         modified:
31           name: modified
32           data_type: timestamp
33       indices:
34         - fields:
35             - modified
36       constraints:
37         - fields:
38             - modified
39           type: UNIQUE
40     Person:
41       name: Person
42       fields:
43         first_name:
44           data_type: foovar
45           name: first_name
46 };
47
48 # Should include the the items added from the Global table defined above in the
49 # schema as well as those defined in the filter args below.
50 my $ans_yaml = qq{---
51 schema:
52   procedures: {}
53   tables:
54     Person:
55       constraints:
56         - deferrable: 1
57           expression: ''
58           fields:
59             - modified
60           match_type: ''
61           name: ''
62           on_delete: ''
63           on_update: ''
64           options: []
65           reference_fields: []
66           reference_table: ''
67           type: UNIQUE
68       fields:
69         created:
70           data_type: timestamp
71           default_value: ~
72           is_nullable: 0
73           is_primary_key: 0
74           is_unique: 0
75           name: created
76           order: 2
77           size:
78             - 0
79         first_name:
80           data_type: foovar
81           default_value: ~
82           is_nullable: 1
83           is_primary_key: 0
84           is_unique: 0
85           name: first_name
86           order: 1
87           size:
88             - 0
89         modified:
90           data_type: timestamp
91           default_value: ~
92           is_nullable: 1
93           is_primary_key: 0
94           is_unique: 1
95           name: modified
96           order: 3
97           size:
98             - 0
99       indices:
100         - fields:
101             - created
102           name: ''
103           options: []
104           type: NORMAL
105         - fields:
106             - modified
107           name: ''
108           options: []
109           type: NORMAL
110       name: Person
111       options: []
112       order: 2
113   triggers: {}
114   views: {}
115 translator:
116   add_drop_table: 0
117   filename: ~
118   no_comments: 0
119   parser_args: {}
120   parser_type: SQL::Translator::Parser::YAML
121   producer_args: {}
122   producer_type: SQL::Translator::Producer::YAML
123   show_warnings: 1
124   trace: 0
125   version: $sqlt_version
126 };
127
128
129 # Parse the test XML schema
130 my $obj;
131 $obj = SQL::Translator->new(
132     debug         => 0,
133     show_warnings => 1,
134     from          => "YAML",
135     to            => "YAML",
136     data          => $in_yaml,
137     filters => [
138         # Filter from SQL::Translator::Filter::*
139         [ 'Globals',
140             # A global field to add given in the args
141             fields => [
142                 {
143                     name => 'created',
144                     data_type => 'timestamp',
145                     is_nullable => 0,
146                 }
147             ],
148             indices => [
149                 {
150                     fields => 'created',
151                 }
152             ],
153         ],
154     ],
155
156 ) or die "Failed to create translator object: ".SQL::Translator->error;
157
158 my $out;
159 lives_ok { $out = $obj->translate; }  "Translate ran";
160 is $obj->error, ''                   ,"No errors";
161 ok $out ne ""                        ,"Produced something!";
162 eq_or_diff $out, $ans_yaml           ,"Output looks right";
163 #print "$out\n";