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