Commit | Line | Data |
046f18e5 |
1 | #!/usr/bin/perl |
2 | # vim: set ft=perl: |
9c62867a |
3 | |
046f18e5 |
4 | # |
9c62867a |
5 | # Tests for xSV parser |
046f18e5 |
6 | # |
046f18e5 |
7 | use strict; |
8 | use SQL::Translator; |
6b73ef23 |
9 | use SQL::Translator::Schema; |
046f18e5 |
10 | use SQL::Translator::Parser::xSV qw(parse); |
9c62867a |
11 | use Test::More tests => 13; |
046f18e5 |
12 | |
13 | my $tr = SQL::Translator->new; |
6b73ef23 |
14 | my $s = SQL::Translator::Schema->new; |
9c62867a |
15 | my $data = q|One, Two, Three, Four, Five, Six, Seven |
16 | I, Am, Some, Data, Yo, -10, .04 |
17 | And, So, am, I, "you crazy, crazy bastard", 500982, 1.1 |
18 | |; |
046f18e5 |
19 | |
9c62867a |
20 | $tr->parser_args( trim_fields => 1, scan_fields => 1 ); |
6b73ef23 |
21 | my $val = parse($tr, $data, $s); |
046f18e5 |
22 | |
23 | # $val holds the processed data structure. |
24 | |
25 | # The data structure should have one key: |
37ac104a |
26 | is(scalar keys %{$val}, 1, "One table..."); |
046f18e5 |
27 | |
28 | # The data structure should have a single key, named sessions |
37ac104a |
29 | ok(defined $val->{'table1'} => "...named 'table1'"); |
046f18e5 |
30 | |
31 | # $val->{'table1'} should have a single index (since we haven't |
32 | # defined an index, but have defined a primary key) |
44fcd0b5 |
33 | my $indices = $val->{'table1'}->{'indices'}; |
37ac104a |
34 | is(scalar @{$indices}, 1, "correct index number"); |
046f18e5 |
35 | |
37ac104a |
36 | is($indices->[0]->{'type'}, 'primary_key', "correct index type"); |
37 | is($indices->[0]->{'fields'}->[0], 'One', "correct index name"); |
046f18e5 |
38 | |
39 | # $val->{'table1'} should have two fields, id and a_sessionn |
40 | my $fields = $val->{'table1'}->{'fields'}; |
9c62867a |
41 | is(scalar keys %{$fields} => 7 => "7 fields in %fields"); |
046f18e5 |
42 | |
37ac104a |
43 | is($fields->{'One'}->{'data_type'}, 'char', |
44 | "\$fields->{'One'}->{'data_type'} == 'char'"); |
046f18e5 |
45 | |
9c62867a |
46 | my $size = join(',', @{ $fields->{'One'}{'size'} } ); |
47 | is( $size, 3, "\$fields->{'One'}->{'size'} == 3"); |
48 | |
37ac104a |
49 | is($fields->{'One'}->{'is_primary_key'} => 1, |
50 | "\$fields->{'One'}->{'is_primary_key'} == 1"); |
046f18e5 |
51 | |
37ac104a |
52 | ok(! defined $fields->{'Two'}->{'is_primary_key'}, |
53 | "\$fields->{'Two'}->{'is_primary_key'} == 0"); |
046f18e5 |
54 | |
9c62867a |
55 | is($fields->{'Six'}->{'data_type'}, 'integer', |
56 | "\$fields->{'Six'}->{'data_type'} == 'integer'"); |
57 | |
58 | is($fields->{'Seven'}->{'data_type'}, 'float', |
59 | "\$fields->{'Seven'}->{'data_type'} == 'float'"); |
60 | |
046f18e5 |
61 | # Test that the order is being maintained by the internal order |
62 | # data element |
63 | my @order = sort { $fields->{$a}->{'order'} |
64 | <=> |
65 | $fields->{$b}->{'order'} |
66 | } keys %{$fields}; |
37ac104a |
67 | ok($order[0] eq 'One' && $order[4] eq 'Five', "Ordering OK"); |