Playing around with new schema object.
[dbsrgits/SQL-Translator.git] / t / 06xsv.t
CommitLineData
046f18e5 1#!/usr/bin/perl
2# vim: set ft=perl:
9c62867a 3
046f18e5 4#
9c62867a 5# Tests for xSV parser
046f18e5 6#
046f18e5 7use strict;
8use SQL::Translator;
9use SQL::Translator::Parser::xSV qw(parse);
9c62867a 10use Test::More tests => 13;
046f18e5 11
12my $tr = SQL::Translator->new;
9c62867a 13my $data = q|One, Two, Three, Four, Five, Six, Seven
14I, Am, Some, Data, Yo, -10, .04
15And, 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 19my $val = parse($tr, $data);
20
21# $val holds the processed data structure.
22
23# The data structure should have one key:
37ac104a 24is(scalar keys %{$val}, 1, "One table...");
046f18e5 25
26# The data structure should have a single key, named sessions
37ac104a 27ok(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 31my $indices = $val->{'table1'}->{'indices'};
37ac104a 32is(scalar @{$indices}, 1, "correct index number");
046f18e5 33
37ac104a 34is($indices->[0]->{'type'}, 'primary_key', "correct index type");
35is($indices->[0]->{'fields'}->[0], 'One', "correct index name");
046f18e5 36
37# $val->{'table1'} should have two fields, id and a_sessionn
38my $fields = $val->{'table1'}->{'fields'};
9c62867a 39is(scalar keys %{$fields} => 7 => "7 fields in %fields");
046f18e5 40
37ac104a 41is($fields->{'One'}->{'data_type'}, 'char',
42 "\$fields->{'One'}->{'data_type'} == 'char'");
046f18e5 43
9c62867a 44my $size = join(',', @{ $fields->{'One'}{'size'} } );
45is( $size, 3, "\$fields->{'One'}->{'size'} == 3");
46
37ac104a 47is($fields->{'One'}->{'is_primary_key'} => 1,
48 "\$fields->{'One'}->{'is_primary_key'} == 1");
046f18e5 49
37ac104a 50ok(! defined $fields->{'Two'}->{'is_primary_key'},
51 "\$fields->{'Two'}->{'is_primary_key'} == 0");
046f18e5 52
9c62867a 53is($fields->{'Six'}->{'data_type'}, 'integer',
54 "\$fields->{'Six'}->{'data_type'} == 'integer'");
55
56is($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
61my @order = sort { $fields->{$a}->{'order'}
62 <=>
63 $fields->{$b}->{'order'}
64 } keys %{$fields};
37ac104a 65ok($order[0] eq 'One' && $order[4] eq 'Five', "Ordering OK");