Add IRC metadata and update repository and bugtracker URLs
[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 BEGIN {
13     maybe_plan(3, 'YAML', 'Test::Differences')
14 }
15 use Test::Differences;
16 use SQL::Translator;
17
18 # The _GLOBAL_ table should be removed and its fields copied onto all other
19 # tables.
20 #
21 # FIXME - the loader should not require order for globals, needs to be able
22 # to recognize/sort approproately
23 my $in_yaml = qq{---
24 schema:
25   tables:
26     _GLOBAL_:
27       order: 99
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       order: 1
42       name: Person
43       fields:
44         first_name:
45           data_type: foovar
46           name: first_name
47 };
48
49
50
51 # Parse the test XML schema
52 my $obj;
53 $obj = SQL::Translator->new(
54     debug         => 0,
55     show_warnings => 1,
56     from          => "YAML",
57     to            => "YAML",
58     data          => $in_yaml,
59     filters => [
60         # Filter from SQL::Translator::Filter::*
61         [ 'Globals',
62             # A global field to add given in the args
63             fields => [
64                 {
65                     name => 'created',
66                     data_type => 'timestamp',
67                     is_nullable => 0,
68                 }
69             ],
70             indices => [
71                 {
72                     fields => 'created',
73                 }
74             ],
75         ],
76     ],
77
78 ) or die "Failed to create translator object: ".SQL::Translator->error;
79
80 my $struct;
81 lives_ok { $struct = YAML::Load($obj->translate) }  "Translate/yaml reload ran";
82 is $obj->error, '', "No errors";
83
84 # Should include the the items added from the Global table defined above in the
85 # schema as well as those defined in the filter args below.
86 is_deeply ($struct, {
87   schema => {
88     procedures => {},
89     tables => {
90       Person => {
91         constraints => [
92           {
93             deferrable => 1,
94             expression => "",
95             fields => [
96               "modified"
97             ],
98             match_type => "",
99             name => "",
100             on_delete => "",
101             on_update => "",
102             options => [],
103             reference_fields => [],
104             reference_table => "",
105             type => "UNIQUE"
106           }
107         ],
108         fields => {
109           first_name => {
110             data_type => "foovar",
111             default_value => undef,
112             is_nullable => 1,
113             is_primary_key => 0,
114             is_unique => 0,
115             name => "first_name",
116             order => 1,
117             size => [
118               0
119             ]
120           },
121           created => {
122             data_type => "timestamp",
123             default_value => undef,
124             is_nullable => 0,
125             is_primary_key => 0,
126             is_unique => 0,
127             name => "created",
128             order => 2,
129             size => [
130               0
131             ]
132           },
133           modified => {
134             data_type => "timestamp",
135             default_value => undef,
136             is_nullable => 1,
137             is_primary_key => 0,
138             is_unique => 1,
139             name => "modified",
140             order => 3,
141             size => [
142               0
143             ]
144           }
145         },
146         indices => [
147           {
148             fields => [
149               "created"
150             ],
151             name => "",
152             options => [],
153             type => "NORMAL"
154           },
155           {
156             fields => [
157               "modified"
158             ],
159             name => "",
160             options => [],
161             type => "NORMAL"
162           }
163         ],
164         name => "Person",
165         options => [],
166         order => 1
167       }
168     },
169     triggers => {},
170     views => {}
171   },
172   translator => {
173     add_drop_table => 0,
174     filename => undef,
175     no_comments => 0,
176     parser_args => {},
177     parser_type => "SQL::Translator::Parser::YAML",
178     producer_args => {},
179     producer_type => "SQL::Translator::Producer::YAML",
180     show_warnings => 1,
181     trace => 0,
182     version => $SQL::Translator::VERSION,
183   }
184 }, 'Expected final yaml-schema');