bump Test::Pod dep in Optional::Dependencies too
[dbsrgits/DBIx-Class.git] / t / 99dbic_sqlt_parser.t
CommitLineData
0e2c6809 1use strict;
2use warnings;
206d1995 3
0e2c6809 4use Test::More;
8fd10683 5use Test::Exception;
0e2c6809 6use lib qw(t/lib);
7use DBICTest;
06e15b8e 8use DBICTest::Schema;
9use Scalar::Util ();
0e2c6809 10
11BEGIN {
2527233b 12 require DBIx::Class;
7f6f5b69 13 plan skip_all =>
2527233b 14 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy')
15 unless DBIx::Class::Optional::Dependencies->req_ok_for ('deploy')
0e2c6809 16}
17
06e15b8e 18# Test for SQLT-related leaks
19{
20 my $s = DBICTest::Schema->clone;
a7acf09f 21 create_schema ({ schema => $s });
06e15b8e 22 Scalar::Util::weaken ($s);
23
24 ok (!$s, 'Schema not leaked');
25}
26
27
0e2c6809 28my $schema = DBICTest->init_schema();
0fc7cd47 29# Dummy was yanked out by the sqlt hook test
8e8a8d91 30# CustomSql tests the horrific/deprecated ->name(\$sql) hack
e0cd97a4 31# YearXXXXCDs are views
8e8a8d91 32#
33my @sources = grep
d6c322f8 34 { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs ) $/x }
8e8a8d91 35 $schema->sources
36;
0fc7cd47 37
f0ac764e 38my $idx_exceptions = {
39 'Artwork' => -1,
40 'ForceForeign' => -1,
41 'LinerNotes' => -1,
827a808f 42 'TwoKeys' => -1, # TwoKeys has the index turned off on the rel def
f0ac764e 43};
44
c49ff507 45{
206d1995 46 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
0e2c6809 47
620df7e8 48 foreach my $source_name (@sources) {
49 my $table = get_table($sqlt_schema, $schema, $source_name);
0e2c6809 50
206d1995 51 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
f0ac764e 52 $fk_count += $idx_exceptions->{$source_name} || 0;
206d1995 53 my @indices = $table->get_indices;
620df7e8 54
206d1995 55 my $index_count = scalar(@indices);
620df7e8 56 is($index_count, $fk_count, "correct number of indices for $source_name with no args");
c49ff507 57
620df7e8 58 for my $index (@indices) {
59 my $source = $schema->source($source_name);
3eaae0f2 60 my $pk_test = join("\x00", $source->primary_columns);
61 my $idx_test = join("\x00", $index->fields);
c1092055 62 isnt ( $pk_test, $idx_test, "no additional index for the primary columns exists in $source_name");
620df7e8 63 }
206d1995 64 }
0e2c6809 65}
66
c49ff507 67{
206d1995 68 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
0e2c6809 69
f0ac764e 70 foreach my $source_name (@sources) {
71 my $table = get_table($sqlt_schema, $schema, $source_name);
0e2c6809 72
206d1995 73 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
f0ac764e 74 $fk_count += $idx_exceptions->{$source_name} || 0;
206d1995 75 my @indices = $table->get_indices;
76 my $index_count = scalar(@indices);
f0ac764e 77 is($index_count, $fk_count, "correct number of indices for $source_name with add_fk_index => 1");
206d1995 78 }
0e2c6809 79}
80
c49ff507 81{
206d1995 82 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
0e2c6809 83
206d1995 84 foreach my $source (@sources) {
85 my $table = get_table($sqlt_schema, $schema, $source);
0e2c6809 86
206d1995 87 my @indices = $table->get_indices;
88 my $index_count = scalar(@indices);
89 is($index_count, 0, "correct number of indices for $source with add_fk_index => 0");
90 }
0e2c6809 91}
92
c49ff507 93{
ab7e74aa 94 {
95 package # hide from PAUSE
96 DBICTest::Schema::NoViewDefinition;
97
98 use base qw/DBICTest::BaseResult/;
8f1617e2 99
ab7e74aa 100 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
101 __PACKAGE__->table('noviewdefinition');
8f1617e2 102
ab7e74aa 103 1;
8f1617e2 104 }
105
ab7e74aa 106 my $schema_invalid_view = $schema->clone;
107 $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition');
8f1617e2 108
8fd10683 109 throws_ok { create_schema({ schema => $schema_invalid_view }) }
110 qr/view noviewdefinition is missing a view_definition/,
111 'parser detects views with a view_definition';
8f1617e2 112}
113
a7f4b74c 114lives_ok (sub {
115 my $sqlt_schema = create_schema ({
116 schema => $schema,
117 args => {
118 parser_args => {
119 sources => ['CD']
120 },
121 },
122 });
123
124 is_deeply (
125 [$sqlt_schema->get_tables ],
126 ['cd'],
127 'sources limitng with relationships works',
128 );
129
130});
131
7f6f5b69 132done_testing;
133
0e2c6809 134sub create_schema {
206d1995 135 my $args = shift;
0e2c6809 136
206d1995 137 my $schema = $args->{schema};
138 my $additional_sqltargs = $args->{args} || {};
0e2c6809 139
206d1995 140 my $sqltargs = {
141 add_drop_table => 1,
142 ignore_constraint_names => 1,
143 ignore_index_names => 1,
144 %{$additional_sqltargs}
145 };
0e2c6809 146
206d1995 147 my $sqlt = SQL::Translator->new( $sqltargs );
0e2c6809 148
206d1995 149 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
150 return $sqlt->translate({ data => $schema }) || die $sqlt->error;
0e2c6809 151}
c2b7c5dc 152
153sub get_table {
154 my ($sqlt_schema, $schema, $source) = @_;
155
156 my $table_name = $schema->source($source)->from;
157 $table_name = $$table_name if ref $table_name;
158
159 return $sqlt_schema->get_table($table_name);
160}