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