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