Added constraint copy to Globals filter. (Seem to be getting lots of warnings from...
[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 # The _GLOBAL_ table should be removed and its fields copied onto all other
21 # tables.
22 my $in_yaml = qq{---
23 schema:
24   tables:
25     _GLOBAL_:
26       name: _GLOBAL_
27       fields:
28         modified:
29           name: modified
30           data_type: timestamp
31       indices:
32         - fields:
33             - modified
34       constraints:
35         - fields:
36             - modified
37           type: UNIQUE
38     Person:
39       name: Person
40       fields:
41         first_name:
42           data_type: foovar
43           name: first_name
44 };
45
46 # Should include the the items added from the Global table defined above in the
47 # schema as well as those defined in the filter args below.
48 my $ans_yaml = qq{---
49 schema:
50   procedures: {}
51   tables:
52     Person:
53       comments: ''
54       constraints:
55         - deferrable: 1
56           expression: ''
57           fields:
58             - modified
59           match_type: ''
60           name: ''
61           on_delete: ''
62           on_update: ''
63           options: []
64           reference_fields: []
65           reference_table: ''
66           type: UNIQUE
67       fields:
68         created:
69           comments: ''
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           comments: ''
82           data_type: foovar
83           default_value: ~
84           extra: {}
85           is_nullable: 1
86           is_primary_key: 0
87           is_unique: 0
88           name: first_name
89           order: 2
90           size:
91             - 0
92         modified:
93           comments: ''
94           data_type: timestamp
95           default_value: ~
96           extra: {}
97           is_nullable: 1
98           is_primary_key: 0
99           is_unique: 1
100           name: modified
101           order: 4
102           size:
103             - 0
104       indices:
105         - fields:
106             - created
107           name: ''
108           options: []
109           type: NORMAL
110         - fields:
111             - modified
112           name: ''
113           options: []
114           type: NORMAL
115       name: Person
116       options: []
117       order: 2
118   triggers: {}
119   views: {}
120 translator:
121   add_drop_table: 0
122   filename: ~
123   no_comments: 0
124   parser_args: {}
125   parser_type: SQL::Translator::Parser::YAML
126   producer_args: {}
127   producer_type: SQL::Translator::Producer::YAML
128   show_warnings: 1
129   trace: 0
130   version: 0.07
131 };
132
133
134 # Parse the test XML schema
135 my $obj;
136 $obj = SQL::Translator->new(
137     debug         => 0,
138     show_warnings => 1,
139     from          => "YAML",
140     to            => "YAML",
141     data          => $in_yaml,
142     filters => [
143         # Filter from SQL::Translator::Filter::*
144         [ 'Globals',
145             # A global field to add given in the args
146             fields => [
147                 {
148                     name => 'created',
149                     data_type => 'timestamp',
150                     is_nullable => 0,
151                 }
152             ],
153             indices => [
154                 {
155                     fields => 'created',
156                 }
157             ],
158         ],
159     ],
160
161 ) or die "Failed to create translator object: ".SQL::Translator->error;
162
163 my $out;
164 lives_ok { $out = $obj->translate; }  "Translate ran";
165 is $obj->error, ''                   ,"No errors";
166 ok $out ne ""                        ,"Produced something!";
167 eq_or_diff $out, $ans_yaml           ,"Output looks right";
168 #print "$out\n";