Skip to content

Commit

Permalink
#37 Reuse test documents between weighted search tests
Browse files Browse the repository at this point in the history
  • Loading branch information
extracts committed Nov 23, 2023
1 parent c78afc7 commit 66c06a1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
49 changes: 29 additions & 20 deletions test/Solr/Solarium/AdapterSearchingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
namespace OpusTest\Search\Solr\Solarium;

use Exception;
use Opus\Common\Document;
use Opus\Common\Person;
use Opus\Search\Query;
use Opus\Search\QueryFactory;
Expand All @@ -48,6 +47,30 @@

class AdapterSearchingTest extends DocumentBasedTestCase
{
/** @var array[] */
protected static $additionalDocumentPropertySets = [
'weightedTestDocA' => [
'TitleMain' => [
'Value' => 'Some Document',
'Language' => 'eng',
],
'TitleAbstract' => [
'Value' => 'Abstract of test document A.\nSome more text.',
'Language' => 'eng',
],
],
'weightedTestDocB' => [
'TitleMain' => [
'Value' => 'Another Test Document',
'Language' => 'eng',
],
'TitleAbstract' => [
'Value' => 'Abstract of document B.\nSome blah blah text.',
'Language' => 'eng',
],
],
];

public function testService()
{
$search = Service::selectSearchingService(null, 'solr');
Expand Down Expand Up @@ -253,15 +276,8 @@ public function testMapYearFacetIndexFieldsToYearAsset()

public function testWeightedSearch()
{
$docA = Document::new();
$docA->addTitleMain()->setLanguage("eng")->setValue("Some Document");
$docA->addTitleAbstract()->setLanguage("eng")->setValue("Abstract of test document A.\nSome more text.");
$docA->store();

$docB = Document::new();
$docB->addTitleMain()->setLanguage("eng")->setValue("Another Test Document");
$docB->addTitleAbstract()->setLanguage("eng")->setValue("Abstract of document B.\nSome blah blah text.");
$docB->store();
$docA = $this->createDocument('weightedTestDocA'); // 'Some Document'
$docB = $this->createDocument('weightedTestDocB'); // 'Another Test Document'

$index = Service::selectIndexingService(null, 'solr');
$index->addDocumentsToIndex([$docA, $docB]);
Expand Down Expand Up @@ -310,15 +326,8 @@ public function testWeightedSearch()

public function testWeightedSearchWithEqualWeights()
{
$docA = Document::new();
$docA->addTitleMain()->setLanguage("eng")->setValue("Some Document");
$docA->addTitleAbstract()->setLanguage("eng")->setValue("Abstract of test document A.\nSome more text.");
$docA->store();

$docB = Document::new();
$docB->addTitleMain()->setLanguage("eng")->setValue("Another Test Document");
$docB->addTitleAbstract()->setLanguage("eng")->setValue("Abstract of document B.\nSome blah blah text.");
$docB->store();
$docA = $this->createDocument('weightedTestDocA');
$docB = $this->createDocument('weightedTestDocB');

$index = Service::selectIndexingService(null, 'solr');
$index->addDocumentsToIndex([$docA, $docB]);
Expand All @@ -342,7 +351,7 @@ public function testWeightedSearchWithEqualWeights()

$this->assertTrue(abs($matches[0]->getScore() - $matches[1]->getScore()) < 1.0);

// 2. with equal boost factors assigned to fields, expect roughly equal scores
// 2. with equal boost factors, expect roughly equal scores
$query->setWeightedFields(['abstract' => 1.0, 'title' => 1.0]);

$result = $search->customSearch($query);
Expand Down
16 changes: 11 additions & 5 deletions test/TestAsset/DocumentBasedTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use ReflectionClass;

use function array_key_exists;
use function array_merge;
use function array_values;
use function basename;
use function file_get_contents;
Expand Down Expand Up @@ -155,12 +156,15 @@ class DocumentBasedTestCase extends TestCase
],
];

/** @var array[] */
protected static $additionalDocumentPropertySets;

/**
* @return array
*/
public static function documentPropertiesProvider()
{
return self::$documentPropertySets;
return array_merge(static::$documentPropertySets, static::$additionalDocumentPropertySets ?? []);
}

/**
Expand All @@ -169,11 +173,13 @@ public static function documentPropertiesProvider()
*/
public static function getDocumentDescriptionByName($name)
{
if (! array_key_exists($name, self::$documentPropertySets)) {
$documentPropertySets = self::documentPropertiesProvider();

if (! array_key_exists($name, $documentPropertySets)) {
throw new InvalidArgumentException("unknown document description");
}

return self::$documentPropertySets[$name];
return $documentPropertySets[$name];
}

/**
Expand All @@ -187,9 +193,9 @@ public static function getDocumentDescriptionByName($name)
protected function createDocument($documentProperties = null)
{
if ($documentProperties === null) {
$documentProperties = self::$documentPropertySets['article'];
$documentProperties = self::getDocumentDescriptionByName('article');
} if (is_string($documentProperties)) {
$documentProperties = self::$documentPropertySets[$documentProperties];
$documentProperties = self::getDocumentDescriptionByName($documentProperties);
}

$document = Document::new();
Expand Down

0 comments on commit 66c06a1

Please sign in to comment.