Fixed YAML testi. Failed due to sqlfairy version increase and YAML not putting
[dbsrgits/SQL-Translator.git] / t / 36-filters.t
CommitLineData
185c34d5 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# SQL::Translator::Filter::HelloWorld - Test filter in a package
8#=============================================================================
9package SQL::Translator::Filter::HelloWorld;
10
11use strict;
12use vars qw/$VERSION/;
13$VERSION=0.1;
14
15sub filter {
16 my ($schema,$args) = (shift,shift);
17
18 my $greeting = $args->{greeting} || "Hello";
19 $schema->add_table(
20 name => "HelloWorld",
21 );
22}
23
24# Hack to allow sqlt to see our module as it wasn't loaded from a .pm
25$INC{'SQL/Translator/Filter/HelloWorld.pm'}
26 = 'lib/SQL/Translator/Filter/HelloWorld.pm';
27
28#=============================================================================
29
30package main;
31
32use strict;
33use Test::More;
34use Test::Exception;
35use Test::SQL::Translator qw(maybe_plan);
36
37use Data::Dumper;
38
39BEGIN {
40 maybe_plan(14, 'Template', 'Test::Differences')
41}
42use Test::Differences;
43use SQL::Translator;
44
45my $in_yaml = qq{--- #YAML:1.0
46schema:
47 tables:
48 person:
49 name: person
50 fields:
51 first_name:
52 data_type: foovar
53 name: First_Name
54};
55
56my $ans_yaml = qq{--- #YAML:1.0
57schema:
58 procedures: {}
59 tables:
60 HelloWorld:
61 comments: ''
62 constraints: []
63 fields: {}
64 indices: []
65 name: HelloWorld
66 options: []
67 order: 2
68 PERSON:
69 comments: ''
70 constraints: []
71 fields:
72 first_name:
73 data_type: foovar
74 default_value: ~
75 extra: {}
76 is_nullable: 1
77 is_primary_key: 0
78 is_unique: 0
79 name: first_name
80 order: 1
81 size:
82 - 0
83 indices: []
84 name: PERSON
85 options: []
86 order: 1
87 triggers: {}
88 views: {}
89translator:
90 add_drop_table: 0
91 filename: ~
92 no_comments: 0
93 parser_args: {}
94 parser_type: SQL::Translator::Parser::YAML
95 producer_args: {}
96 producer_type: SQL::Translator::Producer::YAML
97 show_warnings: 1
98 trace: 0
99 version: 0.06
100};
101
102# Parse the test XML schema
103my $obj;
104$obj = SQL::Translator->new(
105 debug => 0,
106 show_warnings => 1,
107 parser => "YAML",
108 data => $in_yaml,
109 to => "YAML",
110 filters => [
111 # Check they get called ok
112 sub {
113 pass("Filter 1 called");
114 isa_ok($_[0],"SQL::Translator::Schema", "Filter 1, arg0 ");
115 ok( ref($_[1]) eq "HASH", "Filter 1, arg1 is a hashref ");
116 },
117 sub {
118 pass("Filter 2 called");
119 isa_ok($_[0],"SQL::Translator::Schema", "Filter 2, arg0 ");
120 ok( ref($_[1]) eq "HASH", "Filter 2, arg1 is a hashref ");
121 },
122
123 # Sub filter with args
124 [ sub {
125 pass("Filter 3 called");
126 isa_ok($_[0],"SQL::Translator::Schema", "Filter 3, arg0 ");
127 ok( ref($_[1]) eq "HASH", "Filter 3, arg1 is a hashref ");
128 is( $_[1]->{hello}, "world", "Filter 3, got args ");
129 },
130 { hello=>"world" } ],
131
132 # Uppercase all the table names.
133 sub {
134 my $schema = shift;
135 foreach ($schema->get_tables) {
136 $_->name(uc $_->name);
137 }
138 },
139
140 # lowercase all the field names.
141 sub {
142 my $schema = shift;
143 foreach ( map { $_->get_fields } $schema->get_tables ) {
144 $_->name(lc $_->name);
145 }
146 },
147
148 # Filter from SQL::Translator::Filter::*
149 [ 'HelloWorld' ],
150 ],
151
152) or die "Failed to create translator object: ".SQL::Translator->error;
153
154my $out;
155lives_ok { $out = $obj->translate; } "Translate ran";
156is $obj->error, '' ,"No errors";
157ok $out ne "" ,"Produced something!";
158eq_or_diff $out, $ans_yaml ,"Output looks right";