Skip to content

Commit

Permalink
Add units & functionals test on composite aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
itkg-ppottie committed Oct 1, 2020
1 parent d0390a1 commit b4c5cb2
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 3 deletions.
3 changes: 0 additions & 3 deletions src/Aggregation/Composite.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
class Composite extends AbstractAggregation
{
/**
* @param int $size
* @return $this
*/
public function setSize(int $size): self
Expand All @@ -14,7 +13,6 @@ public function setSize(int $size): self
}

/**
* @param AbstractAggregation $aggregation
* @return $this
*/
public function addSource(AbstractAggregation $aggregation): self
Expand All @@ -23,7 +21,6 @@ public function addSource(AbstractAggregation $aggregation): self
}

/**
* @param array|null $checkpoint
* @return $this
*/
public function addAfter(?array $checkpoint): self
Expand Down
131 changes: 131 additions & 0 deletions tests/Aggregation/CompositeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace Elastica\Test\Aggregation;

use Elastica\Aggregation\Composite;
use Elastica\Aggregation\Terms;
use Elastica\Document;
use Elastica\Index;
use Elastica\Query;

/**
* @internal
*/
class CompositeTest extends BaseAggregationTest
{
/**
* @group unit
*/
public function testSize(): void
{
$composite = new Composite('products');
$composite->setSize(200);
$this->assertEquals(200, $composite->getParam('size'));

$expected = [
'composite' => [
'size' => 200,
],
];
$this->assertEquals($expected, $composite->toArray());
}

/**
* @group unit
*/
public function testAddSource(): void
{
$expected = [
'composite' => [
'sources' => [
[
'product' => [
'terms' => [
'field' => 'product_id',
],
],
],
],
],
];

$composite = new Composite('products');
$composite->addSource((new Terms('product_id'))->setField('product_id')->setName('product'));
$this->assertEquals($expected, $composite->toArray());
}

/**
* @group unit
*/
public function testAddAfter(): void
{
$checkpoint = ['checkpointproduct' => 'checkpoint'];
$expected = [
'composite' => [
'after' => $checkpoint,
],
];

$composite = new Composite('products');
$composite->addAfter($checkpoint);
$this->assertEquals($expected, $composite->toArray());
}

/**
* @group functional
*/
public function testCompositeNoAfterAggregation(): void
{
$composite = new Composite('products');
$composite->setSize(5);
$composite->addSource((new Terms('color'))->setField('color.keyword')->setName('color'));

$query = new Query();
$query->addAggregation($composite);

$results = $this->_getIndexForTest()->search($query)->getAggregation('products');
$expected = [
'after_key' => [
'color' => 'red',
],
'buckets' => [
[
'key' => [
'color' => 'blue',
],
'doc_count' => 2,
],
[
'key' => [
'color' => 'green',
],
'doc_count' => 1,
],
[
'key' => [
'color' => 'red',
],
'doc_count' => 1,
],
],
];

$this->assertEquals($expected, $results);
}

protected function _getIndexForTest(): Index
{
$index = $this->_createIndex();

$index->addDocuments([
new Document(1, ['price' => 5, 'color' => 'blue']),
new Document(2, ['price' => 5, 'color' => 'blue']),
new Document(3, ['price' => 3, 'color' => 'red']),
new Document(4, ['price' => 3, 'color' => 'green']),
]);

$index->refresh();

return $index;
}
}

0 comments on commit b4c5cb2

Please sign in to comment.