ArangoDB

 view release on metacpan or  search on metacpan

t/05_query.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;
use JSON;

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,
};

init();

sub init {
    my $db = ArangoDB->new($config);
    map { $_->drop } @{ $db->collections };
    my $users = $db->collection('users');
    $users->save( { name => 'John Doe', age => 42 } );
    $users->save( { name => 'Foo',      age => 10 } );
    $users->save( { name => 'Bar',      age => 20 } );
    $users->save( { name => 'Baz',      age => 11 } );
}

subtest 'Normal statement' => sub {
    my $db  = ArangoDB->new($config);
    my $sth = $db->query('FOR u IN users SORT u.name ASC RETURN u');
    is "$sth", 'FOR u IN users SORT u.name ASC RETURN u';
    my $cur = $sth->execute();
    my @docs;
    while ( my $doc = $cur->next() ) {
        push @docs, $doc->content;
    }
    my $expects = [
        { name => 'Bar',      age => 20 },
        { name => 'Baz',      age => 11 },
        { name => 'Foo',      age => 10 },
        { name => 'John Doe', age => 42 },
    ];
    is_deeply( \@docs, $expects );

    like exception {
        my $guard = mock_guard(
            'ArangoDB::Connection' => {
                http_post => sub {die}
            }
        );
        $sth->execute();
    }, qr/^Failed to execute query/;

};

subtest 'Use bind var1' => sub {
    my $db  = ArangoDB->new($config);
    my $sth = $db->query('FOR u IN users FILTER u.age > @age SORT u.name ASC RETURN u');
    $sth->bind( age => 10 );
    is_deeply $sth->bind_vars, { age => 10 };
    is $sth->bind_vars('age'), 10;



( run in 1.113 second using v1.01-cache-2.11-cpan-54e63673c56 )