Rough draft of low-level store mechanisam.
[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 => 18;
8 use Data::Dump qw/dump/;
9 use Cwd qw/abs_path/;
10 use lib 'lib';
11
12 BEGIN {
13         use_ok('CWMP::Store');
14         use_ok('CWMP::Store::DBMDeep');
15 }
16
17 ok(my $abs_path = abs_path($0), "abs_path");
18 $abs_path =~ s!/[^/]*$!/!;      #!fix-vim
19
20 my $path = "$abs_path/var/state.db";
21
22 unlink $path if -e $path;
23
24 ok( my $store = CWMP::Store->new({
25         debug => $debug,
26         module => 'DBMDeep',
27         path => $path,
28 }), 'new' );
29 isa_ok( $store, 'CWMP::Store' );
30
31 cmp_ok( $store->path, 'eq', $path, 'path' );
32
33 my $state = {
34         foo => 'bar',
35         DeviceID => {
36                 SerialNumber => 123456,
37         },
38 };
39
40 cmp_ok( $store->ID_to_uid( 42, $state ), 'eq', 123456, 'ID_to_uid' );
41
42 ok( $store->update_state( ID => 42, $state ), 'update_state new' );
43
44 ok( my $store_state = $store->state( ID => '42'), 'db->get' );
45
46 is_deeply( $store_state, $state, 'state ID' );
47
48 ok( $store_state = $store->state( uid =>  123456 ), 'db->get' );
49
50 is_deeply( $store_state, $state, 'state uid' );
51
52 ok( $store->update_state( ID => 42, { baz => 12345 } ), 'update_state existing' );
53
54 $state->{baz} = 12345;
55
56 is_deeply( $store->state( ID => 42 ), $state, 'store->state ID' );
57
58 is_deeply( $store->state( uid => 123456 ), $state, 'store->state uid' );
59
60 is_deeply( [ $store->known_CPE ], [ 123456 ], 'known_CPE' );
61
62 ok( $store->update_state( ID => 11, { DeviceID => { SerialNumber => 99999 } } ), 'new device' );
63
64 is_deeply( [ $store->known_CPE ], [ 123456, 99999 ], 'known_CPE' );
65