13 use SL::Util qw(snakify);
17 tie %dir, 'IO::Dir', $_[0];
18 return sort grep { m/\.pm$/ } keys %dir;
21 my %no_db_ok = map { ($_ => 1) } qw();
22 my %no_metasetup_ok = map { ($_ => 1) } qw(Object.pm VC.pm);
23 my @dbs = find_pms('SL/DB');
24 my @metasetups = find_pms('SL/DB/MetaSetup');
25 my %metasetup_content = map { ($_ => scalar(read_file("SL/DB/MetaSetup/$_"))) } @metasetups;
26 my %db_content = map { ($_ => scalar(read_file("SL/DB/$_"))) } @dbs;
27 my $all_content = read_file('SL/DB/Helper/ALL.pm');
28 my $mapping_content = read_file('SL/DB/Helper/Mappings.pm');
30 sub test_db_has_metasetup {
31 foreach my $pm (@metasetups) {
32 my $base = basename($pm);
33 is(-f "SL/DB/MetaSetup/${base}" ? 1 : 0, $no_metasetup_ok{$base} ? 0 : 1, "$pm has entry in SL/DB/MetaSetup");
37 sub test_metasetup_has_db {
38 foreach my $pm (@metasetups) {
39 my $base = basename($pm);
40 is(-f "SL/DB/${base}" ? 1 : 0, $no_db_ok{$base} ? 0 : 1, "$pm has entry in SL/DB");
44 sub test_db_included_in_all {
45 foreach my $pm (@dbs) {
46 my $base = basename($pm, '.pm');
47 ok($all_content =~ m/\nuse\s+SL::DB::${base};/, "$pm has entry in SL::DB::Helper::ALL");
51 sub test_use_in_all_exists_as_db {
52 foreach my $package (map { m/^use\s+(.+?);/; $1 } grep { '^use SL::DB::' } split m/\n/, $all_content) {
54 next unless $package =~ m/^SL::DB::(.+)/;
57 ok(-f "SL/DB/${file}.pm", "'use $package' has entry in SL/DB");
61 sub test_metasetup_has_table_to_class_mapping {
62 my ($package_name_mapping) = $mapping_content =~ m/my\s*\%kivitendo_package_names\s*=\s*\((.+?)\n\)/s;
63 ok($package_name_mapping, "found kivitendo_package_names in SL/DB/Helper/Mappings.pm");
64 return unless $package_name_mapping;
66 foreach my $pm (@metasetups) {
67 my ($table) = $metasetup_content{$pm} =~ m{\n__PACKAGE__->meta->table\('(.+?)'\)};
68 ok($table, "$pm has table setup");
71 my ($schema) = $metasetup_content{$pm} =~ m{\n__PACKAGE__->meta->schema\('(.*?)'\)};
72 $table = "${schema}.${table}" if $schema;
73 ok(!$schema || ($schema =~ m{^(?:auth|tax)$}), "$pm has either no schema or a known one");
75 my $model = basename($pm, '.pm');
76 my $snaked_model = snakify($model);
77 my $maps_table_to_class = $package_name_mapping =~ m{\b'?\Q${table}\E'?\s*=>\s*\'(?:${snaked_model}|${model})\'};
78 ok($maps_table_to_class, "$pm has mapping from table ${table} to class ${snaked_model} or ${model} in \%kivitendo_package_names");
82 sub test_db_contains_meta_initialize {
83 foreach my $pm (grep { !m{^(?:Object|VC)\.pm$} } @dbs) {
84 ok($db_content{$pm} =~ m/\n__PACKAGE__->meta->initialize;/, "$pm contains __PACKAGE__->meta->initialize;");
88 test_db_has_metasetup();
89 test_metasetup_has_db();
90 test_db_included_in_all();
91 test_use_in_all_exists_as_db();
92 test_metasetup_has_table_to_class_mapping();
93 test_db_contains_meta_initialize();