Now uses schema_ok
[dbsrgits/SQL-Translator.git] / t / 16xml-parser.t
CommitLineData
c957e92d 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'
c957e92d 6#
1c375f48 7# Run script with -d for debug.
c957e92d 8
2e11379e 9use strict;
1c375f48 10
11use FindBin qw/$Bin/;
12
b3530353 13use Test::More;
1c375f48 14use Test::SQL::Translator;
c957e92d 15use Test::Exception;
c957e92d 16use Data::Dumper;
1c375f48 17use SQL::Translator;
18use SQL::Translator::Schema::Constants;
19
20# Simple options. -d for debug
2e11379e 21my %opt;
c957e92d 22BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; }
23use constant DEBUG => (exists $opt{d} ? 1 : 0);
c957e92d 24
c957e92d 25
26# Testing 1,2,3,4...
27#=============================================================================
28
9c36b41b 29plan tests => 284;
c957e92d 30
07a82527 31foreach (
32 "$Bin/data/xml/schema-basic.xml",
33 "$Bin/data/xml/schema-basic-attribs.xml"
34) {
35 do_file($_);
36}
37
38sub do_file {
39 my $testschema = shift;
40 # Parse the test XML schema
2e11379e 41 my $obj;
07a82527 42 $obj = SQL::Translator->new(
43 debug => DEBUG,
44 show_warnings => 1,
45 add_drop_table => 1,
46 );
47 die "Can't find test schema $testschema" unless -e $testschema;
48 my $sql = $obj->translate(
2e11379e 49 from => 'XML-SQLFairy',
50 to => 'MySQL',
07a82527 51 filename => $testschema,
52 );
53 print $sql if DEBUG;
07a82527 54
55 # Test the schema objs generted from the XML
56 #
57 my $scma = $obj->schema;
9c36b41b 58
59 # Hmmm, when using schema_ok the field test data gets a bit too nested and
60 # fiddly to work with. (See 28xml-xmi-parser-sqlfairy.t for more split out
61 # version)
62 schema_ok( $scma, {
63 tables => [
64 {
65 name => "Basic",
66 fields => [
67 {
68 name => "id",
69 data_type => "int",
70 default_value => undef,
71 is_nullable => 0,
72 size => 10,
73 is_primary_key => 1,
74 is_auto_increment => 1,
75 },
76 {
77 name => "title",
78 data_type => "varchar",
79 is_nullable => 0,
80 default_value => "hello",
81 size => 100,
82 },
83 {
84 name => "description",
85 data_type => "text",
86 is_nullable => 1,
87 default_value => "",
88 },
89 {
90 name => "email",
91 data_type => "varchar",
92 size => 255,
93 is_unique => 1,
94 default_value => undef,
95 is_nullable => 1,
96 },
97 {
98 name => "explicitnulldef",
99 data_type => "varchar",
100 default_value => undef,
101 is_nullable => 1,
102 },
103 {
104 name => "explicitemptystring",
105 data_type => "varchar",
106 default_value => "",
107 is_nullable => 1,
108 },
109 {
110 name => "emptytagdef",
111 data_type => "varchar",
112 default_value => "",
113 is_nullable => 1,
114 },
115 ],
116 constraints => [
117 {
118 type => PRIMARY_KEY,
119 fields => ["id"],
120 },
121 {
122 name => 'emailuniqueindex',
123 type => UNIQUE,
124 fields => ["email"],
125 }
126 ],
127 indices => [
128 {
129 name => "titleindex",
130 fields => ["title"],
131 },
132 ],
133 } # end table Basic
134 ], # end tables
135
136 views => [
137 {
138 name => 'email_list',
139 sql => "SELECT email FROM Basic WHERE email IS NOT NULL",
140 fields => ['email'],
141 },
1c375f48 142 ],
9c36b41b 143
144 triggers => [
145 {
146 name => 'foo_trigger',
147 perform_action_when => 'after',
148 database_event => 'insert',
149 on_table => 'foo',
150 action => 'update modified=timestamp();',
151 },
1c375f48 152 ],
19922fbc 153
9c36b41b 154 procedures => [
155 {
156 name => 'foo_proc',
157 sql => 'select foo from bar',
158 parameters => ['foo', 'bar'],
159 owner => 'Nomar',
160 comments => 'Go Sox!',
161 },
162 ],
1c375f48 163
9c36b41b 164 }); # end schema
19922fbc 165
9c36b41b 166} # end do_file()