Allow using all the moniker parts in hashref moniker_map
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 23dumpmore.t
CommitLineData
605fcea8 1use strict;
b564fc4b 2use warnings;
71130750 3use Test::More;
b564fc4b 4use DBIx::Class::Schema::Loader::Utils qw/slurp_file write_file/;
5use namespace::clean;
6use File::Temp ();
71130750 7use lib qw(t/lib);
8use dbixcsl_dumper_tests;
9my $t = 'dbixcsl_dumper_tests';
a4187fdf 10
71130750 11$t->cleanup;
a4187fdf 12
6dde4613 13# test loading external content
71130750 14$t->dump_test(
15 classname => 'DBICTest::Schema::_no_skip_load_external',
71130750 16 regexes => {
17 Foo => [
18 qr/package DBICTest::Schema::_no_skip_load_external::Foo;\nour \$skip_me = "bad mojo";\n1;/
6dde4613 19 ],
71130750 20 },
6dde4613 21);
22
23# test skipping external content
71130750 24$t->dump_test(
25 classname => 'DBICTest::Schema::_skip_load_external',
26 options => {
900195eb 27 skip_load_external => 1,
71130750 28 },
71130750 29 neg_regexes => {
30 Foo => [
31 qr/package DBICTest::Schema::_skip_load_external::Foo;\nour \$skip_me = "bad mojo";\n1;/
6dde4613 32 ],
71130750 33 },
6dde4613 34);
35
71130750 36$t->cleanup;
73099af4 37# test config_file
71130750 38{
39 my $config_file = File::Temp->new (UNLINK => 1);
73099af4 40
71130750 41 print $config_file "{ skip_relationships => 1 }\n";
42 close $config_file;
73099af4 43
71130750 44 $t->dump_test(
1a8fd949 45 classname => 'DBICTest::Schema::_config_file',
71130750 46 options => { config_file => "$config_file" },
73099af4 47 neg_regexes => {
71130750 48 Foo => [
49 qr/has_many/,
50 ],
73099af4 51 },
71130750 52 );
53}
c9cf9b4d 54
71130750 55# proper exception
56$t->dump_test(
1a8fd949 57 classname => 'DBICTest::Schema::_clashing_monikers',
71130750 58 test_db_class => 'make_dbictest_db_clashing_monikers',
c4a69b87 59 error => qr/tables (?:"bar", "bars"|"bars", "bar") reduced to the same source moniker 'Bar'/,
c9cf9b4d 60);
61
c9cf9b4d 62
71130750 63$t->cleanup;
c9cf9b4d 64
c0a0986f 65# test naming => { column_accessors => 'preserve' }
8007f3a7 66# also test POD for unique constraint
c0a0986f 67$t->dump_test(
68 classname => 'DBICTest::Schema::_preserve_column_accessors',
69 test_db_class => 'make_dbictest_db_with_unique',
70 options => { naming => { column_accessors => 'preserve' } },
c0a0986f 71 neg_regexes => {
72 RouteChange => [
73 qr/\baccessor\b/,
74 ],
75 },
8007f3a7 76 regexes => {
77 Baz => [
78 qr/\n\n=head1 UNIQUE CONSTRAINTS\n\n=head2 C<baz_num_unique>\n\n=over 4\n\n=item \* L<\/baz_num>\n\n=back\n\n=cut\n\n__PACKAGE__->add_unique_constraint\("baz_num_unique"\, \["baz_num"\]\);\n\n/,
79 ],
80 }
c0a0986f 81);
82
83$t->cleanup;
84
b33d5949 85# test that rels are sorted
86$t->dump_test(
87 classname => 'DBICTest::Schema::_sorted_rels',
88 test_db_class => 'make_dbictest_db_with_unique',
b33d5949 89 regexes => {
90 Baz => [
91 qr/->might_have\(\n "quux".*->belongs_to\(\n "station_visited"/s,
92 ],
93 }
94);
95
96$t->cleanup;
97
6d358d58 98$t->dump_test(
99 classname => 'DBICTest::Schema::_sorted_uniqs',
100 test_db_class => 'make_dbictest_db_multi_unique',
101 regexes => {
102 Bar => [
103 qr/->add_unique_constraint\("uniq1_unique".*->add_unique_constraint\("uniq2_unique"/s,
104 ],
105 },
106);
107
108$t->cleanup;
109
c0a0986f 110# test naming => { monikers => 'plural' }
111$t->dump_test(
112 classname => 'DBICTest::Schema::_plural_monikers',
113 options => { naming => { monikers => 'plural' } },
c0a0986f 114 regexes => {
115 Foos => [
116 qr/\n=head1 NAME\n\nDBICTest::Schema::_plural_monikers::Foos\n\n=cut\n\n/,
117 ],
118 Bars => [
119 qr/\n=head1 NAME\n\nDBICTest::Schema::_plural_monikers::Bars\n\n=cut\n\n/,
120 ],
121 },
122);
123
124$t->cleanup;
125
126# test naming => { monikers => 'singular' }
127$t->dump_test(
128 classname => 'DBICTest::Schema::_singular_monikers',
129 test_db_class => 'make_dbictest_db_plural_tables',
130 options => { naming => { monikers => 'singular' } },
c0a0986f 131 regexes => {
132 Foo => [
133 qr/\n=head1 NAME\n\nDBICTest::Schema::_singular_monikers::Foo\n\n=cut\n\n/,
134 ],
135 Bar => [
136 qr/\n=head1 NAME\n\nDBICTest::Schema::_singular_monikers::Bar\n\n=cut\n\n/,
137 ],
138 },
139);
140
141$t->cleanup;
142
143# test naming => { monikers => 'preserve' }
144$t->dump_test(
145 classname => 'DBICTest::Schema::_preserve_monikers',
146 test_db_class => 'make_dbictest_db_plural_tables',
147 options => { naming => { monikers => 'preserve' } },
c0a0986f 148 regexes => {
149 Foos => [
150 qr/\n=head1 NAME\n\nDBICTest::Schema::_preserve_monikers::Foos\n\n=cut\n\n/,
151 ],
152 Bars => [
153 qr/\n=head1 NAME\n\nDBICTest::Schema::_preserve_monikers::Bars\n\n=cut\n\n/,
154 ],
155 },
156);
157
158$t->cleanup;
159
c6c8787c 160# test out the POD and "use utf8;"
71130750 161$t->dump_test(
162 classname => 'DBICTest::DumpMore::1',
163 options => {
164 custom_column_info => sub {
165 my ($table, $col, $info) = @_;
166 return +{ extra => { is_footext => 1 } } if $col eq 'footext';
06e06245 167 },
a8acb698 168 result_base_class => 'My::ResultBaseClass',
06e06245 169 additional_classes => 'TestAdditional',
170 additional_base_classes => 'TestAdditionalBase',
171 left_base_classes => 'TestLeftBase',
172 components => [ 'TestComponent', '+TestComponentFQN' ],
71130750 173 },
71130750 174 regexes => {
175 schema => [
c6c8787c 176 qr/^use utf8;\n/,
71130750 177 qr/package DBICTest::DumpMore::1;/,
178 qr/->load_classes/,
c9cf9b4d 179 ],
71130750 180 Foo => [
c6c8787c 181 qr/^use utf8;\n/,
71130750 182 qr/package DBICTest::DumpMore::1::Foo;/,
61c8fd6d 183 qr/\n=head1 NAME\n\nDBICTest::DumpMore::1::Foo\n\n=cut\n\nuse strict;\nuse warnings;\n\n/,
184 qr/\n=head1 BASE CLASS: L<My::ResultBaseClass>\n\n=cut\n\nuse base 'My::ResultBaseClass';\n\n/,
017d9734 185 qr/\n=head1 ADDITIONAL CLASSES USED\n\n=over 4\n\n=item \* L<TestAdditional>\n\n=back\n\n=cut\n\n/,
186 qr/\n=head1 ADDITIONAL BASE CLASSES\n\n=over 4\n\n=item \* L<TestAdditionalBase>\n\n=back\n\n=cut\n\n/,
187 qr/\n=head1 LEFT BASE CLASSES\n\n=over 4\n\n=item \* L<TestLeftBase>\n\n=back\n\n=cut\n\n/,
188 qr/\n=head1 COMPONENTS LOADED\n\n=over 4\n\n=item \* L<DBIx::Class::TestComponent>\n\n=item \* L<TestComponentFQN>\n\n=back\n\n=cut\n\n/,
8007f3a7 189 qr/\n=head1 TABLE: C<foo>\n\n=cut\n\n__PACKAGE__->table\("foo"\);\n\n/,
06e06245 190 qr/\n=head1 ACCESSORS\n\n/,
191 qr/\n=head2 fooid\n\n data_type: 'integer'\n is_auto_increment: 1\n is_nullable: 0\n\n/,
192 qr/\n=head2 footext\n\n data_type: 'text'\n default_value: 'footext'\n extra: {is_footext => 1}\n is_nullable: 1\n\n/,
8007f3a7 193 qr/\n=head1 PRIMARY KEY\n\n=over 4\n\n=item \* L<\/fooid>\n\n=back\n\n=cut\n\n__PACKAGE__->set_primary_key\("fooid"\);\n/,
06e06245 194 qr/\n=head1 RELATIONS\n\n/,
195 qr/\n=head2 bars\n\nType: has_many\n\nRelated object: L<DBICTest::DumpMore::1::Bar>\n\n=cut\n\n/,
71130750 196 qr/1;\n$/,
c9cf9b4d 197 ],
71130750 198 Bar => [
c6c8787c 199 qr/^use utf8;\n/,
71130750 200 qr/package DBICTest::DumpMore::1::Bar;/,
61c8fd6d 201 qr/\n=head1 NAME\n\nDBICTest::DumpMore::1::Bar\n\n=cut\n\nuse strict;\nuse warnings;\n\n/,
202 qr/\n=head1 BASE CLASS: L<My::ResultBaseClass>\n\n=cut\n\nuse base 'My::ResultBaseClass';\n\n/,
017d9734 203 qr/\n=head1 ADDITIONAL CLASSES USED\n\n=over 4\n\n=item \* L<TestAdditional>\n\n=back\n\n=cut\n\n/,
204 qr/\n=head1 ADDITIONAL BASE CLASSES\n\n=over 4\n\n=item \* L<TestAdditionalBase>\n\n=back\n\n=cut\n\n/,
205 qr/\n=head1 LEFT BASE CLASSES\n\n=over 4\n\n=item \* L<TestLeftBase>\n\n=back\n\n=cut\n\n/,
206 qr/\n=head1 COMPONENTS LOADED\n\n=over 4\n\n=item \* L<DBIx::Class::TestComponent>\n\n=item \* L<TestComponentFQN>\n\n=back\n\n=cut\n\n/,
8007f3a7 207 qr/\n=head1 TABLE: C<bar>\n\n=cut\n\n__PACKAGE__->table\("bar"\);\n\n/,
06e06245 208 qr/\n=head1 ACCESSORS\n\n/,
209 qr/\n=head2 barid\n\n data_type: 'integer'\n is_auto_increment: 1\n is_nullable: 0\n\n/,
210 qr/\n=head2 fooref\n\n data_type: 'integer'\n is_foreign_key: 1\n is_nullable: 1\n\n/,
8007f3a7 211 qr/\n=head1 PRIMARY KEY\n\n=over 4\n\n=item \* L<\/barid>\n\n=back\n\n=cut\n\n__PACKAGE__->set_primary_key\("barid"\);\n/,
06e06245 212 qr/\n=head1 RELATIONS\n\n/,
213 qr/\n=head2 fooref\n\nType: belongs_to\n\nRelated object: L<DBICTest::DumpMore::1::Foo>\n\n=cut\n\n/,
214 qr/\n1;\n$/,
c9cf9b4d 215 ],
71130750 216 },
c9cf9b4d 217);
218
71130750 219$t->append_to_class('DBICTest::DumpMore::1::Foo',q{# XXX This is my custom content XXX});
f812ef60 220
9de8c789 221
71130750 222$t->dump_test(
223 classname => 'DBICTest::DumpMore::1',
71130750 224 regexes => {
225 schema => [
226 qr/package DBICTest::DumpMore::1;/,
227 qr/->load_classes/,
a4187fdf 228 ],
71130750 229 Foo => [
230 qr/package DBICTest::DumpMore::1::Foo;/,
231 qr/->set_primary_key/,
232 qr/1;\n# XXX This is my custom content XXX/,
233 ],
234 Bar => [
235 qr/package DBICTest::DumpMore::1::Bar;/,
236 qr/->set_primary_key/,
237 qr/1;\n$/,
238 ],
239 },
a4187fdf 240);
241
a4187fdf 242
71130750 243$t->dump_test(
244 classname => 'DBICTest::DumpMore::1',
245 options => {
246 really_erase_my_files => 1
247 },
71130750 248 regexes => {
249 schema => [
250 qr/package DBICTest::DumpMore::1;/,
251 qr/->load_classes/,
a4187fdf 252 ],
71130750 253 Foo => [
254 qr/package DBICTest::DumpMore::1::Foo;/,
255 qr/->set_primary_key/,
256 qr/1;\n$/,
a4187fdf 257 ],
71130750 258 Bar => [
259 qr/package DBICTest::DumpMore::1::Bar;/,
260 qr/->set_primary_key/,
261 qr/1;\n$/,
262 ],
263 },
264 neg_regexes => {
265 Foo => [
266 qr/# XXX This is my custom content XXX/,
267 ],
268 },
492dce8d 269);
270
eac5494b 271
71130750 272$t->cleanup;
273
274# test namespaces
275$t->dump_test(
276 classname => 'DBICTest::DumpMore::1',
277 options => {
278 use_namespaces => 1,
279 generate_pod => 0
280 },
71130750 281 neg_regexes => {
282 'Result/Foo' => [
283 qr/^=/m,
492dce8d 284 ],
71130750 285 },
a4187fdf 286);
605fcea8 287
eac5494b 288
71130750 289$t->dump_test(
290 classname => 'DBICTest::DumpMore::1',
291 options => {
292 db_schema => 'foo_schema',
293 qualify_objects => 1,
294 use_namespaces => 1
295 },
c4a69b87 296 warnings => [
297 qr/^db_schema is not supported on SQLite/,
298 ],
71130750 299 regexes => {
300 'Result/Foo' => [
301 qr/^\Q__PACKAGE__->table("foo_schema.foo");\E/m,
302 # the has_many relname should not have the schema in it!
303 qr/^__PACKAGE__->has_many\(\n "bars"/m,
69219349 304 ],
71130750 305 },
69219349 306);
307
9890b10c 308# test qualify_objects
309$t->dump_test(
310 classname => 'DBICTest::DumpMore::1',
311 options => {
312 db_schema => [ 'foo_schema', 'bar_schema' ],
313 qualify_objects => 0,
314 use_namespaces => 1,
315 },
316 warnings => [
317 qr/^db_schema is not supported on SQLite/,
318 ],
319 regexes => {
320 'Result/Foo' => [
321 # the table name should not include the db schema
322 qr/^\Q__PACKAGE__->table("foo");\E/m,
323 ],
324 'Result/Bar' => [
325 # the table name should not include the db schema
326 qr/^\Q__PACKAGE__->table("bar");\E/m,
327 ],
328 },
329);
330
c4a69b87 331# test moniker_parts
332$t->dump_test(
333 classname => 'DBICTest::DumpMore::1',
334 options => {
335 db_schema => 'my_schema',
336 moniker_parts => ['_schema', 'name'],
337 qualify_objects => 1,
338 use_namespaces => 1,
339 },
340 warnings => [
341 qr/^db_schema is not supported on SQLite/,
342 ],
343 regexes => {
344 'Result/MySchemaFoo' => [
345 qr/^\Q__PACKAGE__->table("my_schema.foo");\E/m,
848731a7 346 # the has_many relname should not have the schema in it, but the class should
347 qr/^__PACKAGE__->has_many\(\n "bars",\n "DBICTest::DumpMore::1::Result::MySchemaBar"/m,
348 ],
349 },
350);
351
352# test moniker_part_separator
353$t->dump_test(
354 classname => 'DBICTest::DumpMore::1',
355 options => {
356 db_schema => 'my_schema',
357 moniker_parts => ['_schema', 'name'],
358 moniker_part_separator => '::',
359 qualify_objects => 1,
360 use_namespaces => 1,
361 },
362 warnings => [
363 qr/^db_schema is not supported on SQLite/,
364 ],
365 regexes => {
366 'Result/MySchema/Foo' => [
367 qr/^package DBICTest::DumpMore::1::Result::MySchema::Foo;$/m,
368 qr/^\Q__PACKAGE__->table("my_schema.foo");\E/m,
369 # the has_many relname should not have the schema in it, but the class should
370 qr/^__PACKAGE__->has_many\(\n "bars",\n "DBICTest::DumpMore::1::Result::MySchema::Bar"/m,
c4a69b87 371 ],
372 },
373);
374
fc972571 375# test moniker_part_separator + moniker_map
376$t->dump_test(
377 classname => 'DBICTest::DumpMore::1',
378 options => {
379 db_schema => 'my_schema',
380 moniker_parts => ['_schema', 'name'],
381 moniker_part_separator => '::',
382 qualify_objects => 1,
383 use_namespaces => 1,
384 moniker_map => {
385 my_schema => { foo => "MySchema::Floop" },
386 }
387 },
388 warnings => [
389 qr/^db_schema is not supported on SQLite/,
390 ],
391 regexes => {
392 'Result/MySchema/Floop' => [
393 qr/^package DBICTest::DumpMore::1::Result::MySchema::Floop;$/m,
394 qr/^\Q__PACKAGE__->table("my_schema.foo");\E/m,
395 # the has_many relname should not have the schema in it!
396 qr/^__PACKAGE__->has_many\(\n "bars"/m,
397 ],
398 },
399);
400
71130750 401$t->dump_test(
402 classname => 'DBICTest::DumpMore::1',
403 options => {
404 use_namespaces => 1
405 },
71130750 406 regexes => {
407 schema => [
408 qr/package DBICTest::DumpMore::1;/,
409 qr/->load_namespaces/,
f44ecc2f 410 ],
71130750 411 'Result/Foo' => [
412 qr/package DBICTest::DumpMore::1::Result::Foo;/,
413 qr/->set_primary_key/,
414 qr/1;\n$/,
415 ],
416 'Result/Bar' => [
417 qr/package DBICTest::DumpMore::1::Result::Bar;/,
418 qr/->set_primary_key/,
419 qr/1;\n$/,
420 ],
421 },
f44ecc2f 422);
423
eac5494b 424
71130750 425$t->dump_test(
426 classname => 'DBICTest::DumpMore::1',
427 options => {
428 use_namespaces => 1,
429 result_namespace => 'Res',
430 resultset_namespace => 'RSet',
431 default_resultset_class => 'RSetBase',
432 },
71130750 433 regexes => {
434 schema => [
435 qr/package DBICTest::DumpMore::1;/,
436 qr/->load_namespaces/,
eb28f95b 437 qr/result_namespace => "Res"/,
438 qr/resultset_namespace => "RSet"/,
439 qr/default_resultset_class => "RSetBase"/,
f44ecc2f 440 ],
71130750 441 'Res/Foo' => [
442 qr/package DBICTest::DumpMore::1::Res::Foo;/,
443 qr/->set_primary_key/,
444 qr/1;\n$/,
445 ],
446 'Res/Bar' => [
447 qr/package DBICTest::DumpMore::1::Res::Bar;/,
448 qr/->set_primary_key/,
449 qr/1;\n$/,
450 ],
451 },
f44ecc2f 452);
453
eac5494b 454
71130750 455$t->dump_test(
456 classname => 'DBICTest::DumpMore::1',
457 options => {
458 use_namespaces => 1,
459 result_namespace => '+DBICTest::DumpMore::1::Res',
460 resultset_namespace => 'RSet',
461 default_resultset_class => 'RSetBase',
462 result_base_class => 'My::ResultBaseClass',
463 schema_base_class => 'My::SchemaBaseClass',
464 },
71130750 465 regexes => {
466 schema => [
467 qr/package DBICTest::DumpMore::1;/,
468 qr/->load_namespaces/,
eb28f95b 469 qr/result_namespace => "\+DBICTest::DumpMore::1::Res"/,
470 qr/resultset_namespace => "RSet"/,
471 qr/default_resultset_class => "RSetBase"/,
71130750 472 qr/use base 'My::SchemaBaseClass'/,
f44ecc2f 473 ],
71130750 474 'Res/Foo' => [
475 qr/package DBICTest::DumpMore::1::Res::Foo;/,
476 qr/use base 'My::ResultBaseClass'/,
477 qr/->set_primary_key/,
478 qr/1;\n$/,
479 ],
480 'Res/Bar' => [
481 qr/package DBICTest::DumpMore::1::Res::Bar;/,
482 qr/use base 'My::ResultBaseClass'/,
483 qr/->set_primary_key/,
484 qr/1;\n$/,
485 ],
486 },
f44ecc2f 487);
c634fde9 488
71130750 489$t->dump_test(
490 classname => 'DBICTest::DumpMore::1',
491 options => {
492 use_namespaces => 1,
493 result_base_class => 'My::MissingResultBaseClass',
494 },
495 error => qr/My::MissingResultBaseClass.*is not installed/,
8048320c 496);
f44ecc2f 497
667f1a0b 498# test quote_char in connect_info for dbicdump
499$t->dump_test(
500 classname => 'DBICTest::DumpMore::1',
501 extra_connect_info => [
502 '',
503 '',
504 { quote_char => '"' },
505 ],
667f1a0b 506);
507
b564fc4b 508# test fix for RT#70507 (end comment and 1; gets lost if left with actual
509# custom content)
510
511$t->dump_test(
512 classname => 'DBICTest::DumpMore::Upgrade',
513 options => {
514 use_namespaces => 0,
515 },
516);
517
518my $file = $t->class_file('DBICTest::DumpMore::Upgrade::Foo');
519
520my $code = slurp_file $file;
521
522$code =~ s/(?=# You can replace)/sub custom_method { 'custom_method works' }\n0;\n\n/;
523
524write_file $file, $code;
525
526$t->dump_test(
527 classname => 'DBICTest::DumpMore::Upgrade',
528 options => {
529 use_namespaces => 1,
530 },
531 regexes => {
532 'Result/Foo' => [
533 qr/sub custom_method { 'custom_method works' }\n0;\n\n# You can replace.*\n1;\n\z/,
534 ],
535 },
536);
537
d27f2b7b 538done_testing;
c0a0986f 539# vim:et sts=4 sw=4 tw=0: