Changed to use new "SqlfXML" producer.
[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
13use Test::More qw/no_plan/;
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 {
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 ( defined(my $ans = $test->{$attr}) ) {
50 if ( $attr =~ m/^is_/ ) {
51 ok $fld->$attr, " $name - $attr true";
52 }
53 else {
54 is $fld->$attr, $ans, " $name - $attr = '$ans'";
55 }
56 }
57 else {
58 ok !$fld->$attr, "$name - $attr not set";
59 }
60 }
61}
62
63# TODO test_constraint, test_index
64
65# Testing 1,2,3,4...
66#=============================================================================
67
68use SQL::Translator;
69use SQL::Translator::Schema::Constants;
70
71# Parse the test XML schema
72our $obj;
73$obj = SQL::Translator->new(
74 debug => DEBUG,
75 show_warnings => 1,
76 add_drop_table => 1,
77);
78my $testschema = "$Bin/data/xml/schema-basic.xml";
79die "Can't find test schema $testschema" unless -e $testschema;
80my $sql = $obj->translate(
81 from => "SqlfXML",
82 to =>"MySQL",
83 filename => $testschema,
84);
85print $sql;
86#print "Debug:", Dumper($obj) if DEBUG;
87
88# Test the schema objs generted from the XML
89#
90my $scma = $obj->schema;
91my @tblnames = map {$_->name} $scma->get_tables;
92is_deeply( \@tblnames, [qw/Basic/], "tables");
93
94# Basic
95my $tbl = $scma->get_table("Basic");
96is $tbl->order, 1, "Basic->order";
97is_deeply( [map {$_->name} $tbl->get_fields], [qw/id title description email/]
98 , "Table Basic's fields");
99test_field($tbl->get_field("id"),{
100 name => "id",
101 order => 1,
102 data_type => "int",
103 size => 10,
104 is_primary_key => 1,
105 is_auto_increment => 1,
106});
107test_field($tbl->get_field("title"),{
108 name => "title",
109 order => 2,
110 data_type => "varchar",
111 default_value => "hello",
112 size => 100,
113});
114test_field($tbl->get_field("description"),{
115 name => "description",
116 order => 3,
117 data_type => "text",
118 is_nullable => 1,
119});
120test_field($tbl->get_field("email"),{
121 name => "email",
122 order => 4,
123 data_type => "varchar",
124 size => 255,
125 is_unique => 1,
126});
127
128my @indices = $tbl->get_indices;
129is scalar(@indices), 1, "Table basic has 1 index";
130
131my @constraints = $tbl->get_constraints;
132is scalar(@constraints), 2, "Table basic has 2 constraints";
133my $con = shift @constraints;
134is $con->table, $tbl, "Constaints table right";
135is $con->name, "", "Constaints table right";
136is $con->type, PRIMARY_KEY, "Constaint is primary key";
137is_deeply [$con->fields], ["id"], "Constaint fields";
138$con = shift @constraints;
139is $con->table, $tbl, "Constaints table right";
140is $con->type, UNIQUE, "Constaint UNIQUE";
141is_deeply [$con->fields], ["email"], "Constaint fields";