forked from Smile-SA/elasticsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
998 additions
and
2 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
123 changes: 123 additions & 0 deletions
123
...e/Test/Unit/Search/Adapter/Elasticsuite/Request/Aggregation/Builder/DateHistogramTest.php
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,123 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade this module to newer versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCore | ||
* @author Richard BAYET <richard.bayet@smile.fr> | ||
* @copyright 2024 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
|
||
namespace Smile\ElasticsuiteCore\Test\Unit\Search\Adapter\Elasticsuite\Request\Aggregation\Builder; | ||
|
||
use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Aggregation\Builder\DateHistogram as HistogramBuilder; | ||
use Smile\ElasticsuiteCore\Search\Request\Aggregation\Bucket\DateHistogram as HistogramBucket; | ||
use Smile\ElasticsuiteCore\Search\Request\BucketInterface; | ||
|
||
/** | ||
* Search adapter date histogram aggregation builder test case. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCore | ||
* @author Richard BAYET <richard.bayet@smile.fr> | ||
*/ | ||
class DateHistogramTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* Build a histogram aggregation from a bucket. | ||
*/ | ||
public function testBasicAggregationBuild(): void | ||
{ | ||
$aggBuilder = $this->getHistogramAggregationBuilder(); | ||
$bucket = new HistogramBucket('aggregationName', 'fieldName'); | ||
|
||
$aggregation = $aggBuilder->buildBucket($bucket); | ||
|
||
$this->assertArrayHasKey('date_histogram', $aggregation); | ||
$this->assertEquals('fieldName', $aggregation['date_histogram']['field']); | ||
$this->assertArrayNotHasKey('interval', $aggregation['date_histogram']); | ||
$this->assertEquals('1d', $aggregation['date_histogram']['fixed_interval']); | ||
$this->assertArrayNotHasKey('calendar_interval', $aggregation['date_histogram']); | ||
$this->assertEquals(0, $aggregation['date_histogram']['min_doc_count']); | ||
} | ||
|
||
/** | ||
* Build a histogram aggregation from a bucket. | ||
*/ | ||
public function testComplexeAggregationBuild(): void | ||
{ | ||
$aggBuilder = $this->getHistogramAggregationBuilder(); | ||
$bucket = new HistogramBucket( | ||
'aggregationName', | ||
'fieldName', | ||
[], | ||
[], | ||
[], | ||
null, | ||
null, | ||
null, | ||
'1d', // Deprecated. | ||
null, | ||
'2y', | ||
10, | ||
['min' => 2008, 'max' => 2050] | ||
); | ||
|
||
$aggregation = $aggBuilder->buildBucket($bucket); | ||
|
||
$this->assertArrayHasKey('date_histogram', $aggregation); | ||
$this->assertEquals('fieldName', $aggregation['date_histogram']['field']); | ||
$this->assertEquals('2y', $aggregation['date_histogram']['fixed_interval']); | ||
$this->assertArrayNotHasKey('calendar_interval', $aggregation['date_histogram']); | ||
$this->assertEquals(10, $aggregation['date_histogram']['min_doc_count']); | ||
$this->assertEquals(['min' => 2008, 'max' => 2050], $aggregation['date_histogram']['extended_bounds']); | ||
|
||
$bucket = new HistogramBucket( | ||
'aggregationName', | ||
'fieldName', | ||
[], | ||
[], | ||
[], | ||
null, | ||
null, | ||
null, | ||
'1d', // Deprecated. | ||
'1y' | ||
); | ||
|
||
$aggregation = $aggBuilder->buildBucket($bucket); | ||
|
||
$this->assertArrayHasKey('date_histogram', $aggregation); | ||
$this->assertEquals('fieldName', $aggregation['date_histogram']['field']); | ||
$this->assertArrayNotHasKey('fixed_interval', $aggregation['date_histogram']); | ||
$this->assertEquals('1y', $aggregation['date_histogram']['calendar_interval']); | ||
$this->assertEquals(0, $aggregation['date_histogram']['min_doc_count']); | ||
$this->assertArrayNotHasKey('extended_bounds', $aggregation['date_histogram']); | ||
} | ||
|
||
/** | ||
* Test an exception is thrown when using the term aggs builder with another bucket type. | ||
*/ | ||
public function testInvalidBucketAggregationBuild(): void | ||
{ | ||
$aggBuilder = $this->getHistogramAggregationBuilder(); | ||
$this->expectExceptionMessage('Query builder : invalid aggregation type invalidType.'); | ||
$this->expectException(\InvalidArgumentException::class); | ||
$termsBucket = $this->getMockBuilder(BucketInterface::class)->getMock(); | ||
$termsBucket->method('getType')->willReturn('invalidType'); | ||
|
||
$aggBuilder->buildBucket($termsBucket); | ||
} | ||
/** | ||
* Get the histogram builder used in tests. | ||
* | ||
* @return HistogramBuilder | ||
*/ | ||
private function getHistogramAggregationBuilder() | ||
{ | ||
return new HistogramBuilder(); | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
...ite-core/Test/Unit/Search/Adapter/Elasticsuite/Request/Aggregation/Builder/MetricTest.php
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,135 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade this module to newer versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\Elasticsuite | ||
* @author Richard BAYET <richard.bayet@smile.fr> | ||
* @copyright 2024 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
|
||
namespace Smile\ElasticsuiteCore\Test\Unit\Search\Adapter\Elasticsuite\Request\Aggregation\Builder; | ||
|
||
use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Aggregation\Builder\Metric as MetricBuilder; | ||
use Smile\ElasticsuiteCore\Search\Request\Aggregation\Bucket\Metric as MetricBucket; | ||
use Smile\ElasticsuiteCore\Search\Request\BucketInterface; | ||
use Smile\ElasticsuiteCore\Search\Request\MetricInterface; | ||
use Smile\ElasticsuiteCore\Search\Request\SortOrderInterface; | ||
|
||
/** | ||
* Search adapter top level metrics aggregation builder test case. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCore | ||
* @author Richard BAYET <richard.bayet@smile.fr> | ||
*/ | ||
class MetricTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* Test the basic metric aggregation building. | ||
* | ||
* @return void | ||
*/ | ||
public function testBasicMetricAggregationBuild(): void | ||
{ | ||
$aggBuilder = $this->getAggregationBuilder(); | ||
$metricBucket = new MetricBucket( | ||
'aggregationName', | ||
'aggregationField' | ||
); | ||
|
||
$aggregation = $aggBuilder->buildBucket($metricBucket); | ||
$this->assertEquals(MetricInterface::TYPE_STATS, $metricBucket->getMetricType()); | ||
$this->assertArrayHasKey($metricBucket->getMetricType(), $aggregation); | ||
$this->assertArrayHasKey('field', $aggregation[$metricBucket->getMetricType()]); | ||
$this->assertEquals('aggregationField', $aggregation[$metricBucket->getMetricType()]['field']); | ||
} | ||
|
||
/** | ||
* Test the basic metric aggregation building. | ||
* | ||
* @return void | ||
*/ | ||
public function testBasicMetricConfigAggregationBuild(): void | ||
{ | ||
$aggBuilder = $this->getAggregationBuilder(); | ||
$metricBucket = new MetricBucket( | ||
'aggregationName', | ||
'aggregationField', | ||
[], | ||
[], | ||
[], | ||
null, | ||
null, | ||
null, | ||
MetricInterface::TYPE_PERCENTILES, | ||
['values' => [500, 600]] | ||
); | ||
|
||
$aggregation = $aggBuilder->buildBucket($metricBucket); | ||
$this->assertEquals(MetricInterface::TYPE_PERCENTILES, $metricBucket->getMetricType()); | ||
$this->assertArrayHasKey($metricBucket->getMetricType(), $aggregation); | ||
$this->assertArrayHasKey('field', $aggregation[$metricBucket->getMetricType()]); | ||
$this->assertEquals('aggregationField', $aggregation[$metricBucket->getMetricType()]['field']); | ||
$this->assertArrayHasKey('values', $aggregation[$metricBucket->getMetricType()]); | ||
$this->assertEquals([500, 600], $aggregation[$metricBucket->getMetricType()]['values']); | ||
} | ||
|
||
/** | ||
* Test the metric aggregation building with a script. | ||
* | ||
* @return void | ||
*/ | ||
public function testScriptMetricAggregationBuild(): void | ||
{ | ||
$aggBuilder = $this->getAggregationBuilder(); | ||
$metricBucket = new MetricBucket( | ||
'aggregationName', | ||
'aggregationField', | ||
[], | ||
[], | ||
[], | ||
null, | ||
null, | ||
null, | ||
MetricInterface::TYPE_AVG, | ||
['script' => '_score'] | ||
); | ||
|
||
$aggregation = $aggBuilder->buildBucket($metricBucket); | ||
$this->assertEquals(MetricInterface::TYPE_AVG, $metricBucket->getMetricType()); | ||
$this->assertArrayHasKey($metricBucket->getMetricType(), $aggregation); | ||
|
||
$this->assertArrayNotHasKey('field', $aggregation[$metricBucket->getMetricType()]); | ||
$this->assertArrayHasKey('script', $aggregation[$metricBucket->getMetricType()]); | ||
$this->assertEquals('_score', $aggregation[$metricBucket->getMetricType()]['script']); | ||
} | ||
|
||
/** | ||
* Test an exception is thrown when using the metric aggs builder with another bucket type. | ||
* | ||
* @return void | ||
*/ | ||
public function testInvalidMetricAggregationBuild(): void | ||
{ | ||
$this->expectExceptionMessage("Query builder : invalid aggregation type invalidType."); | ||
$this->expectException(\InvalidArgumentException::class); | ||
$metricBucket = $this->getMockBuilder(BucketInterface::class)->getMock(); | ||
$metricBucket->method('getType')->will($this->returnValue('invalidType')); | ||
|
||
$this->getAggregationBuilder()->buildBucket($metricBucket); | ||
} | ||
|
||
/** | ||
* Aggregation builder used in tests. | ||
* | ||
* @return MetricBuilder | ||
*/ | ||
private function getAggregationBuilder(): MetricBuilder | ||
{ | ||
return new MetricBuilder(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...e/Test/Unit/Search/Adapter/Elasticsuite/Request/Aggregation/Builder/ReverseNestedTest.php
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,56 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade this module to newer versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCore | ||
* @author Richard BAYET <richard.bayet@smile.fr> | ||
* @copyright 2024 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
|
||
namespace Smile\ElasticsuiteCore\Test\Unit\Search\Adapter\Elasticsuite\Request\Aggregation\Builder; | ||
|
||
use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Aggregation\Builder\ReverseNested as ReverseNestedBuilder; | ||
use Smile\ElasticsuiteCore\Search\Request\Aggregation\Bucket\ReverseNested; | ||
use Smile\ElasticsuiteCore\Search\Request\BucketInterface; | ||
|
||
/** | ||
* Search adapter reverse nested aggregation builder test case. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCore | ||
* @author Richard BAYET <richard.bayet@smile.fr> | ||
*/ | ||
class ReverseNestedTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* Build a reverse nested aggregation from a bucket. | ||
*/ | ||
public function testReverseNestedAggregationBuild(): void | ||
{ | ||
$aggBuilder = new ReverseNestedBuilder(); | ||
$bucket = new ReverseNested('aggregationName', 'fieldName'); | ||
|
||
$aggregation = $aggBuilder->buildBucket($bucket); | ||
|
||
$this->assertArrayHasKey('reverse_nested', $aggregation); | ||
$this->assertIsObject($aggregation['reverse_nested']); | ||
} | ||
|
||
/** | ||
* Test an exception is thrown when using the term aggs builder with another bucket type. | ||
*/ | ||
public function testInvalidBucketAggregationBuild(): void | ||
{ | ||
$aggBuilder = new ReverseNestedBuilder(); | ||
$this->expectExceptionMessage('Query builder : invalid aggregation type invalidType.'); | ||
$this->expectException(\InvalidArgumentException::class); | ||
$termsBucket = $this->getMockBuilder(BucketInterface::class)->getMock(); | ||
$termsBucket->method('getType')->willReturn('invalidType'); | ||
|
||
$aggBuilder->buildBucket($termsBucket); | ||
} | ||
} |
Oops, something went wrong.