File: Catmandu-Store-File-Multi-Bag.t

package info (click to toggle)
libcatmandu-filestore-perl 1.16-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 732 kB
  • sloc: perl: 2,495; sh: 6; makefile: 2
file content (147 lines) | stat: -rw-r--r-- 3,197 bytes parent folder | download | duplicates (4)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Test::Exception;
use Catmandu::Store::File::Simple;
use Catmandu::Store::File::Multi;
use Path::Tiny;
use utf8;

my $pkg;

BEGIN {
    $pkg = 'Catmandu::Store::File::Multi::Bag';
    use_ok $pkg;
}

require_ok $pkg;

path("t/tmp/multi-bag/a")->mkpath;
path("t/tmp/multi-bag/b")->mkpath;

my $stores = [
    Catmandu::Store::File::Simple->new(
        root    => 't/tmp/multi-bag/a',
        keysize => 9
    ),
    Catmandu::Store::File::Simple->new(
        root    => 't/tmp/multi-bag/b',
        keysize => 9
    ),
];

my $store = Catmandu::Store::File::Multi->new(stores => $stores);
my $index = $store->index;

ok $store , 'got a store';
ok $index , 'got an index';

ok $index->add({_id => 7012}), 'adding bag `7012`';

my $bag = $store->bag('7012');

ok $bag , 'got bag(7012)';

note("add");
{
    my $n1 = $bag->upload(IO::File->new('t/data2/000/000/001/test.txt'),
        'test1.txt');

    ok $n1 , 'upload test1.txt';

    is $n1 , 16, '16 bytes';

    ok -f 't/tmp/multi-bag/a/000/007/012/test1.txt', 'test1.txt exists';

    ok -f 't/tmp/multi-bag/b/000/007/012/test1.txt', 'test1.txt exists';

    my $n2 = $bag->upload(IO::File->new('t/data2/000/000/002/test.txt'),
        'test2.txt');

    ok $n2 , 'upload test2.txt';

    is $n2 , 6, '6 bytes';

    ok -f 't/tmp/multi-bag/a/000/007/012/test2.txt', 'test2.txt exists';

    ok -f 't/tmp/multi-bag/b/000/007/012/test2.txt', 'test1.txt exists';

    my $n3 = $bag->upload(IO::File->new('t/data2/000/000/003/test.txt'),
        'test3.txt');

    ok $n3 , 'upload test3.txt';

    is $n3 , 6, '6 bytes';

    ok -f 't/tmp/multi-bag/a/000/007/012/test3.txt', 'test3.txt exists';

    ok -f 't/tmp/multi-bag/b/000/007/012/test3.txt', 'test1.txt exists';

    my $data = {
        _id     => 'test4.txt',
        _stream => IO::File->new('t/data2/000/000/003/test.txt')
    };

    ok $bag->add($data), 'add({ ..test4.. })';

    is $data->{size}, 6, '$data->{size}';
}

note("list");
{
    my $array = [sort @{$bag->map(sub {shift->{_id}})->to_array}];

    ok $array , 'list got a response';

    is_deeply $array , [qw(test1.txt test2.txt test3.txt test4.txt)],
        'got correct response';
}

note("exists");
{
    for (1 .. 4) {
        ok $bag->exists("test" . $_ . ".txt"), "exists(test" . $_ . ".txt)";
    }
}

note("get");
{
    for (1 .. 3) {
        ok $bag->get("test" . $_ . ".txt"), "get(test" . $_ . ".txt)";
    }

    my $file = $bag->get("test1.txt");

    my $str = $bag->as_string_utf8($file);

    ok $str , 'can stream the data';

    is $str , "钱唐湖春行\n", 'got the correct data';
}

note("delete");
{
    ok $bag->delete('test1.txt'), 'delete(test1.txt)';

    my $array = [sort @{$bag->map(sub {shift->{_id}})->to_array}];

    ok $array , 'list got a response';

    is_deeply $array , [qw(test2.txt test3.txt test4.txt)],
        'got correct response';
}

note("...delete_all (index)");
{
    lives_ok {$index->delete_all()} 'delete_all';

    my $array = $index->to_array;

    is_deeply $array , [], 'got correct response';
}

path("t/tmp/multi-bag/a")->remove_tree;
path("t/tmp/multi-bag/b")->remove_tree;

done_testing();