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