forked from ruflin/Elastica
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add units & functionals test on composite aggregation
- Loading branch information
1 parent
d0390a1
commit b4c5cb2
Showing
2 changed files
with
131 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |