File: Catmandu-Store-Hash.t

package info (click to toggle)
libcatmandu-perl 1.2024-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,552 kB
  • sloc: perl: 17,037; makefile: 24; sh: 1
file content (72 lines) | stat: -rw-r--r-- 1,707 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use Test::Exception;

my $pkg;

BEGIN {
    $pkg = 'Catmandu::Store::Hash';
    use_ok $pkg;
}
require_ok $pkg;

my $data = [
    {_id => '123', name => 'Patrick', age => '39'},
    {_id => '321', name => 'Nicolas', age => '34'},
];

my $store = $pkg->new();
can_ok $store, 'transaction';

my $bag = $store->bag;
my @method
    = qw(to_array each take add add_many count slice first rest any many all tap map reduce);
can_ok $bag, $_ for @method;

$store->transaction(
    sub {
        my $rec = $bag->get_or_add('1', {latest => '0'});
        ++$rec->{latest};
        $bag->add($rec);
    }
);
is_deeply $bag->first, {_id => 1, latest => 1}, "transaction ok";
$store->transaction(
    sub {
        my $rec = $bag->get_or_add('1', {latest => '0'});
        ++$rec->{latest};
        $bag->add($rec);
    }
);
is_deeply $bag->first, {_id => 1, latest => 2}, "transaction ok again";
$bag->drop;

$bag->add_many($data);
is $bag->count,   2, "Count bag size";
isnt $bag->count, 0, "Count bag size";

is_deeply $bag->first, {_id => '123', name => 'Patrick', age => '39'},
    "Data package ok.";
is_deeply $bag->rest->first, {_id => '321', name => 'Nicolas', age => '34'},
    "Data package ok.";

$bag->delete('123');
is_deeply $bag->first, {_id => '321', name => 'Nicolas', age => '34'},
    "Data package ok.";
is $bag->count, 1, "Count bag size";
$bag->delete_all;
is $bag->count,   0, "Count bag size";
isnt $bag->count, 1, "Count bag size";

$bag->add({_id => '123', foo => "bar"});

my $bag2 = $store->bag;
is $bag2->count, 1, "Bags stay alive";

my $bag3 = $store->bag('foo');
ok !$bag3->get('123'), "foo doesnt have 123";

done_testing;