added test file
[webpac2] / t / 6-unit.t
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 use Test::More tests => 31;
6 use Test::Exception;
7 use Cwd qw/abs_path/;
8 use File::Temp qw/tempdir/;
9 use File::Slurp;
10 use Data::Dump qw/dump/;
11 use Time::HiRes qw/time/;
12 use lib 'lib';
13
14 my $debug = shift @ARGV;
15
16 #
17 # FIXME add lookup testing!
18 #
19
20 BEGIN {
21 use_ok( 'WebPAC::Input' );
22 use_ok( 'WebPAC::Store' );
23 use_ok( 'WebPAC::Normalize' );
24 use_ok( 'WebPAC::Output::TT' );
25 }
26
27 ok(my $abs_path = abs_path($0), "abs_path");
28 $abs_path =~ s#/[^/]*$#/#;
29 diag "abs_path: $abs_path" if ($debug);
30
31 my $isis_file = "$abs_path../t/winisis/BIBL";
32 #$isis_file = '/data/hidra/THS/THS';
33 #$isis_file = '/data/isis_data/ffkk/';
34
35 diag "isis_file: $isis_file" if ($debug);
36
37 my $normalize_set_pl = "$abs_path/data/normalize.pl";
38 my $lookup_file = "$abs_path../conf/lookup/isis.pm";
39
40 ok(my $isis = new WebPAC::Input(
41         module => 'WebPAC::Input::ISIS',
42         limit => 100,
43         no_progress_bar => 1,
44 ), "new Input::ISIS");
45
46 ok(my $maxmfn = $isis->open(
47         path => $isis_file,
48         input_encoding => 'cp852',              # database encoding
49         lookup_coderef => sub {
50                 my $rec = shift || return;
51                 ok($rec, 'lookup_coderef has rec');
52                 ok(defined($rec->{'000'}->[0]), 'have mfn');
53         },
54 ), "Input::ISIS->open");
55
56 ok(my $path = tempdir( CLEANUP => 1 ), "path");
57
58 ok(my $db = new WebPAC::Store({
59         database => '.',
60 }), "new Store");
61
62 ok(my $norm_pl = read_file( $normalize_set_pl ), "set definitions: $normalize_set_pl" );
63
64 ok(my $out = new WebPAC::Output::TT(
65         include_path => "$abs_path../conf/output/tt",
66         filters => { foo => sub { shift } },
67 ), "new Output::TT");
68
69 my $t_norm = 0;
70
71 foreach my $pos ( 0 ... $isis->size ) {
72
73         my $row = $isis->fetch || next;
74
75         diag " row $pos => ",dump($row) if ($debug);
76
77         my $t = time();
78         ok( my $ds = WebPAC::Normalize::data_structure(
79                 row => $row,
80                 rules => $norm_pl,
81         ), "Set data_structure");
82         $t_norm += time() - $t;
83
84         diag " ds $pos => ",dump($ds) if ($debug);
85
86         ok(my $html = $out->apply(
87                 template => 'html.tt',
88                 data => $ds,
89         ), "apply");
90
91         $html =~ s#\s*[\n\r]+\s*##gs;
92
93         #diag $html;
94
95 };
96
97 diag sprintf("timings: %.2fs\n", $t_norm);