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