-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathdbixcsl_common_tests.pm
2513 lines (1977 loc) · 89.2 KB
/
dbixcsl_common_tests.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package dbixcsl_common_tests;
use strict;
use warnings;
use Test::More;
use Test::Deep;
use Test::Exception;
use Test::Differences;
use DBIx::Class::Schema::Loader;
use Class::Unload;
use File::Path 'rmtree';
use curry;
use DBI;
use File::Find 'find';
use Class::Unload ();
use DBIx::Class::Schema::Loader::Utils qw/dumper_squashed slurp_file sigwarn_silencer apply/;
use DBIx::Class::Schema::Loader::Optional::Dependencies ();
use Try::Tiny;
use File::Spec::Functions 'catfile';
use File::Basename 'basename';
use namespace::clean;
use dbixcsl_test_dir '$tdir';
use constant DUMP_DIR => "$tdir/common_dump";
rmtree DUMP_DIR;
use constant RESCAN_WARNINGS => qr/(?i:loader_test|LoaderTest)\d+s? has no primary key|^Dumping manual schema|^Schema dump completed|collides with an inherited method|invalidates \d+ active statement|^Bad table or view/;
# skip schema-qualified tables in the Pg tests
use constant SOURCE_DDL => qr/CREATE (?:TABLE|VIEW) (?!"dbicsl[.-]test")/i;
use constant SCHEMA_CLASS => 'DBIXCSL_Test::Schema';
use constant RESULT_NAMESPACE => [ 'MyResult', 'MyResultTwo' ];
use constant RESULTSET_NAMESPACE => [ 'MyResultSet', 'MyResultSetTwo' ];
sub new {
my $class = shift;
my $self;
if( ref($_[0]) eq 'HASH') {
my $args = shift;
$self = { (%$args) };
}
else {
$self = { @_ };
}
# Only MySQL uses this
$self->{innodb} ||= '';
# DB2 and Firebird don't support 'field type NULL'
$self->{null} = 'NULL' unless defined $self->{null};
$self->{verbose} = $ENV{TEST_VERBOSE} || 0;
# Optional extra tables and tests
$self->{extra} ||= {};
$self->{basic_date_datatype} ||= 'DATE';
# Not all DBS do SQL-standard CURRENT_TIMESTAMP
$self->{default_function} ||= "current_timestamp";
$self->{default_function_def} ||= "timestamp default $self->{default_function}";
$self = bless $self, $class;
$self->{preserve_case_tests_table_names} = [qw/LoaderTest40 LoaderTest41/];
if (lc($self->{vendor}) eq 'mysql' && $^O =~ /^(?:MSWin32|cygwin)\z/) {
$self->{preserve_case_tests_table_names} = [qw/Loader_Test40 Loader_Test41/];
}
$self->setup_data_type_tests;
return $self;
}
sub skip_tests {
my ($self, $why) = @_;
plan skip_all => $why;
}
sub _monikerize {
my $name = shift;
my $orig = pop;
return $orig->({
loader_test2 => 'LoaderTest2X',
LOADER_TEST2 => 'LoaderTest2X',
});
}
sub run_tests {
my $self = shift;
my @connect_info;
if ($self->{dsn}) {
push @connect_info, [ @{$self}{qw/dsn user password connect_info_opts/ } ];
}
else {
foreach my $info (@{ $self->{connect_info} || [] }) {
push @connect_info, [ @{$info}{qw/dsn user password connect_info_opts/ } ];
}
}
if ($ENV{SCHEMA_LOADER_TESTS_EXTRA_ONLY}) {
$self->run_only_extra_tests(\@connect_info);
return;
}
my $extra_count = $self->{extra}{count} || 0;
my $col_accessor_map_tests = 6;
plan tests => @connect_info *
(237 + $col_accessor_map_tests + $extra_count + ($self->{data_type_tests}{test_count} || 0));
foreach my $info_idx (0..$#connect_info) {
my $info = $connect_info[$info_idx];
@{$self}{qw/dsn user password connect_info_opts/} = @$info;
$self->create();
my $schema_class = $self->setup_schema($info);
$self->test_schema($schema_class);
rmtree DUMP_DIR
unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP} && $info_idx == $#connect_info;
}
}
sub run_only_extra_tests {
my ($self, $connect_info) = @_;
plan tests => @$connect_info * (3 + ($self->{extra}{count} || 0) + ($self->{data_type_tests}{test_count} || 0));
rmtree DUMP_DIR;
foreach my $info_idx (0..$#$connect_info) {
my $info = $connect_info->[$info_idx];
@{$self}{qw/dsn user password connect_info_opts/} = @$info;
$self->drop_extra_tables_only;
my $dbh = $self->dbconnect(1);
$dbh->do($_) for @{ $self->{pre_create} || [] };
$dbh->do($_) for @{ $self->{extra}{create} || [] };
if (not ($self->{vendor} eq 'mssql' && $dbh->{Driver}{Name} eq 'Sybase')) {
foreach my $ddl (@{ $self->{data_type_tests}{ddl} || []}) {
if (my $cb = $self->{data_types_ddl_cb}) {
$cb->($ddl);
}
else {
$dbh->do($ddl);
}
}
}
$dbh->disconnect;
$self->{_created} = 1;
my $file_count = grep $_ =~ SOURCE_DDL, @{ $self->{extra}{create} || [] };
$file_count++; # schema
if (not ($self->{vendor} eq 'mssql' && $dbh->{Driver}{Name} eq 'Sybase')) {
$file_count++ for @{ $self->{data_type_tests}{table_names} || [] };
}
my $schema_class = $self->setup_schema($info, $file_count);
my ($monikers, $classes) = $self->monikers_and_classes($schema_class);
my $conn = $schema_class->clone;
$self->test_data_types($conn);
$self->{extra}{run}->($conn, $monikers, $classes, $self) if $self->{extra}{run};
$conn->storage->disconnect;
if (not ($ENV{SCHEMA_LOADER_TESTS_NOCLEANUP} && $info_idx == $#$connect_info)) {
$self->drop_extra_tables_only;
rmtree DUMP_DIR;
}
}
}
sub drop_extra_tables_only {
my $self = shift;
my $dbh = $self->dbconnect(0);
local $^W = 0; # for ADO
$dbh->do($_) for @{ $self->{extra}{pre_drop_ddl} || [] };
$self->drop_table($dbh, $_) for @{ $self->{extra}{drop} || [] };
if (not ($self->{vendor} eq 'mssql' && $dbh->{Driver}{Name} eq 'Sybase')) {
foreach my $data_type_table (@{ $self->{data_type_tests}{table_names} || [] }) {
$self->drop_table($dbh, $data_type_table);
}
}
}
# defined in sub create
my (@statements, @statements_reltests, @statements_advanced,
@statements_advanced_sqlite, @statements_inline_rels,
@statements_implicit_rels);
sub CONSTRAINT {
my $self = shift;
return qr/^(?:(?:$self->{vendor}|extra)[_-]?)?loader[_-]?test[0-9]+(?!.*_)/i;
}
sub setup_schema {
my ($self, $connect_info, $expected_count) = @_;
my $debug = ($self->{verbose} > 1) ? 1 : 0;
if ($ENV{SCHEMA_LOADER_TESTS_USE_MOOSE}) {
if (not DBIx::Class::Schema::Loader::Optional::Dependencies->req_ok_for('use_moose')) {
die sprintf ("Missing dependencies for SCHEMA_LOADER_TESTS_USE_MOOSE: %s\n",
DBIx::Class::Schema::Loader::Optional::Dependencies->req_missing_for('use_moose'));
}
$self->{use_moose} = 1;
}
$self->{col_accessor_map_tests_run} = 0;
my %loader_opts = (
constraint => $self->CONSTRAINT,
result_namespace => RESULT_NAMESPACE,
resultset_namespace => RESULTSET_NAMESPACE,
schema_base_class => 'TestSchemaBaseClass',
schema_components => [ 'TestSchemaComponent', '+TestSchemaComponentFQN' ],
additional_classes => 'TestAdditional',
additional_base_classes => 'TestAdditionalBase',
left_base_classes => [ qw/TestLeftBase/ ],
components => [ qw/TestComponent +TestComponentFQN IntrospectableM2M/ ],
inflect_plural => { loader_test4_fkid => 'loader_test4zes' },
inflect_singular => { fkid => 'fkid_singular' },
moniker_map => \&_monikerize,
custom_column_info => \&_custom_column_info,
exclude_columns => \&_exclude_columns,
debug => $debug,
dump_directory => DUMP_DIR,
datetime_timezone => 'Europe/Berlin',
datetime_locale => 'de_DE',
$self->{use_moose} ? (
use_moose => 1,
result_roles => 'TestRole',
result_roles_map => { LoaderTest2X => 'TestRoleForMap' },
) : (),
col_collision_map => { '^(can)\z' => 'caught_collision_%s' },
rel_collision_map => { '^(set_primary_key)\z' => 'caught_rel_collision_%s' },
relationship_attrs => { many_to_many => { order_by => 'me.id' } },
col_accessor_map => $self->curry::weak::test_col_accessor_map,
result_components_map => { LoaderTest2X => 'TestComponentForMap', LoaderTest1 => '+TestComponentForMapFQN' },
uniq_to_primary => 1,
%{ $self->{loader_options} || {} },
);
$loader_opts{db_schema} = $self->{db_schema} if $self->{db_schema};
Class::Unload->unload(SCHEMA_CLASS);
my $file_count;
{
my @loader_warnings;
local $SIG{__WARN__} = sub { push(@loader_warnings, @_); };
eval qq{
package @{[SCHEMA_CLASS]};
use base qw/DBIx::Class::Schema::Loader/;
__PACKAGE__->loader_options(\%loader_opts);
__PACKAGE__->connection(\@\$connect_info);
};
ok(!$@, "Loader initialization") or diag $@;
find sub { return if -d; $file_count++ }, DUMP_DIR;
my $standard_sources = not defined $expected_count;
if ($standard_sources) {
$expected_count = 42;
if (not ($self->{vendor} eq 'mssql' && $connect_info->[0] =~ /Sybase/)) {
$expected_count++ for @{ $self->{data_type_tests}{table_names} || [] };
}
$expected_count += grep $_ =~ SOURCE_DDL,
@{ $self->{extra}{create} || [] };
$expected_count -= grep /CREATE TABLE/i, @statements_inline_rels
if $self->{skip_rels} || $self->{no_inline_rels};
$expected_count -= grep /CREATE TABLE/i, @statements_implicit_rels
if $self->{skip_rels} || $self->{no_implicit_rels};
$expected_count -= grep /CREATE TABLE/i, ($self->{vendor} =~ /sqlite/ ? @statements_advanced_sqlite : @statements_advanced), @statements_reltests
if $self->{skip_rels};
}
is $file_count, $expected_count, 'correct number of files generated';
my $warn_count = 2;
$warn_count++ for grep /^Bad table or view/, @loader_warnings;
$warn_count++ for grep /renaming \S+ relation/, @loader_warnings;
$warn_count++ for grep /\b(?!loader_test9)\w+ has no primary key/i, @loader_warnings;
$warn_count++ for grep /^Column '\w+' in table '\w+' collides with an inherited method\./, @loader_warnings;
$warn_count++ for grep /^Relationship '\w+' in source '\w+' for columns '[^']+' collides with an inherited method\./, @loader_warnings;
$warn_count++ for grep { my $w = $_; grep $w =~ $_, @{ $self->{warnings} || [] } } @loader_warnings;
$warn_count-- for grep { my $w = $_; grep $w =~ $_, @{ $self->{failtrigger_warnings} || [] } } @loader_warnings;
is scalar(@loader_warnings), $warn_count, 'Correct number of warnings'
or diag @loader_warnings;
}
exit if ($file_count||0) != $expected_count;
return SCHEMA_CLASS;
}
sub test_schema {
my $self = shift;
my $schema_class = shift;
my $conn = $schema_class->clone;
($self->{before_tests_run} || sub {})->($conn);
my ($monikers, $classes) = $self->monikers_and_classes($schema_class);
my $moniker1 = $monikers->{loader_test1s};
my $class1 = $classes->{loader_test1s};
my $rsobj1 = $conn->resultset($moniker1);
check_no_duplicate_unique_constraints($class1);
my $moniker2 = $monikers->{loader_test2};
my $class2 = $classes->{loader_test2};
my $rsobj2 = $conn->resultset($moniker2);
check_no_duplicate_unique_constraints($class2);
my $moniker23 = $monikers->{LOADER_test23} || $monikers->{loader_test23};
my $class23 = $classes->{LOADER_test23} || $classes->{loader_test23};
my $rsobj23 = $conn->resultset($moniker1);
my $moniker24 = $monikers->{LoAdEr_test24} || $monikers->{loader_test24};
my $class24 = $classes->{LoAdEr_test24} || $classes->{loader_test24};
my $rsobj24 = $conn->resultset($moniker2);
my $moniker35 = $monikers->{loader_test35};
my $class35 = $classes->{loader_test35};
my $rsobj35 = $conn->resultset($moniker35);
my $moniker50 = $monikers->{loader_test50};
my $class50 = $classes->{loader_test50};
my $rsobj50 = $conn->resultset($moniker50);
isa_ok( $rsobj1, "DBIx::Class::ResultSet" );
isa_ok( $rsobj2, "DBIx::Class::ResultSet" );
isa_ok( $rsobj23, "DBIx::Class::ResultSet" );
isa_ok( $rsobj24, "DBIx::Class::ResultSet" );
isa_ok( $rsobj35, "DBIx::Class::ResultSet" );
isa_ok( $rsobj50, "DBIx::Class::ResultSet" );
# check result_namespace
my @schema_dir = split /::/, SCHEMA_CLASS;
my $result_dir = ref RESULT_NAMESPACE ? ${RESULT_NAMESPACE()}[0] : RESULT_NAMESPACE;
my $schema_files = [ sort map basename($_), glob catfile(DUMP_DIR, @schema_dir, '*') ];
is_deeply $schema_files, [ $result_dir ],
'first entry in result_namespace exists as a directory';
my $result_file_count =()= glob catfile(DUMP_DIR, @schema_dir, $result_dir, '*.pm');
ok $result_file_count,
'Result files dumped to first entry in result_namespace';
# parse out the resultset_namespace
my $schema_code = slurp_file $conn->_loader->get_dump_filename(SCHEMA_CLASS);
my ($schema_resultset_namespace) = $schema_code =~ /\bresultset_namespace => (.*)/;
$schema_resultset_namespace = eval $schema_resultset_namespace;
die $@ if $@;
is_deeply $schema_resultset_namespace, RESULTSET_NAMESPACE,
'resultset_namespace set correctly on Schema';
like $schema_code,
qr/\nuse base 'TestSchemaBaseClass';\n\n|\nextends 'TestSchemaBaseClass';\n\n/,
'schema_base_class works';
is $conn->testschemabaseclass, 'TestSchemaBaseClass works',
'schema base class works';
like $schema_code,
qr/\n__PACKAGE__->load_components\("TestSchemaComponent", "\+TestSchemaComponentFQN"\);\n\n__PACKAGE__->load_namespaces/,
'schema_components works';
is $conn->dbix_class_testschemacomponent, 'dbix_class_testschemacomponent works',
'schema component works';
is $conn->testschemacomponent_fqn, 'TestSchemaComponentFQN works',
'fully qualified schema component works';
my @columns_lt2 = $class2->columns;
is_deeply( \@columns_lt2, [ qw/id dat dat2 set_primary_key can dbix_class_testcomponent dbix_class_testcomponentmap testcomponent_fqn meta test_role_method test_role_for_map_method crumb_crisp_coating sticky_filling/ ], "Column Ordering" );
is $class2->column_info('can')->{accessor}, 'caught_collision_can',
'accessor for column name that conflicts with a UNIVERSAL method renamed based on col_collision_map';
ok (exists $class2->column_info('set_primary_key')->{accessor}
&& (not defined $class2->column_info('set_primary_key')->{accessor}),
'accessor for column name that conflicts with a result base class method removed');
ok (exists $class2->column_info('dbix_class_testcomponent')->{accessor}
&& (not defined $class2->column_info('dbix_class_testcomponent')->{accessor}),
'accessor for column name that conflicts with a component class method removed');
ok (exists $class2->column_info('dbix_class_testcomponentmap')->{accessor}
&& (not defined $class2->column_info('dbix_class_testcomponentmap')->{accessor}),
'accessor for column name that conflicts with a component class method removed');
ok (exists $class2->column_info('testcomponent_fqn')->{accessor}
&& (not defined $class2->column_info('testcomponent_fqn')->{accessor}),
'accessor for column name that conflicts with a fully qualified component class method removed');
if ($self->{use_moose}) {
ok (exists $class2->column_info('meta')->{accessor}
&& (not defined $class2->column_info('meta')->{accessor}),
'accessor for column name that conflicts with Moose removed');
ok (exists $class2->column_info('test_role_for_map_method')->{accessor}
&& (not defined $class2->column_info('test_role_for_map_method')->{accessor}),
'accessor for column name that conflicts with a Result role removed');
ok (exists $class2->column_info('test_role_method')->{accessor}
&& (not defined $class2->column_info('test_role_method')->{accessor}),
'accessor for column name that conflicts with a Result role removed');
}
else {
ok ((not exists $class2->column_info('meta')->{accessor}),
"not removing 'meta' accessor with use_moose disabled");
ok ((not exists $class2->column_info('test_role_for_map_method')->{accessor}),
'no role method conflicts with use_moose disabled');
ok ((not exists $class2->column_info('test_role_method')->{accessor}),
'no role method conflicts with use_moose disabled');
}
my %uniq1 = $class1->unique_constraints;
my $uniq1_test = 0;
foreach my $ucname (keys %uniq1) {
my $cols_arrayref = $uniq1{$ucname};
if(@$cols_arrayref == 1 && $cols_arrayref->[0] eq 'dat') {
$uniq1_test = 1;
last;
}
}
ok($uniq1_test, "Unique constraint");
is($moniker1, 'LoaderTest1', 'moniker singularisation');
my %uniq2 = $class2->unique_constraints;
my $uniq2_test = 0;
foreach my $ucname (keys %uniq2) {
my $cols_arrayref = $uniq2{$ucname};
if (@$cols_arrayref == 2
&& $cols_arrayref->[0] eq 'dat2'
&& $cols_arrayref->[1] eq 'dat'
) {
$uniq2_test = 2;
last;
}
}
ok($uniq2_test, "Multi-col unique constraint");
my %uniq3 = $class50->unique_constraints;
is_deeply $uniq3{primary}, ['id1', 'id2'],
'unique constraint promoted to primary key with uniq_to_primary';
is($moniker2, 'LoaderTest2X', "moniker_map testing");
SKIP: {
can_ok( $class1, 'test_additional_base' )
or skip "Pre-requisite test failed", 1;
is( $class1->test_additional_base, "test_additional_base",
"Additional Base method" );
}
SKIP: {
can_ok( $class1, 'test_additional_base_override' )
or skip "Pre-requisite test failed", 1;
is( $class1->test_additional_base_override,
"test_left_base_override",
"Left Base overrides Additional Base method" );
}
SKIP: {
can_ok( $class1, 'test_additional_base_additional' )
or skip "Pre-requisite test failed", 1;
is( $class1->test_additional_base_additional, "test_additional",
"Additional Base can use Additional package method" );
}
SKIP: {
can_ok( $class1, 'dbix_class_testcomponent' )
or skip "Pre-requisite test failed", 1;
is( $class1->dbix_class_testcomponent,
'dbix_class_testcomponent works',
'Additional Component' );
}
is try { $class2->dbix_class_testcomponentmap }, 'dbix_class_testcomponentmap works',
'component from result_component_map';
isnt try { $class1->dbix_class_testcomponentmap }, 'dbix_class_testcomponentmap works',
'component from result_component_map not added to not mapped Result';
is try { $class1->testcomponent_fqn }, 'TestComponentFQN works',
'fully qualified component class';
is try { $class1->testcomponentformap_fqn }, 'TestComponentForMapFQN works',
'fully qualified component class from result_component_map';
isnt try { $class2->testcomponentformap_fqn }, 'TestComponentForMapFQN works',
'fully qualified component class from result_component_map not added to not mapped Result';
SKIP: {
skip 'not testing role methods with use_moose disabled', 2
unless $self->{use_moose};
is try { $class1->test_role_method }, 'test_role_method works',
'role from result_roles applied';
is try { $class2->test_role_for_map_method },
'test_role_for_map_method works',
'role from result_roles_map applied';
}
SKIP: {
can_ok( $class1, 'loader_test1_classmeth' )
or skip "Pre-requisite test failed", 1;
is( $class1->loader_test1_classmeth, 'all is well', 'Class method' );
}
ok( $class1->column_info('id')->{is_auto_increment}, 'is_auto_increment detection' );
my $obj = try { $rsobj1->find(1) };
is( try { $obj->id }, 1, "Find got the right row" );
is( try { $obj->dat }, "foo", "Column value" );
is( $rsobj2->count, 4, "Count" );
my $saved_id;
eval {
my $new_obj1 = $rsobj1->create({ dat => 'newthing' });
$saved_id = $new_obj1->id;
};
ok(!$@, "Inserting new record using a PK::Auto key didn't die") or diag $@;
ok($saved_id, "Got PK::Auto-generated id");
my $new_obj1 = $rsobj1->search({ dat => 'newthing' })->single;
ok($new_obj1, "Found newly inserted PK::Auto record");
is($new_obj1->id, $saved_id, "Correct PK::Auto-generated id");
my ($obj2) = $rsobj2->search({ dat => 'bbb' })->single;
is( $obj2->id, 2 );
SKIP: {
skip 'no DEFAULT on Access', 7 if $self->{vendor} eq 'Access';
is(
$class35->column_info('a_varchar')->{default_value}, 'foo',
'constant character default',
);
is(
$class35->column_info('an_int')->{default_value}, 42,
'constant integer default',
);
is(
$class35->column_info('a_negative_int')->{default_value}, -42,
'constant negative integer default',
);
is(
sprintf("%.3f", $class35->column_info('a_double')->{default_value}||0), '10.555',
'constant numeric default',
);
is(
sprintf("%.3f", $class35->column_info('a_negative_double')->{default_value}||0), -10.555,
'constant negative numeric default',
);
my $function_default = $class35->column_info('a_function')->{default_value};
isa_ok( $function_default, 'SCALAR', 'default_value for function default' );
is_deeply(
$function_default, \$self->{default_function},
'default_value for function default is correct'
);
}
is( $class2->column_info('crumb_crisp_coating')->{accessor}, 'trivet',
'col_accessor_map is being run' );
is( $class2->column_info('sticky_filling')->{accessor}, 'goo',
'multi-level hash col_accessor_map works' );
is $class1->column_info('dat')->{is_nullable}, 0,
'is_nullable=0 detection';
is $class2->column_info('set_primary_key')->{is_nullable}, 1,
'is_nullable=1 detection';
SKIP: {
skip $self->{skip_rels}, 143 if $self->{skip_rels};
my $moniker3 = $monikers->{loader_test3};
my $class3 = $classes->{loader_test3};
my $rsobj3 = $conn->resultset($moniker3);
my $moniker4 = $monikers->{loader_test4};
my $class4 = $classes->{loader_test4};
my $rsobj4 = $conn->resultset($moniker4);
my $moniker5 = $monikers->{loader_test5};
my $class5 = $classes->{loader_test5};
my $rsobj5 = $conn->resultset($moniker5);
my $moniker6 = $monikers->{loader_test6};
my $class6 = $classes->{loader_test6};
my $rsobj6 = $conn->resultset($moniker6);
my $moniker7 = $monikers->{loader_test7};
my $class7 = $classes->{loader_test7};
my $rsobj7 = $conn->resultset($moniker7);
my $moniker8 = $monikers->{loader_test8};
my $class8 = $classes->{loader_test8};
my $rsobj8 = $conn->resultset($moniker8);
my $moniker9 = $monikers->{loader_test9};
my $class9 = $classes->{loader_test9};
my $rsobj9 = $conn->resultset($moniker9);
my $moniker16 = $monikers->{loader_test16};
my $class16 = $classes->{loader_test16};
my $rsobj16 = $conn->resultset($moniker16);
my $moniker17 = $monikers->{loader_test17};
my $class17 = $classes->{loader_test17};
my $rsobj17 = $conn->resultset($moniker17);
my $moniker18 = $monikers->{loader_test18};
my $class18 = $classes->{loader_test18};
my $rsobj18 = $conn->resultset($moniker18);
my $moniker19 = $monikers->{loader_test19};
my $class19 = $classes->{loader_test19};
my $rsobj19 = $conn->resultset($moniker19);
my $moniker20 = $monikers->{loader_test20};
my $class20 = $classes->{loader_test20};
my $rsobj20 = $conn->resultset($moniker20);
my $moniker21 = $monikers->{loader_test21};
my $class21 = $classes->{loader_test21};
my $rsobj21 = $conn->resultset($moniker21);
my $moniker22 = $monikers->{loader_test22};
my $class22 = $classes->{loader_test22};
my $rsobj22 = $conn->resultset($moniker22);
my $moniker25 = $monikers->{loader_test25};
my $class25 = $classes->{loader_test25};
my $rsobj25 = $conn->resultset($moniker25);
my $moniker26 = $monikers->{loader_test26};
my $class26 = $classes->{loader_test26};
my $rsobj26 = $conn->resultset($moniker26);
my $moniker27 = $monikers->{loader_test27};
my $class27 = $classes->{loader_test27};
my $rsobj27 = $conn->resultset($moniker27);
my $moniker28 = $monikers->{loader_test28};
my $class28 = $classes->{loader_test28};
my $rsobj28 = $conn->resultset($moniker28);
my $moniker29 = $monikers->{loader_test29};
my $class29 = $classes->{loader_test29};
my $rsobj29 = $conn->resultset($moniker29);
my $moniker31 = $monikers->{loader_test31};
my $class31 = $classes->{loader_test31};
my $rsobj31 = $conn->resultset($moniker31);
my $moniker32 = $monikers->{loader_test32};
my $class32 = $classes->{loader_test32};
my $rsobj32 = $conn->resultset($moniker32);
my $moniker33 = $monikers->{loader_test33};
my $class33 = $classes->{loader_test33};
my $rsobj33 = $conn->resultset($moniker33);
my $moniker34 = $monikers->{loader_test34};
my $class34 = $classes->{loader_test34};
my $rsobj34 = $conn->resultset($moniker34);
my $moniker36 = $monikers->{loader_test36};
my $class36 = $classes->{loader_test36};
my $rsobj36 = $conn->resultset($moniker36);
my $moniker37 = $monikers->{loader_test37};
my $class37 = $classes->{loader_test37};
my $rsobj37 = $conn->resultset($moniker37);
my $moniker38 = $monikers->{loader_test38};
my $class38 = $classes->{loader_test38};
my $rsobj38 = $conn->resultset($moniker38);
my $moniker42 = $monikers->{loader_test42};
my $class42 = $classes->{loader_test42};
my $rsobj42 = $conn->resultset($moniker42);
my $moniker43 = $monikers->{loader_test43};
my $class43 = $classes->{loader_test43};
my $rsobj43 = $conn->resultset($moniker43);
my $moniker44 = $monikers->{loader_test44};
my $class44 = $classes->{loader_test44};
my $rsobj44 = $conn->resultset($moniker44);
isa_ok( $rsobj3, "DBIx::Class::ResultSet" );
isa_ok( $rsobj4, "DBIx::Class::ResultSet" );
isa_ok( $rsobj5, "DBIx::Class::ResultSet" );
isa_ok( $rsobj6, "DBIx::Class::ResultSet" );
isa_ok( $rsobj7, "DBIx::Class::ResultSet" );
isa_ok( $rsobj8, "DBIx::Class::ResultSet" );
isa_ok( $rsobj9, "DBIx::Class::ResultSet" );
isa_ok( $rsobj16, "DBIx::Class::ResultSet" );
isa_ok( $rsobj17, "DBIx::Class::ResultSet" );
isa_ok( $rsobj18, "DBIx::Class::ResultSet" );
isa_ok( $rsobj19, "DBIx::Class::ResultSet" );
isa_ok( $rsobj20, "DBIx::Class::ResultSet" );
isa_ok( $rsobj21, "DBIx::Class::ResultSet" );
isa_ok( $rsobj22, "DBIx::Class::ResultSet" );
isa_ok( $rsobj25, "DBIx::Class::ResultSet" );
isa_ok( $rsobj26, "DBIx::Class::ResultSet" );
isa_ok( $rsobj27, "DBIx::Class::ResultSet" );
isa_ok( $rsobj28, "DBIx::Class::ResultSet" );
isa_ok( $rsobj29, "DBIx::Class::ResultSet" );
isa_ok( $rsobj31, "DBIx::Class::ResultSet" );
isa_ok( $rsobj32, "DBIx::Class::ResultSet" );
isa_ok( $rsobj33, "DBIx::Class::ResultSet" );
isa_ok( $rsobj34, "DBIx::Class::ResultSet" );
isa_ok( $rsobj36, "DBIx::Class::ResultSet" );
isa_ok( $rsobj37, "DBIx::Class::ResultSet" );
isa_ok( $rsobj38, "DBIx::Class::ResultSet" );
isa_ok( $rsobj42, "DBIx::Class::ResultSet" );
isa_ok( $rsobj43, "DBIx::Class::ResultSet" );
isa_ok( $rsobj44, "DBIx::Class::ResultSet" );
# basic rel test
my $obj4 = try { $rsobj4->find(123) } || $rsobj4->search({ id => 123 })->single;
isa_ok( try { $obj4->fkid_singular }, $class3);
# test renaming rel that conflicts with a class method
ok ($obj4->has_relationship('belongs_to_rel'), 'relationship name that conflicts with a method renamed');
isa_ok( try { $obj4->belongs_to_rel }, $class3);
ok ($obj4->has_relationship('caught_rel_collision_set_primary_key'),
'relationship name that conflicts with a method renamed based on rel_collision_map');
isa_ok( try { $obj4->caught_rel_collision_set_primary_key }, $class3);
ok($class4->column_info('fkid')->{is_foreign_key}, 'Foreign key detected');
my $obj3 = try { $rsobj3->find(1) } || $rsobj3->search({ id => 1 })->single;
my $rs_rel4 = try { $obj3->search_related('loader_test4zes') };
isa_ok( try { $rs_rel4->single }, $class4);
# check rel naming with prepositions
ok ($rsobj4->result_source->has_relationship('loader_test5s_to'),
"rel with preposition 'to' pluralized correctly");
ok ($rsobj4->result_source->has_relationship('loader_test5s_from'),
"rel with preposition 'from' pluralized correctly");
# check default relationship attributes
is try { $rsobj3->result_source->relationship_info('loader_test4zes')->{attrs}{cascade_delete} }, 0,
'cascade_delete => 0 on has_many by default';
is try { $rsobj3->result_source->relationship_info('loader_test4zes')->{attrs}{cascade_copy} }, 0,
'cascade_copy => 0 on has_many by default';
ok ((not try { exists $rsobj3->result_source->relationship_info('loader_test4zes')->{attrs}{on_delete} }),
'has_many does not have on_delete');
ok ((not try { exists $rsobj3->result_source->relationship_info('loader_test4zes')->{attrs}{on_update} }),
'has_many does not have on_update');
ok ((not try { exists $rsobj3->result_source->relationship_info('loader_test4zes')->{attrs}{is_deferrable} }),
'has_many does not have is_deferrable');
my $default_on_clause = $self->{default_on_clause} || 'CASCADE';
my $default_on_delete_clause = $self->{default_on_delete_clause} || $default_on_clause;
is try { $rsobj4->result_source->relationship_info('fkid_singular')->{attrs}{on_delete} },
$default_on_delete_clause,
"on_delete is $default_on_delete_clause on belongs_to by default";
my $default_on_update_clause = $self->{default_on_update_clause} || $default_on_clause;
is try { $rsobj4->result_source->relationship_info('fkid_singular')->{attrs}{on_update} },
$default_on_update_clause,
"on_update is $default_on_update_clause on belongs_to by default";
my $default_is_deferrable = $self->{default_is_deferrable};
$default_is_deferrable = 1
if not defined $default_is_deferrable;
is try { $rsobj4->result_source->relationship_info('fkid_singular')->{attrs}{is_deferrable} },
$default_is_deferrable,
"is_deferrable => $default_is_deferrable on belongs_to by default";
ok ((not try { exists $rsobj4->result_source->relationship_info('fkid_singular')->{attrs}{cascade_delete} }),
'belongs_to does not have cascade_delete');
ok ((not try { exists $rsobj4->result_source->relationship_info('fkid_singular')->{attrs}{cascade_copy} }),
'belongs_to does not have cascade_copy');
is try { $rsobj27->result_source->relationship_info('loader_test28')->{attrs}{cascade_delete} }, 0,
'cascade_delete => 0 on might_have by default';
is try { $rsobj27->result_source->relationship_info('loader_test28')->{attrs}{cascade_copy} }, 0,
'cascade_copy => 0 on might_have by default';
ok ((not try { exists $rsobj27->result_source->relationship_info('loader_test28')->{attrs}{on_delete} }),
'might_have does not have on_delete');
ok ((not try { exists $rsobj27->result_source->relationship_info('loader_test28')->{attrs}{on_update} }),
'might_have does not have on_update');
ok ((not try { exists $rsobj27->result_source->relationship_info('loader_test28')->{attrs}{is_deferrable} }),
'might_have does not have is_deferrable');
# find on multi-col pk
if ($conn->loader->preserve_case) {
my $obj5 = $rsobj5->find({id1 => 1, iD2 => 1});
is $obj5->i_d2, 1, 'Find on multi-col PK';
}
else {
my $obj5 = $rsobj5->find({id1 => 1, id2 => 1});
is $obj5->id2, 1, 'Find on multi-col PK';
}
# mulit-col fk def
my $obj6 = try { $rsobj6->find(1) } || $rsobj6->search({ id => 1 })->single;
isa_ok( try { $obj6->loader_test2 }, $class2);
isa_ok( try { $obj6->loader_test5 }, $class5);
ok($class6->column_info('loader_test2_id')->{is_foreign_key}, 'Foreign key detected');
ok($class6->column_info('id')->{is_foreign_key}, 'Foreign key detected');
my $id2_info = try { $class6->column_info('id2') } ||
$class6->column_info('Id2');
ok($id2_info->{is_foreign_key}, 'Foreign key detected');
unlike slurp_file $conn->_loader->get_dump_filename($class6),
qr{
\n__PACKAGE__->(?:belongs_to|has_many|might_have|has_one|many_to_many)\(
\s+ "(\w+?)"
.*?
\n__PACKAGE__->(?:belongs_to|has_many|might_have|has_one|many_to_many)\(
\s+ "\1"
}xs,
'did not create two relationships with the same name';
unlike slurp_file $conn->_loader->get_dump_filename($class8),
qr{
\n__PACKAGE__->(?:belongs_to|has_many|might_have|has_one|many_to_many)\(
\s+ "(\w+?)"
.*?
\n__PACKAGE__->(?:belongs_to|has_many|might_have|has_one|many_to_many)\(
\s+ "\1"
}xs,
'did not create two relationships with the same name';
# check naming of ambiguous relationships
my $rel_info = $class6->relationship_info('lovely_loader_test7') || {};
ok (($class6->has_relationship('lovely_loader_test7')
&& $rel_info->{cond}{'foreign.lovely_loader_test6'} eq 'self.id'
&& $rel_info->{class} eq $class7
&& $rel_info->{attrs}{accessor} eq 'single'),
'ambiguous relationship named correctly');
$rel_info = $class8->relationship_info('active_loader_test16') || {};
ok (($class8->has_relationship('active_loader_test16')
&& $rel_info->{cond}{'foreign.loader_test8_id'} eq 'self.id'
&& $rel_info->{class} eq $class16
&& $rel_info->{attrs}{accessor} eq 'single'),
'ambiguous relationship named correctly');
# fk that references a non-pk key (UNIQUE)
my $obj8 = try { $rsobj8->find(1) } || $rsobj8->search({ id => 1 })->single;
isa_ok( try { $obj8->loader_test7 }, $class7);
ok($class8->column_info('loader_test7')->{is_foreign_key}, 'Foreign key detected');
# test double-fk 17 ->-> 16
my $obj17 = try { $rsobj17->find(33) } || $rsobj17->search({ id => 33 })->single;
my $rs_rel16_one = try { $obj17->loader16_one };
isa_ok($rs_rel16_one, $class16);
is(try { $rs_rel16_one->dat }, 'y16', "Multiple FKs to same table");
ok($class17->column_info('loader16_one')->{is_foreign_key}, 'Foreign key detected');
my $rs_rel16_two = try { $obj17->loader16_two };
isa_ok($rs_rel16_two, $class16);
is(try { $rs_rel16_two->dat }, 'z16', "Multiple FKs to same table");
ok($class17->column_info('loader16_two')->{is_foreign_key}, 'Foreign key detected');
my $obj16 = try { $rsobj16->find(2) } || $rsobj16->search({ id => 2 })->single;
my $rs_rel17 = try { $obj16->search_related('loader_test17_loader16_ones') };
isa_ok(try { $rs_rel17->single }, $class17);
is(try { $rs_rel17->single->id }, 3, "search_related with multiple FKs from same table");
# test many_to_many detection 18 -> 20 -> 19 and 19 -> 20 -> 18
ok($class20->column_info('parent')->{is_foreign_key}, 'Foreign key detected');
ok($class20->column_info('child')->{is_foreign_key}, 'Foreign key detected');
cmp_deeply(
$class18->_m2m_metadata->{children},
superhashof({
relation => 'loader_test20s',
foreign_relation => 'child',
attrs => superhashof({ order_by => 'me.id' })
}),
'children m2m correct with ordering'
);
cmp_deeply(
$class19->_m2m_metadata->{parents},
superhashof({
relation => 'loader_test20s',
foreign_relation => 'parent',
attrs => superhashof({ order_by => 'me.id' })
}),
'parents m2m correct with ordering'
);
# test double-fk m:m 21 <- 22 -> 21
ok($class22->column_info('parent')->{is_foreign_key}, 'Foreign key detected');
ok($class22->column_info('child')->{is_foreign_key}, 'Foreign key detected');
is_deeply(
$class21->relationship_info("loader_test22_parents")->{cond},
{ 'foreign.parent' => 'self.id' },
'rel to foreign.parent correct'
);
is_deeply(
$class21->relationship_info("loader_test22_children")->{cond},
{ 'foreign.child' => 'self.id' },
'rel to foreign.child correct'
);
cmp_deeply(
$class21->_m2m_metadata,
{
parents => superhashof({
accessor => 'parents',