Skip to content

Commit

Permalink
Merge pull request #3435 from rbayet/feat-core-agg-builder-unit-tests
Browse files Browse the repository at this point in the history
[Core] Adding some adapter agg. building unit tests
  • Loading branch information
rbayet authored Nov 5, 2024
2 parents 8358bd2 + 8b620b2 commit e222b03
Show file tree
Hide file tree
Showing 10 changed files with 948 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function buildBucket(BucketInterface $bucket)
throw new \InvalidArgumentException("Query builder : invalid aggregation type {$bucket->getType()}.");
}

$params = [];

if (!empty($bucket->getSource())) {
$params['_source']['includes'] = $bucket->getSource();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?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->assertEquals('1d', $aggregation['date_histogram']['interval']);
$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,
'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']['interval']);
$this->assertEquals(10, $aggregation['date_histogram']['min_doc_count']);
$this->assertEquals(['min' => 2008, 'max' => 2050], $aggregation['date_histogram']['extended_bounds']);
}

/**
* 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();
}
}
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();
}
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?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\SignificantTerm as SignificantTermBuilder;
use Smile\ElasticsuiteCore\Search\Request\Aggregation\Bucket\SignificantTerm;
use Smile\ElasticsuiteCore\Search\Request\BucketInterface;

/**
* Search adapter significant term aggregation builder test case.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Richard BAYET <richard.bayet@smile.fr>
*/
class SignificantTermTest extends \PHPUnit\Framework\TestCase
{
/**
* Build a significant term aggregation from a bucket.
*/
public function testBasicAggregationBuild(): void
{
$aggBuilder = new SignificantTermBuilder();
$bucket = new SignificantTerm('aggregationName', 'fieldName');

$aggregation = $aggBuilder->buildBucket($bucket);

$this->assertArrayHasKey('significant_terms', $aggregation);
$this->assertEquals('fieldName', $aggregation['significant_terms']['field']);
$this->assertEquals(BucketInterface::MAX_BUCKET_SIZE, $aggregation['significant_terms']['size']);
$this->assertEquals(5, $aggregation['significant_terms']['min_doc_count']);
$this->assertArrayHasKey('gnd', $aggregation['significant_terms']);
}

/**
* Build a significant term aggregation from a bucket.
*/
public function testComplexAggregationBuild(): void
{
$aggBuilder = new SignificantTermBuilder();
$bucket = new SignificantTerm(
'aggregationName',
'fieldName',
[],
[],
[],
null,
null,
null,
12,
10,
SignificantTerm::ALGORITHM_PERCENTAGE,
);

$aggregation = $aggBuilder->buildBucket($bucket);

$this->assertArrayHasKey('significant_terms', $aggregation);
$this->assertEquals('fieldName', $aggregation['significant_terms']['field']);
$this->assertEquals(12, $aggregation['significant_terms']['size']);
$this->assertEquals(10, $aggregation['significant_terms']['min_doc_count']);
$this->assertArrayNotHasKey('gnd', $aggregation['significant_terms']);
$this->assertArrayHasKey('percentage', $aggregation['significant_terms']);
}

/**
* Test an exception is thrown when using the term aggs builder with another bucket type.
*/
public function testInvalidBucketAggregationBuild(): void
{
$aggBuilder = new SignificantTermBuilder();
$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);
}
}
Loading

0 comments on commit e222b03

Please sign in to comment.