Experimental filters
[dbsrgits/SQL-Translator.git] / t / 38-filter-names.t
CommitLineData
6cedfc23 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(4, 'YAML', 'Test::Differences')
41}
42use Test::Differences;
43use SQL::Translator;
44
45my $in_yaml = qq{---
46schema:
47 tables:
48 Person:
49 name: Person
50 fields:
51 first_name:
52 data_type: foovar
53 name: first_name
54};
55
56# helloworld:
57# comments: ''
58# constraints: []
59# fields: {}
60# indices: []
61# name: HelloWorld
62# options: []
63# order: 2
64my $ans_yaml = qq{---
65schema:
66 procedures: {}
67 tables:
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.07
100};
101
102# Parse the test XML schema
103my $obj;
104$obj = SQL::Translator->new(
105 debug => 0,
106 show_warnings => 1,
107 from => "YAML",
108 to => "YAML",
109 data => $in_yaml,
110 filters => [
111 # Filter from SQL::Translator::Filter::*
112 [ 'Names', {
113 tables => 'lc',
114 fields => 'ucfirst',
115 } ],
116 ],
117
118) or die "Failed to create translator object: ".SQL::Translator->error;
119
120#sub translate_ok {
121# my ($sqlt,$ans_yaml,$name) = @_;
122# $name ||= "";
123#
124# my $out = eval { $sqlt->translate };
125# fail( $sqlt->error ) if $sqlt->error;
126# fail( "No output" ) unless $out;
127# eq_or_diff $out, $ans_yaml ,"Translated $name";
128#}
129
130my $out;
131lives_ok { $out = $obj->translate; } "Translate ran";
132is $obj->error, '' ,"No errors";
133ok $out ne "" ,"Produced something!";
134eq_or_diff $out, $ans_yaml ,"Output looks right";