Skip to content

Fix InArray filter for object items #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/PostgresDocumentStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ private function filterToWhereClause(Filter $filter, $argsCount = 0): array
case DocumentStore\Filter\InArrayFilter::class:
/** @var DocumentStore\Filter\InArrayFilter $filter */
$prop = $this->propToJsonPath($filter->prop());
return ["$prop @> :a$argsCount", ["a$argsCount" => $this->prepareVal($filter->val(), $filter->prop())], ++$argsCount];
return ["$prop @> :a$argsCount", ["a$argsCount" => '[' . $this->prepareVal($filter->val(), $filter->prop()) . ']'], ++$argsCount];
case DocumentStore\Filter\ExistsFilter::class:
/** @var DocumentStore\Filter\ExistsFilter $filter */
$prop = $this->propToJsonPath($filter->prop());
Expand Down
91 changes: 91 additions & 0 deletions tests/PostgresDocumentStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use EventEngine\DocumentStore\Filter\AnyOfDocIdFilter;
use EventEngine\DocumentStore\Filter\AnyOfFilter;
use EventEngine\DocumentStore\Filter\DocIdFilter;
use EventEngine\DocumentStore\Filter\InArrayFilter;
use EventEngine\DocumentStore\Filter\NotFilter;
use PHPUnit\Framework\TestCase;
use EventEngine\DocumentStore\FieldIndex;
Expand Down Expand Up @@ -305,6 +306,96 @@ public function it_handles_not_any_of_id_filter()
$this->assertEquals(['bat'], $vals);
}

/**
* @test
*/
public function it_handles_in_array_filter()
{
$collectionName = 'test_in_array_filter';
$this->documentStore->addCollection($collectionName);

$firstDocId = Uuid::uuid4()->toString();
$secondDocId = Uuid::uuid4()->toString();
$thirdDocId = Uuid::uuid4()->toString();

$this->documentStore->addDoc($collectionName, $firstDocId, ['foo' => ['bar' => ['tag1', 'tag2'], 'ref' => $firstDocId]]);
$this->documentStore->addDoc($collectionName, $secondDocId, ['foo' => ['bar' => ['tag2', 'tag3'], 'ref' => $secondDocId]]);
$this->documentStore->addDoc($collectionName, $thirdDocId, ['foo' => ['bar' => ['tag3', 'tag4'], 'ref' => $thirdDocId]]);

$filteredDocs = \iterator_to_array($this->documentStore->filterDocs(
$collectionName,
new InArrayFilter('foo.bar', 'tag3')
));

$this->assertCount(2, $filteredDocs);

$refs = array_map(function (array $doc) {
return $doc['foo']['ref'];
}, $filteredDocs);

$this->assertEquals([$secondDocId, $thirdDocId], $refs);
}

/**
* @test
*/
public function it_handles_not_in_array_filter()
{
$collectionName = 'test_not_in_array_filter';
$this->documentStore->addCollection($collectionName);

$firstDocId = Uuid::uuid4()->toString();
$secondDocId = Uuid::uuid4()->toString();
$thirdDocId = Uuid::uuid4()->toString();

$this->documentStore->addDoc($collectionName, $firstDocId, ['foo' => ['bar' => ['tag1', 'tag2'], 'ref' => $firstDocId]]);
$this->documentStore->addDoc($collectionName, $secondDocId, ['foo' => ['bar' => ['tag2', 'tag3'], 'ref' => $secondDocId]]);
$this->documentStore->addDoc($collectionName, $thirdDocId, ['foo' => ['bar' => ['tag3', 'tag4'], 'ref' => $thirdDocId]]);

$filteredDocs = \iterator_to_array($this->documentStore->filterDocs(
$collectionName,
new NotFilter(new InArrayFilter('foo.bar', 'tag3'))
));

$this->assertCount(1, $filteredDocs);

$refs = array_map(function (array $doc) {
return $doc['foo']['ref'];
}, $filteredDocs);

$this->assertEquals([$firstDocId], $refs);
}

/**
* @test
*/
public function it_handles_in_array_filter_with_object_items()
{
$collectionName = 'test_in_array_with_object_filter';
$this->documentStore->addCollection($collectionName);

$firstDocId = Uuid::uuid4()->toString();
$secondDocId = Uuid::uuid4()->toString();
$thirdDocId = Uuid::uuid4()->toString();

$this->documentStore->addDoc($collectionName, $firstDocId, ['foo' => ['bar' => [['tag' => 'tag1', 'other' => 'data'], ['tag' => 'tag2']], 'ref' => $firstDocId]]);
$this->documentStore->addDoc($collectionName, $secondDocId, ['foo' => ['bar' => [['tag' => 'tag2', 'other' => 'data'], ['tag' => 'tag3']], 'ref' => $secondDocId]]);
$this->documentStore->addDoc($collectionName, $thirdDocId, ['foo' => ['bar' => [['tag' => 'tag3', 'other' => 'data'], ['tag' => 'tag4']], 'ref' => $thirdDocId]]);

$filteredDocs = \iterator_to_array($this->documentStore->filterDocs(
$collectionName,
new InArrayFilter('foo.bar', ['tag' => 'tag3'])
));

$this->assertCount(2, $filteredDocs);

$refs = array_map(function (array $doc) {
return $doc['foo']['ref'];
}, $filteredDocs);

$this->assertEquals([$secondDocId, $thirdDocId], $refs);
}

private function getIndexes(string $collectionName): array
{
return TestUtil::getIndexes($this->connection, self::TABLE_PREFIX.$collectionName);
Expand Down
2 changes: 1 addition & 1 deletion tests/SchemedPostgresDocumentStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function setUp(): void
$this->documentStore = new PostgresDocumentStore($this->connection, self::TABLE_PREFIX);
}

public function tearDown(): void
protected function tearDown(): void
{
TestUtil::tearDownDatabase();
}
Expand Down
1 change: 1 addition & 0 deletions tests/TestUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public static function tearDownDatabase(): void
$connection = self::getConnection();
$statement = $connection->prepare('SELECT table_name FROM information_schema.tables WHERE table_schema = \'public\';');
$connection->exec('DROP SCHEMA IF EXISTS prooph CASCADE');
$connection->exec('DROP SCHEMA IF EXISTS test CASCADE');

$statement->execute();
$tables = $statement->fetchAll(PDO::FETCH_COLUMN);
Expand Down