dfc4f36064e4c9937ae52c7a3316128c8269bedb
[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       comments: ''
56       constraints:
57         - deferrable: 1
58           expression: ''
59           fields:
60             - modified
61           match_type: ''
62           name: ''
63           on_delete: ''
64           on_update: ''
65           options: []
66           reference_fields: []
67           reference_table: ''
68           type: UNIQUE
69       fields:
70         created:
71           comments: ''
72           data_type: timestamp
73           default_value: ~
74           extra: {}
75           is_nullable: 0
76           is_primary_key: 0
77           is_unique: 0
78           name: created
79           order: 3
80           size:
81             - 0
82         first_name:
83           comments: ''
84           data_type: foovar
85           default_value: ~
86           extra: {}
87           is_nullable: 1
88           is_primary_key: 0
89           is_unique: 0
90           name: first_name
91           order: 2
92           size:
93             - 0
94         modified:
95           comments: ''
96           data_type: timestamp
97           default_value: ~
98           extra: {}
99           is_nullable: 1
100           is_primary_key: 0
101           is_unique: 1
102           name: modified
103           order: 4
104           size:
105             - 0
106       indices:
107         - fields:
108             - created
109           name: ''
110           options: []
111           type: NORMAL
112         - fields:
113             - modified
114           name: ''
115           options: []
116           type: NORMAL
117       name: Person
118       options: []
119       order: 2
120   triggers: {}
121   views: {}
122 translator:
123   add_drop_table: 0
124   filename: ~
125   no_comments: 0
126   parser_args: {}
127   parser_type: SQL::Translator::Parser::YAML
128   producer_args: {}
129   producer_type: SQL::Translator::Producer::YAML
130   show_warnings: 1
131   trace: 0
132   version: $sqlt_version
133 };
134
135
136 # Parse the test XML schema
137 my $obj;
138 $obj = SQL::Translator->new(
139     debug         => 0,
140     show_warnings => 1,
141     from          => "YAML",
142     to            => "YAML",
143     data          => $in_yaml,
144     filters => [
145         # Filter from SQL::Translator::Filter::*
146         [ 'Globals',
147             # A global field to add given in the args
148             fields => [
149                 {
150                     name => 'created',
151                     data_type => 'timestamp',
152                     is_nullable => 0,
153                 }
154             ],
155             indices => [
156                 {
157                     fields => 'created',
158                 }
159             ],
160         ],
161     ],
162
163 ) or die "Failed to create translator object: ".SQL::Translator->error;
164
165 my $out;
166 lives_ok { $out = $obj->translate; }  "Translate ran";
167 is $obj->error, ''                   ,"No errors";
168 ok $out ne ""                        ,"Produced something!";
169 eq_or_diff $out, $ans_yaml           ,"Output looks right";
170 #print "$out\n";