-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from event-engine/feature/in_memory_unique_indices
Support unique indices in InMemoryDocumentStore
- Loading branch information
Showing
11 changed files
with
532 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
# php-document-store | ||
|
||
Event Engine PHP Document Store Contract | ||
|
||
## Testing | ||
|
||
This package includes an in-memory implementation of the `DocumentStore` interface which is useful for tests. | ||
To be able to test the in-memory implementation in isolation we have to copy some classes from `event-engine/persistence` into the test namespace of this repo. | ||
The implementation depends on classes from that other package, but we cannot pull it with composer due to circular dependencies. | ||
We'll solve the issue in the future by moving the in-memory implementation to `event-engine/persistence`, but for now backwards compatibility is more important. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace EventEngineTest\DocumentStore; | ||
|
||
use EventEngine\DocumentStore\FieldIndex; | ||
use EventEngine\DocumentStore\Filter\EqFilter; | ||
use EventEngine\DocumentStore\InMemoryDocumentStore; | ||
use EventEngine\DocumentStore\MultiFieldIndex; | ||
use EventEngine\Persistence\InMemoryConnection; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class InMemoryDocumentStoreTest extends TestCase | ||
{ | ||
/** | ||
* @var InMemoryDocumentStore | ||
*/ | ||
private $store; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->store = new InMemoryDocumentStore(new InMemoryConnection()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_adds_collection() | ||
{ | ||
$this->store->addCollection('test'); | ||
$this->assertTrue($this->store->hasCollection('test')); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_adds_collection_with_unique_index() | ||
{ | ||
$this->store->addCollection('test', FieldIndex::namedIndexForField('unique_prop_idx', 'some.prop', FieldIndex::SORT_ASC, true)); | ||
$this->assertTrue($this->store->hasCollectionIndex('test', 'unique_prop_idx')); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_adds_and_updates_a_doc() | ||
{ | ||
$this->store->addCollection('test'); | ||
|
||
$doc = [ | ||
'some' => [ | ||
'prop' => 'foo', | ||
'other' => [ | ||
'nested' => 42 | ||
] | ||
], | ||
'baz' => 'bat', | ||
]; | ||
|
||
$this->store->addDoc('test', '1', $doc); | ||
|
||
$persistedDoc = $this->store->getDoc('test', '1'); | ||
|
||
$this->assertEquals($doc, $persistedDoc); | ||
|
||
$doc['baz'] = 'changed val'; | ||
|
||
$this->store->updateDoc('test', '1', $doc); | ||
|
||
$filter = new EqFilter('baz', 'changed val'); | ||
|
||
$filteredDocs = $this->store->filterDocs('test', $filter); | ||
|
||
$this->assertCount(1, $filteredDocs); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_updates_a_subset_of_a_doc() | ||
{ | ||
$this->store->addCollection('test'); | ||
|
||
$doc = [ | ||
'some' => [ | ||
'prop' => 'foo', | ||
'other' => [ | ||
'nested' => 42 | ||
] | ||
], | ||
'baz' => 'bat', | ||
]; | ||
|
||
$this->store->addDoc('test', '1', $doc); | ||
|
||
$this->store->updateDoc('test', '1', [ | ||
'some' => [ | ||
'prop' => 'fuzz' | ||
] | ||
]); | ||
|
||
$filteredDocs = iterator_to_array($this->store->filterDocs('test', new EqFilter('some.prop', 'fuzz'))); | ||
$this->assertEquals(42, $filteredDocs[0]['some']['other']['nested']); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_ensures_unique_constraints_for_a_field() | ||
{ | ||
$this->store->addCollection('test', FieldIndex::namedIndexForField('unique_prop_idx', 'some.prop', FieldIndex::SORT_ASC, true)); | ||
|
||
$this->store->addDoc('test', '1', ['some' => ['prop' => 'foo']]); | ||
$this->store->addDoc('test', '2', ['some' => ['prop' => 'bar']]); | ||
|
||
$this->expectExceptionMessageRegExp('/^Unique constraint violation/'); | ||
$this->store->addDoc('test', '3', ['some' => ['prop' => 'foo']]); | ||
} | ||
|
||
|
||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_ensures_unique_constraints_for_multiple_fields() | ||
{ | ||
$multiFieldIndex = MultiFieldIndex::forFields(['some.prop', 'some.other.prop'], true); | ||
|
||
$this->store->addCollection('test', $multiFieldIndex); | ||
|
||
$this->store->addDoc('test', '1', ['some' => ['prop' => 'foo', 'other' => ['prop' => 'bat']]]); | ||
$this->store->addDoc('test', '2', ['some' => ['prop' => 'bar', 'other' => ['prop' => 'bat']]]); | ||
$this->store->addDoc('test', '3', ['some' => ['prop' => 'bar']]); | ||
|
||
$this->expectExceptionMessageRegExp('/^Unique constraint violation/'); | ||
$this->store->updateDoc('test', '2', ['some' => ['prop' => 'foo']]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
/** | ||
* This file is part of event-engine/php-persistence. | ||
* (c) 2018-2019 prooph software GmbH <contact@prooph.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EventEngine\Persistence\Exception; | ||
|
||
final class TransactionAlreadyStarted extends \RuntimeException implements TransactionException | ||
{ | ||
protected $message = 'The transaction has already been started.'; | ||
} |
Oops, something went wrong.