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' |
6 | |
7 | # |
8 | # basic.t |
9 | # ------- |
10 | # Tests that; |
11 | # |
12 | |
b3530353 |
13 | use Test::More; |
c957e92d |
14 | use Test::Exception; |
15 | |
16 | use strict; |
17 | use Data::Dumper; |
18 | our %opt; |
19 | BEGIN { map { $opt{$_}=1 if s/^-// } @ARGV; } |
20 | use constant DEBUG => (exists $opt{d} ? 1 : 0); |
21 | local $SIG{__WARN__} = sub { diag "[warn] ", @_; }; |
22 | |
23 | use FindBin qw/$Bin/; |
24 | |
25 | # Usefull test subs for the schema objs |
26 | #============================================================================= |
27 | |
28 | our %ATTRIBUTES; |
29 | $ATTRIBUTES{field} = [qw/ |
30 | name |
31 | order |
32 | data_type |
33 | default_value |
34 | size |
35 | is_primary_key |
36 | is_unique |
37 | is_nullable |
38 | is_foreign_key |
39 | is_auto_increment |
40 | /]; |
41 | |
42 | sub 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 |
71 | plan tests => 89; |
72 | |
c957e92d |
73 | use SQL::Translator; |
74 | use SQL::Translator::Schema::Constants; |
75 | |
76 | # Parse the test XML schema |
77 | our $obj; |
78 | $obj = SQL::Translator->new( |
5ff70f1a |
79 | debug => DEBUG, |
80 | show_warnings => 1, |
81 | add_drop_table => 1, |
c957e92d |
82 | ); |
83 | my $testschema = "$Bin/data/xml/schema-basic.xml"; |
84 | die "Can't find test schema $testschema" unless -e $testschema; |
85 | my $sql = $obj->translate( |
5ff70f1a |
86 | from => "SqlfXML", |
87 | to =>"MySQL", |
88 | filename => $testschema, |
c957e92d |
89 | ); |
5ff70f1a |
90 | print $sql if DEBUG; |
c957e92d |
91 | #print "Debug:", Dumper($obj) if DEBUG; |
92 | |
93 | # Test the schema objs generted from the XML |
94 | # |
95 | my $scma = $obj->schema; |
96 | my @tblnames = map {$_->name} $scma->get_tables; |
97 | is_deeply( \@tblnames, [qw/Basic/], "tables"); |
98 | |
99 | # Basic |
100 | my $tbl = $scma->get_table("Basic"); |
101 | is $tbl->order, 1, "Basic->order"; |
b3530353 |
102 | is_deeply( [map {$_->name} $tbl->get_fields], [qw/ |
5952d39c |
103 | id title description email explicitnulldef explicitemptystring emptytagdef |
b3530353 |
104 | /] , "Table Basic's fields"); |
c957e92d |
105 | test_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 | }); |
115 | test_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 | }); |
123 | test_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 | }); |
130 | test_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 | }); |
139 | test_field($tbl->get_field("explicitnulldef"),{ |
140 | name => "explicitnulldef", |
141 | order => 5, |
142 | data_type => "varchar", |
143 | default_value => undef, |
144 | is_nullable => 1, |
145 | }); |
146 | test_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 |
153 | test_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 | |
161 | my @indices = $tbl->get_indices; |
162 | is scalar(@indices), 1, "Table basic has 1 index"; |
163 | |
164 | my @constraints = $tbl->get_constraints; |
165 | is scalar(@constraints), 2, "Table basic has 2 constraints"; |
166 | my $con = shift @constraints; |
167 | is $con->table, $tbl, "Constaints table right"; |
168 | is $con->name, "", "Constaints table right"; |
169 | is $con->type, PRIMARY_KEY, "Constaint is primary key"; |
170 | is_deeply [$con->fields], ["id"], "Constaint fields"; |
171 | $con = shift @constraints; |
172 | is $con->table, $tbl, "Constaints table right"; |
173 | is $con->type, UNIQUE, "Constaint UNIQUE"; |
174 | is_deeply [$con->fields], ["email"], "Constaint fields"; |