Fix thesaurus regression from f25bdf4171

- Field class is now immutable, mergeWith() returns a new instance
- Thesaurus roots are recursively merged
- In case of field merge, Structure class do not index the previous field
- Added failing test case for previous bugs
- Added merge tests on Field class
- Added tests for all indexed stuff inside Structure class
This commit is contained in:
Mathieu Darse
2015-06-19 21:17:01 +02:00
parent 1dd4bc5c5f
commit a74d0cd7bd
6 changed files with 291 additions and 40 deletions

View File

@@ -0,0 +1,108 @@
<?php
namespace Alchemy\Tests\Phrasea\SearchEngine;
use Alchemy\Phrasea\SearchEngine\Elastic\Mapping;
use Alchemy\Phrasea\SearchEngine\Elastic\Thesaurus\Concept;
use Alchemy\Phrasea\SearchEngine\Elastic\Structure\Field;
/**
* @group unit
* @group structure
*/
class FieldTest extends \PHPUnit_Framework_TestCase
{
public function testBasicMerge()
{
$field = new Field('foo', Mapping::TYPE_STRING);
$other = new Field('foo', Mapping::TYPE_STRING);
$merged = $field->mergeWith($other);
$this->assertInstanceOf(Field::class, $merged);
$this->assertNotSame($field, $merged);
$this->assertNotSame($other, $merged);
$this->assertEquals('foo', $merged->getName());
$this->assertEquals(Mapping::TYPE_STRING, $merged->getType());
$this->assertTrue($merged->isSearchable());
$this->assertFalse($merged->isPrivate());
$this->assertFalse($merged->isFacet());
$this->assertNull($merged->getThesaurusRoots());
}
/**
* @expectedException Alchemy\Phrasea\SearchEngine\Elastic\Exception\MergeException
* @expectedExceptionMessageRegExp #name#u
*/
public function testConflictingNameMerge()
{
$field = new Field('foo', Mapping::TYPE_STRING);
$other = new Field('bar', Mapping::TYPE_STRING);
$field->mergeWith($other);
}
/**
* @expectedException Alchemy\Phrasea\SearchEngine\Elastic\Exception\MergeException
* @expectedExceptionMessageRegExp #type#u
*/
public function testConflictingTypeMerge()
{
$field = new Field('foo', Mapping::TYPE_STRING);
$other = new Field('foo', Mapping::TYPE_DATE);
$field->mergeWith($other);
}
/**
* @expectedException Alchemy\Phrasea\SearchEngine\Elastic\Exception\MergeException
* @expectedExceptionMessageRegExp #search#u
*/
public function testMixedSearchabilityMerge()
{
$field = new Field('foo', Mapping::TYPE_STRING, ['searchable' => true]);
$other = new Field('foo', Mapping::TYPE_STRING, ['searchable' => false]);
$field->mergeWith($other);
}
/**
* @expectedException Alchemy\Phrasea\SearchEngine\Elastic\Exception\MergeException
* @expectedExceptionMessageRegExp #private#u
*/
public function testMixedPrivateAndPublicMerge()
{
$field = new Field('foo', Mapping::TYPE_STRING, ['private' => true]);
$other = new Field('foo', Mapping::TYPE_STRING, ['private' => false]);
$field->mergeWith($other);
}
/**
* @expectedException Alchemy\Phrasea\SearchEngine\Elastic\Exception\MergeException
* @expectedExceptionMessageRegExp #facet#u
*/
public function testMixedFacetEligibilityMerge()
{
$field = new Field('foo', Mapping::TYPE_STRING, ['facet' => true]);
$other = new Field('foo', Mapping::TYPE_STRING, ['facet' => false]);
$field->mergeWith($other);
}
public function testMergeWithThesaurusRoots()
{
$foo = new Concept('/foo');
$bar = new Concept('/bar');
$field = new Field('foo', Mapping::TYPE_STRING);
$other = new Field('foo', Mapping::TYPE_STRING, [
'thesaurus_roots' => [$foo, $bar]
]);
$merged = $field->mergeWith($other);
$this->assertEquals([$foo, $bar], $merged->getThesaurusRoots());
$foo = new Concept('/foo');
$bar = new Concept('/bar');
$field = new Field('foo', Mapping::TYPE_STRING, [
'thesaurus_roots' => [$foo]
]);
$other = new Field('foo', Mapping::TYPE_STRING, [
'thesaurus_roots' => [$bar]
]);
$merged = $field->mergeWith($other);
$this->assertEquals([$foo, $bar], $merged->getThesaurusRoots());
}
}

View File

@@ -3,27 +3,30 @@
namespace Alchemy\Tests\Phrasea\SearchEngine;
use Alchemy\Phrasea\SearchEngine\Elastic\Mapping;
use Alchemy\Phrasea\SearchEngine\Elastic\Thesaurus\Concept;
use Alchemy\Phrasea\SearchEngine\Elastic\Structure\Field;
use Alchemy\Phrasea\SearchEngine\Elastic\Structure\Structure;
/**
* @group unit
* @group structure
*/
class StructureTest extends \PHPUnit_Framework_TestCase
{
public function testFieldMerge()
public function testEmptiness()
{
$field = new Field('foo', Mapping::TYPE_STRING);
$other = new Field('foo', Mapping::TYPE_STRING);
$field->mergeWith($other);
$this->assertEquals('foo', $field->getName());
$this->assertEquals(Mapping::TYPE_STRING, $field->getType());
$this->assertTrue($field->isSearchable());
$this->assertFalse($field->isPrivate());
$this->assertFalse($field->isFacet());
$structure = new Structure();
$this->assertEmpty($structure->getAllFields());
$this->assertEmpty($structure->getUnrestrictedFields());
$this->assertEmpty($structure->getPrivateFields());
$this->assertEmpty($structure->getFacetFields());
$this->assertEmpty($structure->getThesaurusEnabledFields());
$this->assertEmpty($structure->getDateFields());
}
public function testFieldAdd()
{
$structure = new Structure();
$this->assertEmpty($structure->getAllFields());
$field = $this->prophesize(Field::class);
$field->getName()->willReturn('foo');
@@ -49,12 +52,32 @@ class StructureTest extends \PHPUnit_Framework_TestCase
$this->assertCount(1, $structure->getAllFields());
}
public function testFieldMerge()
{
$field = $this->prophesize(Field::class);
$field->getName()->willReturn('foo');
$field->getType()->willReturn(Mapping::TYPE_STRING);
$field->isPrivate()->willReturn(false);
$field->isFacet()->willReturn(false);
$field->hasConceptInference()->willReturn(false);
$other = new Field('foo', Mapping::TYPE_STRING);
$merged = new Field('foo', Mapping::TYPE_STRING);
$field->mergeWith($other)->shouldBeCalled()->willReturn($merged);
$structure = new Structure();
$structure->add($field->reveal());
$structure->add($other);
$this->assertEquals($merged, $structure->get('foo'));
}
public function testFieldsRestrictions()
{
$structure = new Structure();
$unrestricted_field = new Field('foo', Mapping::TYPE_STRING, true, false);
$unrestricted_field = new Field('foo', Mapping::TYPE_STRING, ['private' => false]);
$structure->add($unrestricted_field);
$private_field = new Field('bar', Mapping::TYPE_STRING, true, true);
$private_field = new Field('bar', Mapping::TYPE_STRING, ['private' => true]);
$structure->add($private_field);
// All
$all_fields = $structure->getAllFields();
@@ -70,6 +93,49 @@ class StructureTest extends \PHPUnit_Framework_TestCase
$this->assertNotContains($unrestricted_field, $private_fields);
}
public function testGetFacetFields()
{
$facet = new Field('foo', Mapping::TYPE_STRING, ['facet' => true]);
$not_facet = new Field('bar', Mapping::TYPE_STRING, ['facet' => false]);
$structure = new Structure();
$structure->add($facet);
$this->assertContains($facet, $structure->getFacetFields());
$structure->add($not_facet);
$facet_fields = $structure->getFacetFields();
$this->assertContains($facet, $facet_fields);
$this->assertNotContains($not_facet, $facet_fields);
}
public function testGetDateFields()
{
$string = new Field('foo', Mapping::TYPE_STRING);
$date = new Field('bar', Mapping::TYPE_DATE);
$structure = new Structure();
$structure->add($string);
$this->assertNotContains($string, $structure->getDateFields());
$structure->add($date);
$date_fields = $structure->getDateFields();
$this->assertContains($date, $date_fields);
$this->assertNotContains($string, $date_fields);
}
public function testGetThesaurusEnabledFields()
{
$not_enabled = new Field('foo', Mapping::TYPE_STRING, [
'thesaurus_roots' => null
]);
$enabled = new Field('bar', Mapping::TYPE_STRING, [
'thesaurus_roots' => [new Concept('/foo')]
]);
$structure = new Structure();
$structure->add($not_enabled);
$this->assertNotContains($not_enabled, $structure->getThesaurusEnabledFields());
$structure->add($enabled);
$enabled_fields = $structure->getThesaurusEnabledFields();
$this->assertContains($enabled, $enabled_fields);
$this->assertNotContains($not_enabled, $enabled_fields);
}
public function testGet()
{
$structure = new Structure();
@@ -90,11 +156,11 @@ class StructureTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(Mapping::TYPE_DOUBLE, $structure->typeOf('baz'));
}
public function testPrivateFieldCheck()
public function testPrivateCheck()
{
$structure = new Structure();
$structure->add(new Field('foo', Mapping::TYPE_STRING, true, false)); // Unrestricted field
$structure->add(new Field('bar', Mapping::TYPE_STRING, true, true)); // Private field
$structure->add(new Field('foo', Mapping::TYPE_STRING, ['private' => false]));
$structure->add(new Field('bar', Mapping::TYPE_STRING, ['private' => true]));
$this->assertFalse($structure->isPrivate('foo'));
$this->assertTrue($structure->isPrivate('bar'));
}