Fixed default value bug in Parser::SqlfXML.
[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
5ff70f1a 13use Test::More tests => 78;
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
71use SQL::Translator;
72use SQL::Translator::Schema::Constants;
73
74# Parse the test XML schema
75our $obj;
76$obj = SQL::Translator->new(
5ff70f1a 77 debug => DEBUG,
78 show_warnings => 1,
79 add_drop_table => 1,
c957e92d 80);
81my $testschema = "$Bin/data/xml/schema-basic.xml";
82die "Can't find test schema $testschema" unless -e $testschema;
83my $sql = $obj->translate(
5ff70f1a 84 from => "SqlfXML",
85 to =>"MySQL",
86 filename => $testschema,
c957e92d 87);
5ff70f1a 88print $sql if DEBUG;
c957e92d 89#print "Debug:", Dumper($obj) if DEBUG;
90
91# Test the schema objs generted from the XML
92#
93my $scma = $obj->schema;
94my @tblnames = map {$_->name} $scma->get_tables;
95is_deeply( \@tblnames, [qw/Basic/], "tables");
96
97# Basic
98my $tbl = $scma->get_table("Basic");
99is $tbl->order, 1, "Basic->order";
5ff70f1a 100is_deeply( [map {$_->name} $tbl->get_fields],
101 [qw/id title description email explicitnulldef explicitemptystring/] ,
102 "Table Basic's fields");
c957e92d 103test_field($tbl->get_field("id"),{
5ff70f1a 104 name => "id",
105 order => 1,
106 data_type => "int",
107 default_value => undef,
108 is_nullable => 0,
109 size => 10,
110 is_primary_key => 1,
111 is_auto_increment => 1,
c957e92d 112});
113test_field($tbl->get_field("title"),{
5ff70f1a 114 name => "title",
115 order => 2,
116 data_type => "varchar",
117 is_nullable => 0,
118 default_value => "hello",
119 size => 100,
c957e92d 120});
121test_field($tbl->get_field("description"),{
5ff70f1a 122 name => "description",
123 order => 3,
124 data_type => "text",
125 is_nullable => 1,
126 default_value => "",
c957e92d 127});
128test_field($tbl->get_field("email"),{
5ff70f1a 129 name => "email",
130 order => 4,
131 data_type => "varchar",
132 size => 255,
133 is_unique => 1,
134 default_value => undef,
135 is_nullable => 1,
136});
137test_field($tbl->get_field("explicitnulldef"),{
138 name => "explicitnulldef",
139 order => 5,
140 data_type => "varchar",
141 default_value => undef,
142 is_nullable => 1,
143});
144test_field($tbl->get_field("explicitemptystring"),{
145 name => "explicitemptystring",
146 order => 6,
147 data_type => "varchar",
148 default_value => "",
149 is_nullable => 1,
c957e92d 150});
151
152my @indices = $tbl->get_indices;
153is scalar(@indices), 1, "Table basic has 1 index";
154
155my @constraints = $tbl->get_constraints;
156is scalar(@constraints), 2, "Table basic has 2 constraints";
157my $con = shift @constraints;
158is $con->table, $tbl, "Constaints table right";
159is $con->name, "", "Constaints table right";
160is $con->type, PRIMARY_KEY, "Constaint is primary key";
161is_deeply [$con->fields], ["id"], "Constaint fields";
162$con = shift @constraints;
163is $con->table, $tbl, "Constaints table right";
164is $con->type, UNIQUE, "Constaint UNIQUE";
165is_deeply [$con->fields], ["email"], "Constaint fields";