Added Globals filter.
[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     Person:
35       name: Person
36       fields:
37         first_name:
38           data_type: foovar
39           name: first_name
40 };
41
42 my $ans_yaml = qq{---
43 schema:
44   procedures: {}
45   tables:
46     Person:
47       comments: ''
48       constraints: []
49       fields:
50         created:
51           comments: ''
52           data_type: timestamp
53           default_value: ~
54           extra: {}
55           is_nullable: 0
56           is_primary_key: 0
57           is_unique: 0
58           name: created
59           order: 3
60           size:
61             - 0
62         first_name:
63           comments: ''
64           data_type: foovar
65           default_value: ~
66           extra: {}
67           is_nullable: 1
68           is_primary_key: 0
69           is_unique: 0
70           name: first_name
71           order: 2
72           size:
73             - 0
74         modified:
75           comments: ''
76           data_type: timestamp
77           default_value: ~
78           extra: {}
79           is_nullable: 1
80           is_primary_key: 0
81           is_unique: 0
82           name: modified
83           order: 4
84           size:
85             - 0
86       indices:
87         - fields:
88             - created
89           name: ''
90           options: []
91           type: NORMAL
92         - fields:
93             - modified
94           name: ''
95           options: []
96           type: NORMAL
97       name: Person
98       options: []
99       order: 2
100   triggers: {}
101   views: {}
102 translator:
103   add_drop_table: 0
104   filename: ~
105   no_comments: 0
106   parser_args: {}
107   parser_type: SQL::Translator::Parser::YAML
108   producer_args: {}
109   producer_type: SQL::Translator::Producer::YAML
110   show_warnings: 1
111   trace: 0
112   version: 0.07
113 };
114
115 # Parse the test XML schema
116 my $obj;
117 $obj = SQL::Translator->new(
118     debug         => 0,
119     show_warnings => 1,
120     from          => "YAML",
121     to            => "YAML",
122     data          => $in_yaml,
123     filters => [
124         # Filter from SQL::Translator::Filter::*
125         [ 'Globals',
126             # A global field to add given in the args
127             fields => [
128                 {
129                     name => 'created',
130                     data_type => 'timestamp',
131                     is_nullable => 0,
132                 }
133             ],
134             indices => [
135                 {
136                     fields => 'created',
137                 }
138             ],
139         ],
140     ],
141
142 ) or die "Failed to create translator object: ".SQL::Translator->error;
143
144 my $out;
145 lives_ok { $out = $obj->translate; }  "Translate ran";
146 is $obj->error, ''                   ,"No errors";
147 ok $out ne ""                        ,"Produced something!";
148 eq_or_diff $out, $ans_yaml           ,"Output looks right";
149 #print "$out\n";