Commit | Line | Data |
0e2c6809 |
1 | #!/usr/bin/perl |
2 | use strict; |
3 | use warnings; |
4 | use Test::More; |
5 | use lib qw(t/lib); |
6 | use DBICTest; |
7 | |
1dac6833 |
8 | |
0e2c6809 |
9 | BEGIN { |
1b840063 |
10 | eval "use SQL::Translator 0.09003;"; |
1dac6833 |
11 | if ($@) { |
1b840063 |
12 | plan skip_all => 'needs SQL::Translator 0.09003 for testing'; |
1dac6833 |
13 | } |
0e2c6809 |
14 | } |
15 | |
16 | my $schema = DBICTest->init_schema(); |
0fc7cd47 |
17 | # Dummy was yanked out by the sqlt hook test |
8e8a8d91 |
18 | # CustomSql tests the horrific/deprecated ->name(\$sql) hack |
0fc7cd47 |
19 | # YearXXXXCDs are views |
8e8a8d91 |
20 | # |
21 | my @sources = grep |
22 | { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs ) $/x } |
23 | $schema->sources |
24 | ; |
0fc7cd47 |
25 | |
181c0934 |
26 | plan tests => ( @sources * 3); |
0e2c6809 |
27 | |
28 | { |
29 | my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } }); |
30 | |
181c0934 |
31 | foreach my $source (@sources) { |
0e2c6809 |
32 | my $table = $sqlt_schema->get_table($schema->source($source)->from); |
33 | |
34 | my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints); |
35 | my @indices = $table->get_indices; |
36 | my $index_count = scalar(@indices); |
2581038c |
37 | $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def |
0e2c6809 |
38 | is($index_count, $fk_count, "correct number of indices for $source with no args"); |
39 | } |
40 | } |
41 | |
42 | { |
43 | my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } }); |
44 | |
181c0934 |
45 | foreach my $source (@sources) { |
0e2c6809 |
46 | my $table = $sqlt_schema->get_table($schema->source($source)->from); |
47 | |
48 | my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints); |
49 | my @indices = $table->get_indices; |
50 | my $index_count = scalar(@indices); |
2581038c |
51 | $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def |
0e2c6809 |
52 | is($index_count, $fk_count, "correct number of indices for $source with add_fk_index => 1"); |
53 | } |
54 | } |
55 | |
56 | { |
57 | my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } }); |
58 | |
181c0934 |
59 | foreach my $source (@sources) { |
0e2c6809 |
60 | my $table = $sqlt_schema->get_table($schema->source($source)->from); |
61 | |
62 | my @indices = $table->get_indices; |
63 | my $index_count = scalar(@indices); |
64 | is($index_count, 0, "correct number of indices for $source with add_fk_index => 0"); |
65 | } |
66 | } |
67 | |
68 | sub create_schema { |
69 | my $args = shift; |
70 | |
71 | my $schema = $args->{schema}; |
72 | my $additional_sqltargs = $args->{args} || {}; |
73 | |
74 | my $sqltargs = { |
75 | add_drop_table => 1, |
76 | ignore_constraint_names => 1, |
77 | ignore_index_names => 1, |
78 | %{$additional_sqltargs} |
79 | }; |
80 | |
81 | my $sqlt = SQL::Translator->new( $sqltargs ); |
82 | |
83 | $sqlt->parser('SQL::Translator::Parser::DBIx::Class'); |
84 | return $sqlt->translate({ data => $schema }) or die $sqlt->error; |
85 | } |