App-Prove-Plugin-Elasticsearch

 view release on metacpan or  search on metacpan

t/App-Prove-Elasticsearch-Indexer.t  view on Meta::CPAN

use strict;
use warnings;

use Test::More tests => 17;
use Test::Fatal;
use Test::Deep;
use Capture::Tiny qw{capture_merged};

use App::Prove::Elasticsearch::Indexer;

#check_index
{
    no warnings qw{redefine once};
    local *Search::Elasticsearch::new = sub { return bless({},'Search::Elasticsearch') };
    local *Search::Elasticsearch::indices = sub { return bless({},'Search::Elasticsearch::Indices') };
    local *Search::Elasticsearch::Indices::exists = sub { return 1};
    use warnings;

    like(exception { App::Prove::Elasticsearch::Indexer::check_index() }, qr/server must be specified/i,"Indexer dies in the event server & port  is not specified");
    like(exception { App::Prove::Elasticsearch::Indexer::check_index({ 'server.port' => 666 }) }, qr/server must be specified/i,"Indexer dies in the event server are not specified");
    like(exception { App::Prove::Elasticsearch::Indexer::check_index({ 'server.host' =>'zippy.test' }) }, qr/port must be specified/i,"Indexer dies in the event port is not specified");

    is(App::Prove::Elasticsearch::Indexer::check_index({ 'server.host' => 'zippy.test', 'server.port' => 666}),0,"Indexer skips indexing in the event index already exists.");

    no warnings qw{redefine once};
    local *Search::Elasticsearch::Indices::exists = sub { return 0 };
    local *Search::Elasticsearch::Indices::create = sub { };
    use warnings;

    is(App::Prove::Elasticsearch::Indexer::check_index({ 'server.host' => 'zippy.test', 'server.port' => 666 }),1,"Indexer runs in the event index nonexistant.");
}

#index_results
{
    $App::Prove::Elasticsearch::Indexer::e = undef;
    like( exception { App::Prove::Elasticsearch::Indexer::index_results({ name => 'zippy.test' }) }, qr/check_index must be run/, "index_results fails if check_index not run first");

    no warnings qw{redefine once};
    local *Search::Elasticsearch::new = sub { return bless({},'Search::Elasticsearch') };
    local *Search::Elasticsearch::index = sub { };
    local *Search::Elasticsearch::exists = sub { return 1};
    local *App::Prove::Elasticsearch::Utils::get_last_index = sub { return 0 };
    use warnings;

    $App::Prove::Elasticsearch::Indexer::e = bless({},'Search::Elasticsearch');
    is(App::Prove::Elasticsearch::Indexer::index_results({ name => 'zippy.test' }), 1, "index_result returns 1 in the event indexing succeeds.");

    no warnings qw{redefine once};
    local *Search::Elasticsearch::exists = sub { return 0 };
    use warnings;

    like( exception { App::Prove::Elasticsearch::Indexer::index_results({ name => 'zippy.test' }) }, qr/failed to index/i, "check_index dies in event of failure.");

}

# associate_case_with_result
{
    $App::Prove::Elasticsearch::Indexer::e = undef;
    like( exception { App::Prove::Elasticsearch::Indexer::associate_case_with_result() }, qr/check_index must be run/, "associate_case_with_result fails if check_index not run first");

    my %args = (
        platforms => ['clownOS', 'clownBrowser'],
        versions  => ['666.666'],
        defects   => ['YOLO-666'],
        case      => 'zippy.test',



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