1d061bc325700761a3c5daa3945ab1430b0192cf
[perl-cwmp.git] / t / 05-store.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 my $debug = shift @ARGV;
6
7 use Test::More tests => 47;
8 use Data::Dump qw/dump/;
9 use Cwd qw/abs_path/;
10 use lib 'lib';
11
12 #use Devel::LeakTrace::Fast;
13
14 BEGIN {
15         use_ok('CWMP::Store');
16         use_ok('CWMP::Store::DBMDeep');
17         use_ok('CWMP::Store::YAML');
18         use_ok('CWMP::Store::JSON');
19 }
20
21 ok(my $abs_path = abs_path($0), "abs_path");
22 $abs_path =~ s!/[^/]*$!/!;      #!fix-vim
23
24 my $path = "$abs_path/var/";
25
26 sub test_store {
27         my $module = shift;
28
29         diag "testing store plugin $module";
30
31         ok( my $store = CWMP::Store->new({
32                 debug => $debug,
33                 module => $module,
34                 path => $path,
35                 clean => 1,
36         }), 'new' );
37         isa_ok( $store, 'CWMP::Store' );
38
39         cmp_ok( $store->path, 'eq', $path, 'path' );
40
41         my $state = {
42                 foo => 'bar',
43                 DeviceID => {
44                         SerialNumber => 123456,
45                 },
46         };
47
48         cmp_ok( $store->state_to_uid( $state ), 'eq', 123456, 'state_to_uid' );
49
50         ok( $store->update_state( $state ), 'update_state new' );
51
52         ok( my $store_state = $store->get_state( 123456 ), 'get_state' );
53
54         isa_ok( $state, 'HASH' );
55         isa_ok( $store_state, 'HASH' );
56
57         if ( $debug ) {
58
59                 diag "store_state = ",dump( $store_state );
60         
61         }
62
63         is_deeply( $state, $store_state, 'state ID same as uid' );
64
65         ok( $store->update_state( {
66                 DeviceID => {
67                         SerialNumber => 123456,
68                 },
69                 baz => 12345 
70         } ), 'update_state existing' );
71
72         $state->{baz} = 12345;
73
74         is_deeply( $store->get_state( 123456 ), $state, 'get_state' );
75
76         is_deeply( [ $store->all_uids ], [ 123456 ], 'all_uids' );
77
78         ok( $store->update_state( { DeviceID => { SerialNumber => 99999 } } ), 'new device' );
79
80         is_deeply( [ $store->all_uids ], [ 123456, 99999 ], 'all_uids' );
81
82         undef $store;
83
84 }
85
86 # now test all stores
87
88 test_store('DBMDeep');
89 test_store('YAML');
90 test_store('JSON');
91