Added _attributes class data to SQL::Translator::Schema::Object for sub classes
[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#=============================================================================
c957e92d 28
ec791002 29BEGIN {
30 maybe_plan(142, 'SQL::Translator::Parser::XML::SQLFairy');
07a82527 31}
32
ec791002 33my $testschema = "$Bin/data/xml/schema.xml";
34
35my $sqlt;
36$sqlt = SQL::Translator->new(
37 debug => DEBUG,
38 show_warnings => 1,
39 add_drop_table => 1,
40);
41die "Can't find test schema $testschema" unless -e $testschema;
42my $sql = $sqlt->translate(
43 from => 'XML-SQLFairy',
44 to => 'MySQL',
45 filename => $testschema,
46) or die $sqlt->error;
47print $sql if DEBUG;
48
49# Test the schema objs generted from the XML
50#
51my $scma = $sqlt->schema;
52
53# Hmmm, when using schema_ok the field test data gets a bit too nested and
54# fiddly to work with. (See 28xml-xmi-parser-sqlfairy.t for more a split out
55# version)
56schema_ok( $scma, {
57 tables => [
58 {
59 name => "Basic",
60 fields => [
61 {
62 name => "id",
63 data_type => "int",
64 default_value => undef,
65 is_nullable => 0,
66 size => 10,
67 is_primary_key => 1,
68 is_auto_increment => 1,
94ed484b 69 extra => { ZEROFILL => 1 },
ec791002 70 },
71 {
72 name => "title",
73 data_type => "varchar",
74 is_nullable => 0,
75 default_value => "hello",
76 size => 100,
77 },
78 {
79 name => "description",
80 data_type => "text",
81 is_nullable => 1,
82 default_value => "",
83 },
84 {
85 name => "email",
86 data_type => "varchar",
87 size => 255,
88 is_unique => 1,
89 default_value => undef,
90 is_nullable => 1,
94ed484b 91 extra => {
92 foo => "bar",
93 hello => "world",
94 bar => "baz",
95 }
ec791002 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,
94ed484b 114 comments => "Hello emptytagdef",
ec791002 115 },
116 ],
117 constraints => [
118 {
119 type => PRIMARY_KEY,
120 fields => ["id"],
121 },
122 {
123 name => 'emailuniqueindex',
124 type => UNIQUE,
125 fields => ["email"],
126 }
127 ],
128 indices => [
129 {
130 name => "titleindex",
131 fields => ["title"],
132 },
133 ],
134 } # end table Basic
135 ], # end tables
136
137 views => [
138 {
139 name => 'email_list',
140 sql => "SELECT email FROM Basic WHERE email IS NOT NULL",
141 fields => ['email'],
142 },
143 ],
144
145 triggers => [
146 {
147 name => 'foo_trigger',
148 perform_action_when => 'after',
149 database_event => 'insert',
150 on_table => 'foo',
151 action => 'update modified=timestamp();',
152 },
153 ],
154
155 procedures => [
156 {
157 name => 'foo_proc',
158 sql => 'select foo from bar',
159 parameters => ['foo', 'bar'],
160 owner => 'Nomar',
161 comments => 'Go Sox!',
162 },
163 ],
164
165}); # end schema