Streamline pg test-schemas cleanup
[dbsrgits/DBIx-Class.git] / t / 99dbic_sqlt_parser.t
CommitLineData
0e2c6809 1#!/usr/bin/perl
2use strict;
3use warnings;
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
1dac6833 8
0e2c6809 9BEGIN {
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
16my $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#
21my @sources = grep
22 { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs ) $/x }
23 $schema->sources
24;
0fc7cd47 25
181c0934 26plan tests => ( @sources * 3);
0e2c6809 27
28{
29 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
30
181c0934 31 foreach my $source (@sources) {
c2b7c5dc 32 my $table = get_table($sqlt_schema, $schema, $source);
0e2c6809 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) {
c2b7c5dc 46 my $table = get_table($sqlt_schema, $schema, $source);
0e2c6809 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) {
c2b7c5dc 60 my $table = get_table($sqlt_schema, $schema, $source);
0e2c6809 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
68sub 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}
c2b7c5dc 86
87sub 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}