File: rels.t

package info (click to toggle)
libsearch-gin-perl 0.11-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 404 kB
  • sloc: perl: 1,007; makefile: 28
file content (80 lines) | stat: -rw-r--r-- 1,417 bytes parent folder | download | duplicates (2)
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
use strict;
use warnings;

use Test::More 'no_plan';

use ok 'Search::GIN::Query::Class';
use ok 'Search::GIN::Extract::Class';

{
    package MyGIN;
    use Moose;

    with (
        qw(
            Search::GIN::Core
            Search::GIN::Driver::Hash
            Search::GIN::SelfIDs
        ),
    );

    sub extract_values {
        my ( $self, $obj, @args ) = @_;

        return $self->objects_to_ids(@{ $obj->friends });
    }

    package MyGIN::Query::Friends;
    use Moose;

    with qw(Search::GIN::Query);

    has friends => (
        isa => "ArrayRef[Person]",
        is  => "ro",
        required => 1,
    );

    sub extract_values {
        my ( $self, $gin ) = @_;

        return (
            values => [ $gin->objects_to_ids(@{ $self->friends }) ],
        );
    }

    sub consistent { 1 }

    package Person;
    use Moose;

    has friends => (
        isa => "ArrayRef[Person]",
        is  => "rw",
        default => sub { [] },
    );
}

my @people = map { Person->new } 0 .. 4;

$people[0]->friends([ @people[1,2] ]);

$people[1]->friends([ @people[0,2,4] ]);

$people[2]->friends([ @people[0,1] ]);

$people[4]->friends([ $people[1] ]);

my $gin = MyGIN->new;

$gin->insert(@people);

my $q = MyGIN::Query::Friends->new( friends => [ $people[1] ] );

my $res = $gin->query( $q );

is_deeply(
    [ sort $res->all ],
    [ sort @people[0, 2, 4] ],
    "friends of person 1",
);