Release commit for 1.62
[dbsrgits/SQL-Translator.git] / t / 06xsv.t
1 #!/usr/bin/perl
2 # vim: set ft=perl:
3
4 #
5 # Tests for xSV parser
6 #
7 use strict;
8 use SQL::Translator;
9 use SQL::Translator::Schema;
10 use SQL::Translator::Schema::Constants;
11 use Test::More;
12 use Test::SQL::Translator qw(maybe_plan);
13
14 BEGIN {
15     maybe_plan(25, 'SQL::Translator::Parser::xSV');
16     SQL::Translator::Parser::xSV->import('parse');
17 }
18
19 my $tr = SQL::Translator->new;
20 my $s  = SQL::Translator::Schema->new;
21 my $data = q|One, Two, Three, Four, Five, Six, Seven
22 I, Am, Some, Data, Yo, -10, .04
23 And, So, am, I, "you crazy, crazy bastard", 500982, 1.1
24 |;
25
26 $tr->parser_args( trim_fields => 1, scan_fields => 1 );
27 my $val = parse($tr, $data, $s);
28
29 my $schema = $tr->schema;
30 my @tables = $schema->get_tables;
31 is( scalar @tables, 1, 'Correct number of tables (1)' );
32
33 my $table = shift @tables;
34 is( $table->name, 'table1', 'Table is named "table1"' );
35
36 my @fields = $table->get_fields;
37 is( scalar @fields, 7, 'Correct number of fields (7)' );
38
39 my $f1 = $fields[0];
40 is( $f1->name, 'One', 'First field name is "One"' );
41 is( $f1->data_type, 'char', 'Data type is "char"' );
42 is( $f1->size, '3', 'Size is "3"' );
43 is( $f1->is_primary_key, 1, 'Field is PK' );
44
45 my $f2 = $fields[1];
46 is( $f2->name, 'Two', 'First field name is "Two"' );
47 is( $f2->data_type, 'char', 'Data type is "char"' );
48 is( $f2->size, '2', 'Size is "2"' );
49 is( $f2->is_primary_key, 0, 'Field is not PK' );
50
51 my $f5 = $fields[4];
52 is( $f5->name, 'Five', 'Fifth field name is "Five"' );
53 is( $f5->data_type, 'char', 'Data type is "char"' );
54 is( $f5->size, '26', 'Size is "26"' );
55 is( $f5->is_primary_key, 0, 'Field is not PK' );
56
57 my $f6 = $fields[5];
58 is( $f6->name, 'Six', 'Sixth field name is "Six"' );
59 is( $f6->data_type, 'integer', 'Data type is "integer"' );
60 is( $f6->size, '6', 'Size is "6"' );
61
62 my $f7 = $fields[6];
63 is( $f7->name, 'Seven', 'Seventh field name is "Seven"' );
64 is( $f7->data_type, 'float', 'Data type is "float"' );
65 is( $f7->size, '3,2', 'Size is "3,2"' );
66
67 my @indices = $table->get_indices;
68 is( scalar @indices, 0, 'Correct number of indices (0)' );
69
70 my @constraints = $table->get_constraints;
71 is( scalar @constraints, 1, 'Correct number of constraints (1)' );
72 my $c = shift @constraints;
73 is( $c->type, PRIMARY_KEY, 'Constraint is a PK' );
74 is( join(',', $c->fields), 'One', 'On field "One"' );