test and pod fixes
[dbsrgits/DBIx-Class.git] / t / 99dbic_sqlt_parser.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8 use DBICTest::Schema;
9 use Scalar::Util ();
10
11 BEGIN {
12   require DBIx::Class::Storage::DBI;
13   plan skip_all =>
14       'Test needs SQL::Translator ' . DBIx::Class::Storage::DBI->_sqlt_minimum_version
15     if not DBIx::Class::Storage::DBI->_sqlt_version_ok;
16 }
17
18 # Test for SQLT-related leaks
19 {
20   my $s = DBICTest::Schema->clone;
21   create_schema ({ schema => $s });
22   Scalar::Util::weaken ($s);
23
24   ok (!$s, 'Schema not leaked');
25 }
26
27
28 my $schema = DBICTest->init_schema();
29 # Dummy was yanked out by the sqlt hook test
30 # CustomSql tests the horrific/deprecated ->name(\$sql) hack
31 # YearXXXXCDs are views
32 #
33 my @sources = grep
34   { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs ) $/x }
35   $schema->sources
36 ;
37
38 my $idx_exceptions = {
39     'Artwork'       => -1,
40     'ForceForeign'  => -1,
41     'LinerNotes'    => -1,
42 };
43
44
45   my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
46
47   foreach my $source_name (@sources) {
48     my $table = get_table($sqlt_schema, $schema, $source_name);
49
50     my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
51     $fk_count += $idx_exceptions->{$source_name} || 0;
52     my @indices = $table->get_indices;
53
54     my $index_count = scalar(@indices);
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);
60         my $pk_test = join("\x00", $source->primary_columns);
61         my $idx_test = join("\x00", $index->fields);
62         isnt ( $pk_test, $idx_test, "no additional index for the primary columns exists in $source_name");
63     }
64   }
65 }
66
67
68   my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
69
70   foreach my $source_name (@sources) {
71     my $table = get_table($sqlt_schema, $schema, $source_name);
72
73     my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
74     $fk_count += $idx_exceptions->{$source_name} || 0;
75     my @indices = $table->get_indices;
76     my $index_count = scalar(@indices);
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");
79   }
80 }
81
82
83   my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
84
85   foreach my $source (@sources) {
86     my $table = get_table($sqlt_schema, $schema, $source);
87
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   }
92 }
93
94
95     {
96         package # hide from PAUSE
97             DBICTest::Schema::NoViewDefinition;
98
99         use base qw/DBICTest::BaseResult/;
100
101         __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
102         __PACKAGE__->table('noviewdefinition');
103
104         1;
105     }
106
107     my $schema_invalid_view = $schema->clone;
108     $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition');
109
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';
113 }
114
115 lives_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
133 done_testing;
134
135 sub create_schema {
136   my $args = shift;
137
138   my $schema = $args->{schema};
139   my $additional_sqltargs = $args->{args} || {};
140
141   my $sqltargs = {
142     add_drop_table => 1, 
143     ignore_constraint_names => 1,
144     ignore_index_names => 1,
145     %{$additional_sqltargs}
146   };
147
148   my $sqlt = SQL::Translator->new( $sqltargs );
149
150   $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
151   return $sqlt->translate({ data => $schema }) || die $sqlt->error;
152 }
153
154 sub 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 }