Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 9443fef3

Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt

  • ID 9443fef35c0b8627d3690526de7df7eb49ff1ca0
  • Vorgänger a5c7ef23
  • Nachfolger 013b538f

Test für Konsistenz SL/DB/*.pm <=> SL/DB/MetaSetup/*.pm sowie die Helfer ALL und Mappings

Unterschiede anzeigen:

t/rdbo_consistency.t
1
use Test::More;
2
use Test::Exception;
3

  
4
use strict;
5

  
6
use lib 't';
7
use utf8;
8

  
9
use Data::Dumper;
10
use File::Basename;
11
use File::Slurp;
12
use IO::Dir;
13
use SL::Util qw(snakify);
14

  
15
sub find_pms {
16
  my %dir;
17
  tie %dir, 'IO::Dir', $_[0];
18
  return sort grep { m/\.pm$/ } keys %dir;
19
}
20

  
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 $all_content       = read_file('SL/DB/Helper/ALL.pm');
27
my $mapping_content   = read_file('SL/DB/Helper/Mappings.pm');
28

  
29
sub test_db_has_metasetup {
30
  foreach my $pm (@metasetups) {
31
    my $base = basename($pm);
32
    is(-f "SL/DB/MetaSetup/${base}" ? 1 : 0, $no_metasetup_ok{$base} ? 0 : 1, "$pm has entry in SL/DB/MetaSetup");
33
  }
34
}
35

  
36
sub test_metasetup_has_db {
37
  foreach my $pm (@metasetups) {
38
    my $base = basename($pm);
39
    is(-f "SL/DB/${base}" ? 1 : 0, $no_db_ok{$base} ? 0 : 1, "$pm has entry in SL/DB");
40
  }
41
}
42

  
43
sub test_db_included_in_all {
44
  foreach my $pm (@dbs) {
45
    my $base = basename($pm, '.pm');
46
    ok($all_content =~ m/\nuse\s+SL::DB::${base};/, "$pm has entry in SL::DB::Helper::ALL");
47
  }
48
}
49

  
50
sub test_use_in_all_exists_as_db {
51
  foreach my $package (map { m/^use\s+(.+?);/; $1 } grep { '^use SL::DB::' } split m/\n/, $all_content) {
52
    next unless $package =~ m/^SL::DB::(.+)/;
53
    my $file = $1;
54
    $file    =~ s{::}{/}g;
55
    ok(-f "SL/DB/${file}.pm", "'use $package' has entry in SL/DB");
56
  }
57
}
58

  
59
sub test_metasetup_has_table_to_class_mapping {
60
  my ($package_name_mapping) = $mapping_content =~ m/my\s*\%kivitendo_package_names\s*=\s*\((.+?)\n\)/s;
61
  ok($package_name_mapping, "found kivitendo_package_names in SL/DB/Helper/Mappings.pm");
62
  return unless $package_name_mapping;
63

  
64
  foreach my $pm (@metasetups) {
65
    my ($table) = $metasetup_content{$pm} =~ m{\n__PACKAGE__->meta->table\('(.+?)'\)};
66
    ok($table, "$pm has table setup");
67
    next unless $table;
68

  
69
    my ($schema) = $metasetup_content{$pm} =~ m{\n__PACKAGE__->meta->schema\('(.*?)'\)};
70
    $table       = "${schema}.${table}" if $schema;
71
    ok(!$schema || ($schema =~ m{^(?:auth|tax)$}), "$pm has either no schema or a known one");
72

  
73
    my $model               = basename($pm, '.pm');
74
    my $snaked_model        = snakify($model);
75
    my $maps_table_to_class = $package_name_mapping =~ m{\b'?\Q${table}\E'?\s*=>\s*\'(?:${snaked_model}|${model})\'};
76
    ok($maps_table_to_class, "$pm has mapping from table ${table} to class ${snaked_model} or ${model} in \%kivitendo_package_names");
77
  }
78
}
79

  
80
test_db_has_metasetup();
81
test_metasetup_has_db();
82
test_db_included_in_all();
83
test_use_in_all_exists_as_db();
84
test_metasetup_has_table_to_class_mapping();
85

  
86
done_testing();

Auch abrufbar als: Unified diff