dclone pod take #2
[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 Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8
9 BEGIN {
10   require DBIx::Class::Storage::DBI;
11   plan skip_all =>
12       'Test needs SQL::Translator ' . DBIx::Class::Storage::DBI->_sqlt_minimum_version
13     if not DBIx::Class::Storage::DBI->_sqlt_version_ok;
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
27         my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
28
29         foreach my $source (@sources) {
30                 my $table = get_table($sqlt_schema, $schema, $source);
31
32                 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
33                 my @indices = $table->get_indices;
34                 my $index_count = scalar(@indices);
35     $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
36                 is($index_count, $fk_count, "correct number of indices for $source with no args");
37         }
38 }
39
40
41         my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
42
43         foreach my $source (@sources) {
44                 my $table = get_table($sqlt_schema, $schema, $source);
45
46                 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
47                 my @indices = $table->get_indices;
48                 my $index_count = scalar(@indices);
49     $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
50                 is($index_count, $fk_count, "correct number of indices for $source with add_fk_index => 1");
51         }
52 }
53
54
55         my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
56
57         foreach my $source (@sources) {
58                 my $table = get_table($sqlt_schema, $schema, $source);
59
60                 my @indices = $table->get_indices;
61                 my $index_count = scalar(@indices);
62                 is($index_count, 0, "correct number of indices for $source with add_fk_index => 0");
63         }
64 }
65
66
67     {
68         package # hide from PAUSE
69             DBICTest::Schema::NoViewDefinition;
70
71         use base qw/DBICTest::BaseResult/;
72
73         __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
74         __PACKAGE__->table('noviewdefinition');
75
76         1;
77     }
78
79     my $schema_invalid_view = $schema->clone;
80     $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition');
81
82     throws_ok { create_schema({ schema => $schema_invalid_view }) }
83         qr/view noviewdefinition is missing a view_definition/,
84         'parser detects views with a view_definition';
85 }
86
87 done_testing;
88
89 sub create_schema {
90         my $args = shift;
91
92         my $schema = $args->{schema};
93         my $additional_sqltargs = $args->{args} || {};
94
95         my $sqltargs = {
96                 add_drop_table => 1, 
97                 ignore_constraint_names => 1,
98                 ignore_index_names => 1,
99                 %{$additional_sqltargs}
100                 };
101
102         my $sqlt = SQL::Translator->new( $sqltargs );
103
104         $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
105         return $sqlt->translate({ data => $schema }) || die $sqlt->error;
106 }
107
108 sub get_table {
109     my ($sqlt_schema, $schema, $source) = @_;
110
111     my $table_name = $schema->source($source)->from;
112     $table_name    = $$table_name if ref $table_name;
113
114     return $sqlt_schema->get_table($table_name);
115 }