ArangoDB

 view release on metacpan or  search on metacpan

t/07_index.t  view on Meta::CPAN

use strict;
use Test::More;
use Test::Fatal qw(lives_ok dies_ok exception);
use Test::Mock::Guard;
use ArangoDB;

if ( !$ENV{TEST_ARANGODB_PORT} ) {
    plan skip_all => 'Can"t find port of arangod';
}

my $port   = $ENV{TEST_ARANGODB_PORT};
my $config = {
    host       => 'localhost',
    port       => $port,
    keep_alive => 1,
};

init();

sub init {
    my $db = ArangoDB->new($config);
    map { $_->drop } @{ $db->collections };
}

subtest 'hash index' => sub {
    my $db   = ArangoDB->new($config);
    my $coll = $db->create('index_test1');
    $coll->save( { foo => 1, bar => { a => 1, b => 1 } } );
    $coll->save( { foo => 2, bar => { a => 5, b => 1 } } );
    $coll->save( { foo => 3, bar => { a => 1, b => 10 } } );

    my $index1 = $coll->ensure_hash_index( [qw/bar.a/] );
    isa_ok $index1, 'ArangoDB::Index::Hash';
    is $index1->type, 'hash';
    is_deeply $index1->fields, [qw/bar.a/];
    is $index1->collection_id, $coll->id;

    like exception {
        $coll->ensure_hash_index( [] );
    }, qr/^Failed to create hash index on the collection/;
};

subtest 'unique hash index' => sub {
    my $db   = ArangoDB->new($config);
    my $coll = $db->create('index_test2');
    $coll->save( { foo => 1, bar => { a => 1, b => 1 } } );
    $coll->save( { foo => 2, bar => { a => 5, b => 1 } } );
    $coll->save( { foo => 3, bar => { a => 1, b => 10 } } );

    my $index1 = $coll->ensure_unique_constraint( [qw/foo/] );
    isa_ok $index1, 'ArangoDB::Index::Hash';
    is $index1->type, 'hash';
    is_deeply $index1->fields, [qw/foo/];

    like exception { $coll->save( { foo => 1 } ) }, qr/unique constraint violated/;

    like exception {
        $coll->ensure_unique_constraint( [qw/bar.a/] );
    }, qr/^Failed to create unique hash index on the collection/;
};

subtest 'skiplist index' => sub {
    my $db   = ArangoDB->new($config);
    my $coll = $db->create('index_test3');
    $coll->save( { foo => 1, } );
    $coll->save( { foo => 2, } );
    $coll->save( { foo => 3, } );
    $coll->save( { foo => 10, } );

    my $index1 = $coll->ensure_skiplist( [qw/foo/] );
    isa_ok $index1, 'ArangoDB::Index::SkipList';
    is $index1->type, 'skiplist';
    is_deeply $index1->fields, [qw/foo/];
    ok !$index1->unique;



( run in 0.798 second using v1.01-cache-2.11-cpan-df04353d9ac )