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