1 |
#!/usr/bin/perl -w |
#!/usr/bin/perl -w |
2 |
|
|
3 |
use Test::More tests => 29; |
use Test::More tests => 37; |
4 |
use Test::Exception; |
use Test::Exception; |
|
use Cwd qw/abs_path/; |
|
5 |
use blib; |
use blib; |
6 |
use File::Temp qw/tempdir/; |
use File::Temp qw/tempdir/; |
7 |
use strict; |
use strict; |
8 |
use Data::Dumper; |
use Data::Dump; |
9 |
|
|
10 |
BEGIN { |
BEGIN { |
11 |
use_ok( 'WebPAC::Store' ); |
use_ok( 'WebPAC::Store' ); |
12 |
} |
} |
13 |
|
|
|
ok(my $abs_path = abs_path($0), "abs_path"); |
|
|
$abs_path =~ s#/[^/]*$#/#; |
|
|
diag "abs_path: $abs_path"; |
|
|
|
|
14 |
my $db; |
my $db; |
15 |
my $debug = 1; |
my $debug = shift @ARGV; |
16 |
my $no_log = 1; # force no log output |
my $no_log = 1; # force no log output |
17 |
|
$no_log = 0 if ($debug); |
18 |
|
|
19 |
diag "NULL Store"; |
diag "NULL Store"; |
20 |
|
|
21 |
ok($db = new WebPAC::Store( debug => $debug ), "new"); |
ok(new WebPAC::Store(), 'new without database'); |
22 |
|
|
23 |
ok(! $db->path, "path"); |
ok($db = new WebPAC::Store({ database => 'foobar', debug => $debug }), "new"); |
24 |
|
|
25 |
ok(! $db->load_ds(), 'load_ds'); |
throws_ok { $db->load_ds() } qr/id/, 'load_ds without id'; |
26 |
ok(! $db->load_ds( id => 000 ), 'load_ds'); |
ok(! $db->load_ds( id => 000 ), 'load_ds'); |
27 |
|
|
28 |
ok(! $db->save_ds(), "save_ds"); |
throws_ok { $db->save_ds() } qr/id/, "save_ds without id"; |
29 |
ok(! $db->save_ds( id => 000 ), 'save_ds'); |
throws_ok { $db->save_ds( id => 000 ) } qr/ds/, 'save_ds without ds'; |
30 |
|
|
31 |
undef $db; |
undef $db; |
32 |
|
|
33 |
ok(my $path = tempdir( CLEANUP => 1 ), "path"); |
ok($db = new WebPAC::Store({ database => 'webpac-test', debug => $debug, no_log => $no_log }), "new"); |
|
|
|
|
diag "Store path: $path"; |
|
|
|
|
|
ok($db = new WebPAC::Store( path => $path, database => '.', debug => $debug, no_log => $no_log ), "new"); |
|
|
|
|
|
cmp_ok($db->{'path'}, 'eq', $path, "path"); |
|
|
|
|
|
ok(! $db->path(''), "path - disable caching"); |
|
|
|
|
|
ok(! defined($db->{'path'}), "no path"); |
|
|
|
|
|
ok($db->path( $path ), "path($path)"); |
|
34 |
|
|
35 |
cmp_ok($db->{'path'}, 'eq', $path, "path"); |
# |
36 |
|
# test *_ds |
37 |
|
# |
38 |
|
|
39 |
throws_ok { $db->load_ds() } qr/without id/, 'load_ds without arguments'; |
throws_ok { $db->load_ds() } qr/without id/, 'load_ds without arguments'; |
40 |
ok(! $db->load_ds( id => 000 ), 'load_ds'); |
ok(! $db->load_ds( id => 000 ), 'load_ds'); |
71 |
|
|
72 |
ok(! $db->load_ds( id => 42 ), "load_ds non-existing"); |
ok(! $db->load_ds( id => 42 ), "load_ds non-existing"); |
73 |
|
|
74 |
ok($db = new WebPAC::Store( path => $path, debug => $debug, no_log => $no_log ), "new without database"); |
ok($db = new WebPAC::Store({ database => 'webpac-test', debug => $debug, no_log => $no_log }), "new"); |
75 |
|
|
76 |
|
ok(! $db->load_ds( id => 1, input => 'foobar' ), "load_ds with invalid input"); |
77 |
|
|
78 |
|
ok(! $db->load_ds( id => 1, database => 'non-existant', ), "load_ds with unknown database"); |
79 |
|
|
80 |
|
ok($ds2 = $db->load_ds( id => 1, database => 'webpac-test' ), "load_ds"); |
81 |
|
|
82 |
|
# |
83 |
|
# test *_lookup |
84 |
|
# |
85 |
|
|
86 |
|
my $l = { |
87 |
|
foo => { 42 => 1 }, |
88 |
|
}; |
89 |
|
|
90 |
|
ok(! $db->load_lookup( input => 'non-existant', key => 'foo' ), 'invalid load_lookup'); |
91 |
|
|
92 |
|
ok($db->save_lookup( input => 'foo', key => 'bar', data => $l ), "save_lookup"); |
93 |
|
|
94 |
|
ok(-e $db->var_path( 'lookup', 'webpac-test', 'foo', 'bar'), "exists"); |
95 |
|
|
96 |
|
is_deeply($db->load_lookup( input => 'foo', key => 'bar' ), $l, 'load_lookup'); |
97 |
|
|
98 |
|
ok($db->save_lookup( database => 'baz', input => 'foo', key => 'bar', data => $l ), "save_lookup with database"); |
99 |
|
|
100 |
|
ok(-e $db->var_path( '/lookup','baz','foo','bar'), "exists"); |
101 |
|
|
102 |
|
is_deeply($db->load_lookup( database => 'baz', input => 'foo', key => 'bar' ), $l, 'load_lookup'); |
103 |
|
|
104 |
|
# |
105 |
|
# test *_row |
106 |
|
# |
107 |
|
|
108 |
|
my $row = { |
109 |
|
'000' => [ 42 ], |
110 |
|
'900' => [ qw/a foo b bar c baz/ ], |
111 |
|
}; |
112 |
|
|
113 |
|
ok(! $db->load_row( input => 'non-existant', id => 1234 ), 'invalid load_row'); |
114 |
|
|
115 |
|
ok($db->save_row( input => 'foo', id => 1234, row => $row ), "save_row"); |
116 |
|
|
117 |
|
ok(-e $db->var_path( 'row','webpac-test','foo',1234), "exists"); |
118 |
|
|
119 |
|
is_deeply($db->load_row( input => 'foo', id => 1234 ), $row, 'load_row'); |
120 |
|
|
121 |
|
ok($db->save_row( database => 'baz', input => 'foo', id => 1234, row => $row ), "save_row with database"); |
122 |
|
|
123 |
throws_ok { $ds2 = $db->load_ds( id => 1 ) } qr/database/, "load_ds without database"; |
ok(-e $db->var_path( 'row','baz','foo',1234), "exists"); |
124 |
|
|
125 |
ok($ds2 = $db->load_ds( id => 1, database => '.' ), "load_ds"); |
is_deeply($db->load_row( database => 'baz', input => 'foo', id => 1234 ), $row, 'load_row'); |
126 |
|
|
127 |
undef $db; |
undef $db; |
128 |
|
|