Views without a view_definition won't be added to the SQL::Translator::Schema by...
[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 BEGIN {
9   require DBIx::Class::Storage::DBI;
10   plan skip_all =>
11       'Test needs SQL::Translator ' . DBIx::Class::Storage::DBI->_sqlt_minimum_version
12     if not DBIx::Class::Storage::DBI->_sqlt_version_ok;
13 }
14
15 my $schema = DBICTest->init_schema();
16 # Dummy was yanked out by the sqlt hook test
17 # CustomSql tests the horrific/deprecated ->name(\$sql) hack
18 # YearXXXXCDs and NoViewDefinition are views
19 #
20 my @sources = grep
21   { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs | NoViewDefinition ) $/x }
22   $schema->sources
23 ;
24
25
26         my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
27
28         foreach my $source (@sources) {
29                 my $table = get_table($sqlt_schema, $schema, $source);
30
31                 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
32                 my @indices = $table->get_indices;
33                 my $index_count = scalar(@indices);
34     $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
35                 is($index_count, $fk_count, "correct number of indices for $source with no args");
36         }
37 }
38
39
40         my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
41
42         foreach my $source (@sources) {
43                 my $table = get_table($sqlt_schema, $schema, $source);
44
45                 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
46                 my @indices = $table->get_indices;
47                 my $index_count = scalar(@indices);
48     $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
49                 is($index_count, $fk_count, "correct number of indices for $source with add_fk_index => 1");
50         }
51 }
52
53
54         my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
55
56         foreach my $source (@sources) {
57                 my $table = get_table($sqlt_schema, $schema, $source);
58
59                 my @indices = $table->get_indices;
60                 my $index_count = scalar(@indices);
61                 is($index_count, 0, "correct number of indices for $source with add_fk_index => 0");
62         }
63 }
64
65
66     #my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
67         my $sqlt_schema = create_schema({ schema => $schema });
68     
69     my @views = $sqlt_schema->get_views;
70
71     # the following views are skipped:
72     # Year1999CDs is virtual
73     # NoViewDefinition has no view_definition
74     is(scalar @views, 1, "number of views ok");
75
76     foreach my $view (@views) {
77         ok($view->is_valid, "view " . $view->name . " is valid");
78     }
79
80     my @expected_view_names = (qw/ year2000cds /);
81     my @view_names = sort map { $_->name } @views;
82
83     is_deeply( @view_names, @expected_view_names, "all expected views included int SQL::Translator schema" );
84     
85     #use Data::Dumper::Concise;
86     #warn Dumper(@view_names);
87     #is($view->error, undef, 'view with a view_definition is skipped.');
88 }
89
90 done_testing;
91
92 sub create_schema {
93         my $args = shift;
94
95         my $schema = $args->{schema};
96         my $additional_sqltargs = $args->{args} || {};
97
98         my $sqltargs = {
99                 add_drop_table => 1, 
100                 ignore_constraint_names => 1,
101                 ignore_index_names => 1,
102                 %{$additional_sqltargs}
103                 };
104
105         my $sqlt = SQL::Translator->new( $sqltargs );
106
107         $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
108         return $sqlt->translate({ data => $schema }) or die $sqlt->error;
109 }
110
111 sub get_table {
112     my ($sqlt_schema, $schema, $source) = @_;
113
114     my $table_name = $schema->source($source)->from;
115     $table_name    = $$table_name if ref $table_name;
116
117     return $sqlt_schema->get_table($table_name);
118 }