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