File: 01-bag.t

package info (click to toggle)
libcatmandu-solr-perl 0.0304-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 184 kB
  • sloc: perl: 837; makefile: 2
file content (150 lines) | stat: -rw-r--r-- 3,485 bytes parent folder | download
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
148
149
150
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Test::Exception;

require Catmandu::Store::Hash;
require Catmandu::Store::Solr;

my $solr_url       = $ENV{T_SOLR_URL};
my $solr_bag       = $ENV{T_SOLR_BAG} || "data";
my $solr_id_field  = $ENV{T_SOLR_ID_FIELD} || "_id";
my $solr_bag_field = $ENV{T_SOLR_BAG_FIELD} || "_bag";

unless (defined($solr_url) && $solr_url ne "") {

    plan skip_all => "no environment variable T_SOLR_URL found";

}
else {

    my $store;
    my $bag;

    lives_ok(
        sub {

            $store = Catmandu::Store::Solr->new(
                url       => $solr_url,
                id_field  => $solr_id_field,
                bag_field => $solr_bag_field
            );

        },
        "store created"
    );

    lives_ok(
        sub {

            $bag = $store->bag($solr_bag);

        },
        "bag created"
    );

    my $record = {_id => "test"};

    #delete all
    lives_ok(sub {$bag->delete_all; $bag->commit();}, "bag delete all");

    #bag add
    lives_ok(sub {$bag->add($record); $bag->commit();}, "bag add");

    #bag get
    my $new_record;
    lives_ok(sub {$new_record = $bag->get($record->{_id});}, "bag get");
    is_deeply($record, $new_record, "retrieved record equal");

    #bag delete
    lives_ok(sub {$bag->delete($record->{_id}); $bag->commit();},
        "bag delete");
    is($bag->get($record->{_id}), undef, "record deleted");

    #bag add_many
    my @records;
    for (1 .. 10) {
        push @records, {_id => "test-$_"};
    }
    lives_ok(sub {$bag->add_many(\@records); $bag->commit();},
        "bag add_many");

    my $num = 0;

    lives_ok(sub {$num = $bag->count();}, "bag count");

    is($num, scalar(@records), "bag count");

    #bag delete_all
    lives_ok(sub {$bag->delete_all(); $bag->commit();}, "bag delete");
    is($bag->count(), 0, "all records deleted");

    #transactions
    dies_ok(
        sub {
            $bag->store->transaction(
                sub {
                    $bag->add({_id => "a"});
                    $bag->add({_id => "b"});
                    die("failed");
                }
            );
        },
        "bag transaction must die"
    );

    is($bag->count(), 0, "bag transaction had no effect");

    #reify
    my $reify;
    lives_ok(sub {$reify = Catmandu::Store::Hash->new()->bag();},
        "reify created");

    #add many
    lives_ok(
        sub {
            for (1 .. 10) {
                $reify->add({_id => "test-$_", title => "test-$_"});
                $reify->commit();
            }
        },
        "added many records to reify"
    );

    #copy to solr without title
    lives_ok(
        sub {
            $bag->add_many(
                $reify->map(
                    sub {
                        +{_id => $_[0]->{_id}};
                    }
                )
            );
            $bag->commit();
        },
        "copied reify to solr bag"
    );

    #reify records
    my $reified_records;
    lives_ok(
        sub {
            $reified_records = $bag->searcher(fl => $solr_id_field)->map(
                sub {
                    $reify->get($_[0]->{_id});
                }
            )->to_array();
        },
        "retrieved reified records"
    );

    #compare
    my $from = [sort {$a->{_id} cmp $b->{_id}} @$reified_records];
    my $to   = [sort {$a->{_id} cmp $b->{_id}} @{$reify->to_array()}];
    is_deeply($from, $to, "all records equal");

    done_testing 20;

}