Added the new files
[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'
6
7#
8# basic.t
9# -------
10# Tests that;
11#
12
b3530353 13use Test::More;
c957e92d 14use Test::Exception;
15
16use strict;
17use Data::Dumper;
18our %opt;
19BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; }
20use constant DEBUG => (exists $opt{d} ? 1 : 0);
21local $SIG{__WARN__} = sub { diag "[warn] ", @_; };
22
23use FindBin qw/$Bin/;
24
25# Usefull test subs for the schema objs
26#=============================================================================
27
28our %ATTRIBUTES;
29$ATTRIBUTES{field} = [qw/
30name
31order
32data_type
33default_value
34size
35is_primary_key
36is_unique
37is_nullable
38is_foreign_key
39is_auto_increment
40/];
41
42sub test_field {
5ff70f1a 43 my ($fld,$test) = @_;
44 die "test_field needs a least a name!" unless $test->{name};
45 my $name = $test->{name};
46 is $fld->name, $name, "$name - Name right";
47
48 foreach my $attr ( @{$ATTRIBUTES{field}} ) {
49 if ( exists $test->{$attr} ) {
50 my $ans = $test->{$attr};
51 if ( $attr =~ m/^is_/ ) {
52 if ($ans) { ok $fld->$attr, " $name - $attr true"; }
53 else { ok !$fld->$attr, " $name - $attr false"; }
54 }
55 else {
56 is $fld->$attr, $ans, " $name - $attr = '"
57 .(defined $ans ? $ans : "NULL" )."'";
58 }
59 }
60 else {
61 ok !$fld->$attr, "$name - $attr not set";
62 }
63 }
c957e92d 64}
65
66# TODO test_constraint, test_index
67
68# Testing 1,2,3,4...
69#=============================================================================
70
b3530353 71plan tests => 89;
72
c957e92d 73use SQL::Translator;
74use SQL::Translator::Schema::Constants;
75
76# Parse the test XML schema
77our $obj;
78$obj = SQL::Translator->new(
5ff70f1a 79 debug => DEBUG,
80 show_warnings => 1,
81 add_drop_table => 1,
c957e92d 82);
83my $testschema = "$Bin/data/xml/schema-basic.xml";
84die "Can't find test schema $testschema" unless -e $testschema;
85my $sql = $obj->translate(
5ff70f1a 86 from => "SqlfXML",
87 to =>"MySQL",
88 filename => $testschema,
c957e92d 89);
5ff70f1a 90print $sql if DEBUG;
c957e92d 91#print "Debug:", Dumper($obj) if DEBUG;
92
93# Test the schema objs generted from the XML
94#
95my $scma = $obj->schema;
96my @tblnames = map {$_->name} $scma->get_tables;
97is_deeply( \@tblnames, [qw/Basic/], "tables");
98
99# Basic
100my $tbl = $scma->get_table("Basic");
101is $tbl->order, 1, "Basic->order";
b3530353 102is_deeply( [map {$_->name} $tbl->get_fields], [qw/
5952d39c 103 id title description email explicitnulldef explicitemptystring emptytagdef
b3530353 104/] , "Table Basic's fields");
c957e92d 105test_field($tbl->get_field("id"),{
5ff70f1a 106 name => "id",
107 order => 1,
108 data_type => "int",
109 default_value => undef,
110 is_nullable => 0,
111 size => 10,
112 is_primary_key => 1,
113 is_auto_increment => 1,
c957e92d 114});
115test_field($tbl->get_field("title"),{
5ff70f1a 116 name => "title",
117 order => 2,
118 data_type => "varchar",
119 is_nullable => 0,
120 default_value => "hello",
121 size => 100,
c957e92d 122});
123test_field($tbl->get_field("description"),{
5ff70f1a 124 name => "description",
125 order => 3,
126 data_type => "text",
127 is_nullable => 1,
128 default_value => "",
c957e92d 129});
130test_field($tbl->get_field("email"),{
5ff70f1a 131 name => "email",
132 order => 4,
133 data_type => "varchar",
134 size => 255,
135 is_unique => 1,
136 default_value => undef,
137 is_nullable => 1,
138});
139test_field($tbl->get_field("explicitnulldef"),{
140 name => "explicitnulldef",
141 order => 5,
142 data_type => "varchar",
143 default_value => undef,
144 is_nullable => 1,
145});
146test_field($tbl->get_field("explicitemptystring"),{
147 name => "explicitemptystring",
148 order => 6,
149 data_type => "varchar",
150 default_value => "",
151 is_nullable => 1,
c957e92d 152});
5952d39c 153test_field($tbl->get_field("emptytagdef"),{
154 name => "emptytagdef",
b3530353 155 order => 7,
156 data_type => "varchar",
157 default_value => "",
158 is_nullable => 1,
159});
c957e92d 160
161my @indices = $tbl->get_indices;
162is scalar(@indices), 1, "Table basic has 1 index";
163
164my @constraints = $tbl->get_constraints;
165is scalar(@constraints), 2, "Table basic has 2 constraints";
166my $con = shift @constraints;
167is $con->table, $tbl, "Constaints table right";
168is $con->name, "", "Constaints table right";
169is $con->type, PRIMARY_KEY, "Constaint is primary key";
170is_deeply [$con->fields], ["id"], "Constaint fields";
171$con = shift @constraints;
172is $con->table, $tbl, "Constaints table right";
173is $con->type, UNIQUE, "Constaint UNIQUE";
174is_deeply [$con->fields], ["email"], "Constaint fields";