r1399@llin: dpavlin | 2007-10-31 11:19:39 +0100
[webpac2] / t / 2-parse.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Test::More tests => 55;
5 use Test::Exception;
6 use blib;
7
8 use Data::Dump qw/dump/;
9 use Cwd qw/abs_path/;
10 use YAML qw/LoadFile/;
11
12 BEGIN {
13 use_ok( 'WebPAC::Parser' );
14 use_ok( 'WebPAC::Config' );
15 }
16
17 my $debug = shift @ARGV;
18
19 ok(my $abs_path = abs_path($0), "abs_path");
20 $abs_path =~ s#/[^/]*$#/#;
21
22 my $config_path = "$abs_path/conf/test.yml";
23
24 ok(-e $config_path, "$config_path exists");
25
26 throws_ok { new WebPAC::Parser( no_log => 1 ) } qr/WebPAC::Config/, "new without config";
27
28 ok(
29         my $parser = new WebPAC::Parser(
30                 config => new WebPAC::Config( path => $config_path ),
31                 base_path => $abs_path,
32                 debug => $debug,
33 ), "new");
34
35 my $inputs = {
36         foo => [ qw/foo-input1 foo-input2/ ],
37         bar => [ qw/bar-input/ ],
38         baz => [ qw/baz-input/ ],
39 };
40
41 foreach my $db (keys %$inputs) {
42         ok($parser->valid_database($db), "database $db");
43         ok($inputs->{$db}, "has known inputs");
44         diag "valid inputs = ", dump($inputs->{$db});
45         foreach my $i (@{ $inputs->{$db} }) {
46                 ok($parser->valid_database_input($db,$i), "input $i");
47         }
48 }
49 ok(! $parser->valid_database('non-existant'), "no database");
50 ok(! $parser->valid_database_input('foo','non-existant'), "no database input");
51
52 ok(my $l = $parser->{_lookup_create_key}, "_lookup_create_key");
53 ok($parser->{_lookup_create}, "_lookup_create");
54 diag "_lookup_create_key = ",dump($l) if ($debug);
55 foreach my $db (keys %$l) {
56         foreach my $i (keys %{$l->{$db}}) {
57                 ok(defined($parser->lookup_create_rules($db,$i)), "lookup_create_rules($db/$i)");
58                 my @keys = sort keys %{$l->{$db}->{$i}};
59                 ok(@keys, 'have keys');
60                 my @have_keys = sort $parser->have_lookup_create($db,$i);
61                 ok(@have_keys, 'have_lookup_create');
62                 ok(eq_array(\@have_keys, \@keys), "found all lookups");
63         }
64 }
65
66 ok(my $n = $parser->{_normalize_source}, "_normalize_source");
67 diag "_normalize_source = ",dump($n) if ($debug);
68 foreach my $db (keys %$n) {
69         foreach my $i (keys %{$n->{$db}}) {
70                 ok(my $r = $parser->normalize_rules($db,$i), "normalize_source($db/$i)");
71                 diag "normalize_rules($db,$i) = $r" if ($debug);
72                 cmp_ok($n->{$db}->{$i}, 'eq', $r, "same");
73         }
74 }
75
76 ok(my $d = $parser->{depends}, "depends");
77 diag "depends = ",dump($d) if ($debug);
78
79 my $expected_depend = {
80    foo => {
81             "foo-input1" => {
82                               bar => { "bar-input" => { "210-a-210-e" => 1, "220-a-220-e" => 1, "230-a-230-e" => 1 } },
83                               baz => { "baz-input" => { "200-a" => 1 } },
84                               foo => { "foo-input1" => { 11 => 1 }, "foo-input2" => { 11 => 1 } },
85                             },
86             "foo-input2" => {
87                               bar => { "bar-input" => { "900-x" => 1 } },
88                               baz => { "baz-input" => { "900-x" => 1 } },
89                               foo => { "foo-input1" => { "245-a" => 1 }, "foo-input2" => { "245-a" => 1 } },
90                             },
91           },
92    bar => {
93             "bar-input" => {
94                   baz => { "baz-input" => { "900-x" => 1 } },
95                   foo => { "foo-input1" => { "245-a" => 1 } },
96                 },
97           },
98    baz => {
99             "baz-input" => {
100                   bar => { "bar-input" => { "900-x" => 1 } },
101                   foo => { "foo-input2" => { "245-a" => 1 } },
102                 },
103           },
104 };
105
106
107 is_deeply($d, $expected_depend, "depends correct");
108
109 foreach my $db (keys %$d) {
110         foreach my $i (keys %{$d->{$db}}) {
111                 is_deeply($d->{$db}->{$i}, $parser->depends($db,$i), "depend $db/$i");
112         }
113 }
114
115 $config_path = "$abs_path/conf/marc.yml";
116
117 ok(-e $config_path, "$config_path exists");
118
119 ok(
120         my $parser = new WebPAC::Parser(
121                 config => new WebPAC::Config( path => $config_path ),
122                 base_path => $abs_path,
123                 debug => $debug,
124 ), "new");
125
126 ok(my $marc = $parser->have_rules('marc', 'marc', 'marc-input'), 'have_rules(marc,...)');
127
128 diag "marc: ",dump($marc) if ($debug);
129
130 is_deeply($marc, {
131         marc                                            => 1,
132         marc_compose                            => 1,
133         marc_duplicate                          => 1,
134         marc_indicators                         => 1,
135         marc_leader                                     => 1,
136         marc_original_order                     => 1,
137         marc_remove                                     => 1,
138         marc_repeatable_subfield        => 1,
139 }, 'catched all marc_*');
140