Skip to content

Commit

Permalink
Add test coverage for nested aggregation results
Browse files Browse the repository at this point in the history
  • Loading branch information
enrise-mbraam committed Sep 13, 2023
1 parent 104ff70 commit 7fc6e30
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions tests/Unit/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use JeroenG\Explorer\Infrastructure\Scout\ScoutSearchCommandBuilder;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use JeroenG\Explorer\Domain\Aggregations\NestedAggregation;

class FinderTest extends MockeryTestCase
{
Expand Down Expand Up @@ -362,6 +363,82 @@ public function test_it_adds_aggregates(): void
self::assertEquals('myKey', $specificAggregationValue['key']);
}

public function test_it_adds_nested_aggregations(): void
{
$client = Mockery::mock(Client::class);
$client->expects('search')
->with([
'index' => self::TEST_INDEX,
'body' => [
'query' => [
'bool' => [
'must' => [],
'should' => [],
'filter' => [],
],
],
'aggs' => [
'nestedAggregation' => [
'nested' => [
'path' => 'nestedAggregation',
],
'aggs' => [
'someField' => [
'terms' => [
'field' => 'nestedAggregation.someField',
'size' => 10,
],
],
],
],
],
],
])
->andReturn([
'hits' => [
'total' => ['value' => 1],
'hits' => [$this->hit()],
],
'aggregations' => [
'nestedAggregation' => [
'doc_count' => 42,
'someField' => [
'doc_count_error_upper_bound' => 0,
'sum_other_doc_count' => 0,
'buckets' => [
['key' => 'someKey', 'doc_count' => 6,]
],
],
],
],
]);

$query = Query::with(new BoolQuery());
$nestedAggregation = new NestedAggregation('nestedAggregation');
$nestedAggregation->add('someField', new TermsAggregation('nestedAggregation.someField'));
$query->addAggregation('nestedAggregation',$nestedAggregation);
$builder = new SearchCommand(self::TEST_INDEX, $query);
$builder->setIndex(self::TEST_INDEX);

$subject = new Finder($client, $builder);
$results = $subject->find();

self::assertCount(1, $results->aggregations());

$nestedAggregation = $results->aggregations()[0];

self::assertInstanceOf(AggregationResult::class, $nestedAggregation);
self::assertEquals('someField', $nestedAggregation->name());
self::assertCount(1, $nestedAggregation->values());

$nestedAggregationValue = $nestedAggregation->values()[0];


self::assertEquals(6, $nestedAggregationValue['doc_count']);
self::assertEquals('someKey', $nestedAggregationValue['key']);
}


private function hit(int $id = 1, float $score = 1.0): array
{
return [
Expand Down

0 comments on commit 7fc6e30

Please sign in to comment.