Skip to content

Commit

Permalink
Merge pull request #3454 from rbayet/feat-add-a-few-unit-test-search
Browse files Browse the repository at this point in the history
[Core] Adding a few Search related unit tests
  • Loading branch information
rbayet authored Nov 26, 2024
2 parents 4aa8aaf + 2f73d1a commit 6b4eeda
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder as QueryBuilder;
use Smile\ElasticsuiteCore\Search\Request\SortOrder\Standard as StandardSortOrder;
use Smile\ElasticsuiteCore\Search\Request\SortOrder\Nested as NestedSortOrder;
use Smile\ElasticsuiteCore\Search\Request\SortOrder\Script as ScriptSortOrder;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;
use Smile\ElasticsuiteCore\Api\Index\Mapping\FieldInterface;

Expand Down Expand Up @@ -138,6 +139,50 @@ public function testNestedFilterSortOrder()
$this->assertEquals('query', $currentSortOrder['parent.child']['nested']['filter']);
}

/**
* Test building a script sort order (without support for direction yet).
*
* @return void
*/
public function testScriptSortOrder()
{
$scriptType = 'number';
$scriptLang = 'painless';
$scriptSource = "doc['sortField'].value * params.factor";
$scriptParams = ['factor' => 1.1];
$scriptSorOrder = new ScriptSortOrder(
$scriptType,
$scriptLang,
$scriptSource,
$scriptParams
);
$this->assertEquals(ScriptSortOrder::SCRIPT_FIELD, $scriptSorOrder->getField());
$this->assertEquals(
[
'lang' => $scriptLang,
'source' => $scriptSource,
'params' => $scriptParams,
],
$scriptSorOrder->getScript()
);

$sortOrders = $this->getSortOrderBuilder()->buildSortOrders([$scriptSorOrder]);

$currentSortOrder = current($sortOrders);
$this->assertIsArray($currentSortOrder);
$this->assertArrayHasKey($scriptSorOrder->getField(), $currentSortOrder);
$this->assertArrayHasKey('type', $currentSortOrder[ScriptSortOrder::SCRIPT_FIELD]);
$this->assertEquals($scriptSorOrder->getScriptType(), $currentSortOrder[ScriptSortOrder::SCRIPT_FIELD]['type']);
$this->assertArrayHasKey('script', $currentSortOrder[ScriptSortOrder::SCRIPT_FIELD]);
$this->assertIsArray($currentSortOrder[ScriptSortOrder::SCRIPT_FIELD]['script']);
$this->assertEquals(
$scriptSorOrder->getScript(),
$currentSortOrder[ScriptSortOrder::SCRIPT_FIELD]['script']
);
// 'order' not supported yet.
$this->assertArrayNotHasKey('order', $currentSortOrder[ScriptSortOrder::SCRIPT_FIELD]);
}

/**
* Init the sort order builder used in tests.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?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\Request;

use Smile\ElasticsuiteCore\Search\Request\SortOrder\Script as ScriptSortOrder;
use Smile\ElasticsuiteCore\Search\Request\SortOrderInterface;

/**
* Script sort order unit testing.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Richard BAYET <richard.bayet@smile.fr>
*/
class ScriptTest extends \PHPUnit\Framework\TestCase
{
/**
* Test basic script order.
*
* @return void
*/
public function testBasicScriptSortOrder()
{
$scriptType = 'number';
$scriptLang = 'painless';
$scriptSource = "doc['sortField'].value * params.factor";

$scriptSorOrder = new ScriptSortOrder($scriptType, $scriptLang, $scriptSource);

// Default values.
$this->assertNull($scriptSorOrder->getName());
$this->assertEquals(ScriptSortOrder::SCRIPT_FIELD, $scriptSorOrder->getField());
$this->assertEquals(SortOrderInterface::SORT_ASC, $scriptSorOrder->getDirection());
$this->assertEquals(ScriptSortOrder::TYPE_SCRIPT, $scriptSorOrder->getType());
$this->assertEquals(SortOrderInterface::MISSING_LAST, $scriptSorOrder->getMissing());

$this->assertEquals(
[
'lang' => $scriptLang,
'source' => $scriptSource,
'params' => [],
],
$scriptSorOrder->getScript(),
);
$this->assertEquals($scriptType, $scriptSorOrder->getScriptType());
}

/**
* Test script order with all params provided.
*
* @return void
*/
public function testAdvancedScriptSortOrder()
{
$scriptType = 'number';
$scriptLang = 'painless';
$scriptSource = "doc['sortField'].value * params.factor";
$scriptParams = ['factor' => 1.1];
$scriptDirection = SortOrderInterface::SORT_DESC;
$scriptName = 'mySortOrder';
$scriptMissing = SortOrderInterface::MISSING_FIRST;

$scriptSorOrder = new ScriptSortOrder(
$scriptType,
$scriptLang,
$scriptSource,
$scriptParams,
$scriptDirection,
$scriptName,
$scriptMissing
);

// Default values.
$this->assertEquals($scriptName, $scriptSorOrder->getName());
$this->assertEquals(ScriptSortOrder::SCRIPT_FIELD, $scriptSorOrder->getField());
$this->assertEquals(SortOrderInterface::SORT_DESC, $scriptSorOrder->getDirection());
$this->assertEquals(ScriptSortOrder::TYPE_SCRIPT, $scriptSorOrder->getType());
$this->assertEquals(SortOrderInterface::MISSING_FIRST, $scriptSorOrder->getMissing());

$this->assertEquals(
[
'lang' => $scriptLang,
'source' => $scriptSource,
'params' => $scriptParams,
],
$scriptSorOrder->getScript(),
);
$this->assertEquals($scriptType, $scriptSorOrder->getScriptType());
}
}

0 comments on commit 6b4eeda

Please sign in to comment.