test and pod fixes
[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 {
b2b2e7fd 12 require DBIx::Class::Storage::DBI;
7f6f5b69 13 plan skip_all =>
b2b2e7fd 14 'Test needs SQL::Translator ' . DBIx::Class::Storage::DBI->_sqlt_minimum_version
15 if not DBIx::Class::Storage::DBI->_sqlt_version_ok;
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,
42};
43
0e2c6809 44{
206d1995 45 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
0e2c6809 46
620df7e8 47 foreach my $source_name (@sources) {
48 my $table = get_table($sqlt_schema, $schema, $source_name);
0e2c6809 49
206d1995 50 my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
f0ac764e 51 $fk_count += $idx_exceptions->{$source_name} || 0;
206d1995 52 my @indices = $table->get_indices;
620df7e8 53
206d1995 54 my $index_count = scalar(@indices);
620df7e8 55 $index_count++ if ($source_name eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
56 is($index_count, $fk_count, "correct number of indices for $source_name with no args");
57
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
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 $index_count++ if ($source_name eq 'TwoKeys'); # TwoKeys has the index turned off on the rel def
78 is($index_count, $fk_count, "correct number of indices for $source_name with add_fk_index => 1");
206d1995 79 }
0e2c6809 80}
81
82{
206d1995 83 my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
0e2c6809 84
206d1995 85 foreach my $source (@sources) {
86 my $table = get_table($sqlt_schema, $schema, $source);
0e2c6809 87
206d1995 88 my @indices = $table->get_indices;
89 my $index_count = scalar(@indices);
90 is($index_count, 0, "correct number of indices for $source with add_fk_index => 0");
91 }
0e2c6809 92}
93
8f1617e2 94{
ab7e74aa 95 {
96 package # hide from PAUSE
97 DBICTest::Schema::NoViewDefinition;
98
99 use base qw/DBICTest::BaseResult/;
8f1617e2 100
ab7e74aa 101 __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
102 __PACKAGE__->table('noviewdefinition');
8f1617e2 103
ab7e74aa 104 1;
8f1617e2 105 }
106
ab7e74aa 107 my $schema_invalid_view = $schema->clone;
108 $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition');
8f1617e2 109
8fd10683 110 throws_ok { create_schema({ schema => $schema_invalid_view }) }
111 qr/view noviewdefinition is missing a view_definition/,
112 'parser detects views with a view_definition';
8f1617e2 113}
114
a7f4b74c 115lives_ok (sub {
116 my $sqlt_schema = create_schema ({
117 schema => $schema,
118 args => {
119 parser_args => {
120 sources => ['CD']
121 },
122 },
123 });
124
125 is_deeply (
126 [$sqlt_schema->get_tables ],
127 ['cd'],
128 'sources limitng with relationships works',
129 );
130
131});
132
7f6f5b69 133done_testing;
134
0e2c6809 135sub create_schema {
206d1995 136 my $args = shift;
0e2c6809 137
206d1995 138 my $schema = $args->{schema};
139 my $additional_sqltargs = $args->{args} || {};
0e2c6809 140
206d1995 141 my $sqltargs = {
142 add_drop_table => 1,
143 ignore_constraint_names => 1,
144 ignore_index_names => 1,
145 %{$additional_sqltargs}
146 };
0e2c6809 147
206d1995 148 my $sqlt = SQL::Translator->new( $sqltargs );
0e2c6809 149
206d1995 150 $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
151 return $sqlt->translate({ data => $schema }) || die $sqlt->error;
0e2c6809 152}
c2b7c5dc 153
154sub get_table {
155 my ($sqlt_schema, $schema, $source) = @_;
156
157 my $table_name = $schema->source($source)->from;
158 $table_name = $$table_name if ref $table_name;
159
160 return $sqlt_schema->get_table($table_name);
161}