throw_exception if view_definition is missing instead of silent skipping + test changes
[dbsrgits/DBIx-Class-Historic.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
8BEGIN {
b2b2e7fd 9 require DBIx::Class::Storage::DBI;
7f6f5b69 10 plan skip_all =>
b2b2e7fd 11 'Test needs SQL::Translator ' . DBIx::Class::Storage::DBI->_sqlt_minimum_version
12 if not DBIx::Class::Storage::DBI->_sqlt_version_ok;
0e2c6809 13}
14
15my $schema = DBICTest->init_schema();
0fc7cd47 16# Dummy was yanked out by the sqlt hook test
8e8a8d91 17# CustomSql tests the horrific/deprecated ->name(\$sql) hack
8f1617e2 18# YearXXXXCDs and NoViewDefinition are views
8e8a8d91 19#
20my @sources = grep
8f1617e2 21 { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs | NoViewDefinition ) $/x }
8e8a8d91 22 $schema->sources
23;
0fc7cd47 24
0e2c6809 25{
26 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
27
181c0934 28 foreach my $source (@sources) {
c2b7c5dc 29 my $table = get_table($sqlt_schema, $schema, $source);
0e2c6809 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);
2581038c 34 $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
0e2c6809 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
181c0934 42 foreach my $source (@sources) {
c2b7c5dc 43 my $table = get_table($sqlt_schema, $schema, $source);
0e2c6809 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);
2581038c 48 $index_count++ if ($source eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
0e2c6809 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
181c0934 56 foreach my $source (@sources) {
c2b7c5dc 57 my $table = get_table($sqlt_schema, $schema, $source);
0e2c6809 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
8f1617e2 65{
ab7e74aa 66 {
67 package # hide from PAUSE
68 DBICTest::Schema::NoViewDefinition;
69
70 use base qw/DBICTest::BaseResult/;
8f1617e2 71
ab7e74aa 72 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
73 __PACKAGE__->table('noviewdefinition');
8f1617e2 74
ab7e74aa 75 1;
8f1617e2 76 }
77
ab7e74aa 78 my $schema_invalid_view = $schema->clone;
79 $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition');
8f1617e2 80
ab7e74aa 81 eval {
82 my $sqlt_schema = create_schema({ schema => $schema_invalid_view });
83 };
84 like($@, qr/view noviewdefinition is missing a view_definition/, "parser detects views with a view_definition");
85
86# my @views = $sqlt_schema->get_views;
87#
88# # the following views are skipped:
89# # Year1999CDs is virtual
90# # NoViewDefinition has no view_definition
91# is(scalar @views, 1, "number of views ok");
92#
93# foreach my $view (@views) {
94# ok($view->is_valid, "view " . $view->name . " is valid");
95# }
96#
97# my @expected_view_names = (qw/ year2000cds /);
98# my @view_names = sort map { $_->name } @views;
99#
100# is_deeply( @view_names, @expected_view_names, "all expected views included int SQL::Translator schema" );
8f1617e2 101}
102
7f6f5b69 103done_testing;
104
0e2c6809 105sub create_schema {
106 my $args = shift;
107
108 my $schema = $args->{schema};
109 my $additional_sqltargs = $args->{args} || {};
110
111 my $sqltargs = {
112 add_drop_table => 1,
113 ignore_constraint_names => 1,
114 ignore_index_names => 1,
115 %{$additional_sqltargs}
116 };
117
118 my $sqlt = SQL::Translator->new( $sqltargs );
119
120 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
ab7e74aa 121 return $sqlt->translate({ data => $schema }) || die $sqlt->error;
0e2c6809 122}
c2b7c5dc 123
124sub get_table {
125 my ($sqlt_schema, $schema, $source) = @_;
126
127 my $table_name = $schema->source($source)->from;
128 $table_name = $$table_name if ref $table_name;
129
130 return $sqlt_schema->get_table($table_name);
131}