fix related resultsets and multi-create
[dbsrgits/DBIx-Class.git] / t / 99dbic_sqlt_parser.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 BEGIN {
9     eval "use DBD::mysql; use SQL::Translator 0.09;";
10     plan $@
11         ? ( skip_all => 'needs SQL::Translator 0.09 for testing' )
12         : ( tests => 102 );
13 }
14
15 my $schema = DBICTest->init_schema();
16
17
18         my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
19
20         foreach my $source ($schema->sources) {
21                 my $table = $sqlt_schema->get_table($schema->source($source)->from);
22
23                 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
24                 my @indices = $table->get_indices;
25                 my $index_count = scalar(@indices);
26     $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
27                 is($index_count, $fk_count, "correct number of indices for $source with no args");
28         }
29 }
30
31
32         my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
33
34         foreach my $source ($schema->sources) {
35                 my $table = $sqlt_schema->get_table($schema->source($source)->from);
36
37                 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
38                 my @indices = $table->get_indices;
39                 my $index_count = scalar(@indices);
40     $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
41                 is($index_count, $fk_count, "correct number of indices for $source with add_fk_index => 1");
42         }
43 }
44
45
46         my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
47
48         foreach my $source ($schema->sources) {
49                 my $table = $sqlt_schema->get_table($schema->source($source)->from);
50
51                 my @indices = $table->get_indices;
52                 my $index_count = scalar(@indices);
53                 is($index_count, 0, "correct number of indices for $source with add_fk_index => 0");
54         }
55 }
56
57 sub create_schema {
58         my $args = shift;
59
60         my $schema = $args->{schema};
61         my $additional_sqltargs = $args->{args} || {};
62
63         my $sqltargs = {
64                 add_drop_table => 1, 
65                 ignore_constraint_names => 1,
66                 ignore_index_names => 1,
67                 %{$additional_sqltargs}
68                 };
69
70         my $sqlt = SQL::Translator->new( $sqltargs );
71
72         $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
73         return $sqlt->translate({ data => $schema }) or die $sqlt->error;
74 }