5bbd30254d0c6849e051d349b83dd477e239e7c1
[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 # CustomSql tests the horrific/deprecated ->name(\$sql) hack
19 # YearXXXXCDs are views
20 #
21 my @sources = grep
22   { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs ) $/x }
23   $schema->sources
24 ;
25
26 plan tests => ( @sources * 3);
27
28
29         my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
30
31         foreach my $source (@sources) {
32                 my $table = get_table($sqlt_schema, $schema, $source);
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);
37     $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
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
45         foreach my $source (@sources) {
46                 my $table = get_table($sqlt_schema, $schema, $source);
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);
51     $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
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
59         foreach my $source (@sources) {
60                 my $table = get_table($sqlt_schema, $schema, $source);
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 }
86
87 sub get_table {
88     my ($sqlt_schema, $schema, $source) = @_;
89
90     my $table_name = $schema->source($source)->from;
91     $table_name    = $$table_name if ref $table_name;
92
93     return $sqlt_schema->get_table($table_name);
94 }