add constraints after all tables are added
[dbsrgits/SQL-Translator-2.0-ish.git] / t / 07p_args.t
CommitLineData
d4c630fa 1use SQL::Translator;
2use Test::More;
3
4sub silly_parser {
5 my ($tr, $data) = @_;
6 my $pargs = $tr->parser_args;
7
8 my @fields = split /$pargs->{'delimiter'}/, $data;
9
10 my $schema = $tr->schema;
11 my $table = $schema->add_table( name => 'foo') or die $schema->error;
12 for my $value ( @fields ) {
13 my $field = $table->add_field( name => $value ) or die $table->error;
14 }
15
16 return 1;
17}
18
19# The "data" to be parsed
20my $data = q(Id|Name|Phone Number|Favorite Flavor|);
21
22my $tr = SQL::Translator->new;
23
24# Pass parser_args as an explicit method call
25$tr->parser(\&silly_parser);
26$tr->parser_args(delimiter => '\|');
27
28my $pargs = $tr->parser_args;
29$tr->translate(\$data);
30my $schema = $tr->schema;
31
32is($pargs->{'delimiter'}, '\|', "parser_args works when called directly");
33my @tables = $schema->get_tables;
34is(scalar @tables, 1, "right number of tables");
35my $table = shift @tables;
36my @fields = $table->get_fields;
37is(scalar @fields, 4, "right number of fields");
38
39#
40# Blow away the existing schema object.
41#
42$tr->schema (undef);
43
44# Now, pass parser_args indirectly...
45$tr->parser(\&silly_parser, { delimiter => "\t" });
46$data =~ s/\|/\t/g;
47
48$pargs = $tr->parser_args;
49$tr->translate(\$data);
50
51is($pargs->{'delimiter'}, "\t",
52 "parser_args works when called indirectly");
53
54@tables = $schema->get_tables;
55is(scalar @tables, 1, "right number of tables");
56$table = shift @tables;
57@fields = $table->get_fields;
58is(scalar @fields, 4, "right number of fields");
59
60undef $tr;
61$tr = SQL::Translator->new(parser => \&silly_parser,
62 parser_args => { delimiter => ":" });
63$data =~ s/\t/:/g;
64$pargs = $tr->parser_args;
65$tr->translate(\$data);
66
67is($pargs->{'delimiter'}, ":",
68 "parser_args works when called as constructor arg");
69
70@tables = $schema->get_tables;
71is(scalar @tables, 1, "right number of tables");
72$table = shift @tables;
73@fields = $table->get_fields;
74is(scalar @fields, 4, "right number of fields with new delimiter");
75
76done_testing;