Revert workarounds for $@ broken during 5.13.x - mainly 1f870d5a
[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 Scalar::Util ();
7
8 use lib qw(t/lib);
9 use DBICTest;
10
11 BEGIN {
12   require DBIx::Class;
13   plan skip_all =>
14       'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy')
15     unless DBIx::Class::Optional::Dependencies->req_ok_for ('deploy')
16 }
17
18 # Test for SQLT-related leaks
19 {
20   my $s = DBICTest::Schema->clone;
21
22   my @schemas = (
23     create_schema ({ schema => $s }),
24     create_schema ({ args => { parser_args => { 'DBIx::Class::Schema' => $s } } }),
25     create_schema ({ args => { parser_args => { 'DBIx::Schema' => $s } } }),
26     create_schema ({ args => { parser_args => { package => $s } } }),
27   );
28
29   Scalar::Util::weaken ($s);
30
31   ok (!$s, 'Schema not leaked');
32
33   isa_ok ($_, 'SQL::Translator::Schema', "SQLT schema object $_ produced")
34     for @schemas;
35 }
36
37 # make sure classname-style works
38 lives_ok { isa_ok (create_schema ({ schema => 'DBICTest::Schema' }), 'SQL::Translator::Schema', 'SQLT schema object produced') };
39
40
41 my $schema = DBICTest->init_schema( no_deploy => 1 );
42
43 # Dummy was yanked out by the sqlt hook test
44 # CustomSql tests the horrific/deprecated ->name(\$sql) hack
45 # YearXXXXCDs are views
46 #
47 my @sources = grep
48   { $_ !~ /^ (?: Dummy | CustomSql | Year\d{4}CDs ) $/x }
49   $schema->sources
50 ;
51
52 my $idx_exceptions = {
53     'Artwork'       => -1,
54     'ForceForeign'  => -1,
55     'LinerNotes'    => -1,
56     'TwoKeys'       => -1, # TwoKeys has the index turned off on the rel def
57 };
58
59 {
60   my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { } } });
61
62   foreach my $source_name (@sources) {
63     my $table = get_table($sqlt_schema, $schema, $source_name);
64
65     my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
66     $fk_count += $idx_exceptions->{$source_name} || 0;
67     my @indices = $table->get_indices;
68
69     my $index_count = scalar(@indices);
70     is($index_count, $fk_count, "correct number of indices for $source_name with no args");
71
72     for my $index (@indices) {
73         my $source = $schema->source($source_name);
74         my $pk_test = join("\x00", $source->primary_columns);
75         my $idx_test = join("\x00", $index->fields);
76         isnt ( $pk_test, $idx_test, "no additional index for the primary columns exists in $source_name");
77     }
78   }
79 }
80
81 {
82   my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 1 } } });
83
84   foreach my $source_name (@sources) {
85     my $table = get_table($sqlt_schema, $schema, $source_name);
86
87     my $fk_count = scalar(grep { $_->type eq 'FOREIGN KEY' } $table->get_constraints);
88     $fk_count += $idx_exceptions->{$source_name} || 0;
89     my @indices = $table->get_indices;
90     my $index_count = scalar(@indices);
91     is($index_count, $fk_count, "correct number of indices for $source_name with add_fk_index => 1");
92   }
93 }
94
95 {
96   my $sqlt_schema = create_schema({ schema => $schema, args => { parser_args => { add_fk_index => 0 } } });
97
98   foreach my $source (@sources) {
99     my $table = get_table($sqlt_schema, $schema, $source);
100
101     my @indices = $table->get_indices;
102     my $index_count = scalar(@indices);
103     is($index_count, 0, "correct number of indices for $source with add_fk_index => 0");
104   }
105 }
106
107 {
108     {
109         package # hide from PAUSE
110             DBICTest::Schema::NoViewDefinition;
111
112         use base qw/DBICTest::BaseResult/;
113
114         __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
115         __PACKAGE__->table('noviewdefinition');
116
117         1;
118     }
119
120     my $schema_invalid_view = $schema->clone;
121     $schema_invalid_view->register_class('NoViewDefinition', 'DBICTest::Schema::NoViewDefinition');
122
123     throws_ok { create_schema({ schema => $schema_invalid_view }) }
124         qr/view noviewdefinition is missing a view_definition/,
125         'parser detects views with a view_definition';
126 }
127
128 lives_ok (sub {
129   my $sqlt_schema = create_schema ({
130     schema => $schema,
131     args => {
132       parser_args => {
133         sources => ['CD']
134       },
135     },
136   });
137
138   is_deeply (
139     [$sqlt_schema->get_tables ],
140     ['cd'],
141     'sources limitng with relationships works',
142   );
143
144 });
145
146 {
147   package DBICTest::PartialSchema;
148
149   use base qw/DBIx::Class::Schema/;
150
151   __PACKAGE__->load_classes(
152     { 'DBICTest::Schema' => [qw/
153       CD
154       Track
155       Tag
156       Producer
157       CD_to_Producer
158     /]}
159   );
160 }
161
162 {
163   my $partial_schema = DBICTest::PartialSchema->connect(DBICTest->_database);
164
165   lives_ok (sub {
166     my $sqlt_schema = do {
167
168       local $SIG{__WARN__} = sub {
169         warn @_
170           unless $_[0] =~ /Ignoring relationship .+ related resultsource .+ is not registered with this schema/
171       };
172
173       create_schema({ schema => $partial_schema });
174     };
175
176     my @tables = $sqlt_schema->get_tables;
177
178     is_deeply (
179       [sort map { $_->name } @tables],
180       [qw/cd cd_to_producer producer tags track/],
181       'partial dbic schema parsing ok',
182     );
183
184     # the primary key is currently unnamed in sqlt - adding below
185     my %constraints_for_table = (
186       producer =>       [qw/prod_name                                                         /],
187       tags =>           [qw/tagid_cd tagid_cd_tag tags_fk_cd tags_tagid_tag tags_tagid_tag_cd /],
188       track =>          [qw/track_cd_position track_cd_title track_fk_cd                      /],
189       cd =>             [qw/cd_artist_title cd_fk_single_track                                /],
190       cd_to_producer => [qw/cd_to_producer_fk_cd cd_to_producer_fk_producer                   /],
191     );
192
193     for my $table (@tables) {
194       my $tablename = $table->name;
195       my @constraints = $table->get_constraints;
196       is_deeply (
197         [ sort map { $_->name } @constraints ],
198
199         # the primary key (present on all loaded tables) is currently named '' in sqlt
200         # subject to future changes
201         [ '', @{$constraints_for_table{$tablename}} ],
202
203         "constraints of table '$tablename' ok",
204       );
205     }
206   }, 'partial schema tests successful');
207 }
208
209 done_testing;
210
211 sub create_schema {
212   my $args = shift;
213
214   my $schema = $args->{schema};
215   my $additional_sqltargs = $args->{args} || {};
216
217   my $sqltargs = {
218     add_drop_table => 1,
219     ignore_constraint_names => 1,
220     ignore_index_names => 1,
221     %{$additional_sqltargs}
222   };
223
224   my $sqlt = SQL::Translator->new( $sqltargs );
225
226   $sqlt->parser('SQL::Translator::Parser::DBIx::Class');
227   return $sqlt->translate({ data => $schema }) || die $sqlt->error;
228 }
229
230 sub get_table {
231     my ($sqlt_schema, $schema, $source) = @_;
232
233     my $table_name = $schema->source($source)->from;
234     $table_name    = $$table_name if ref $table_name;
235
236     return $sqlt_schema->get_table($table_name);
237 }