Skip to content

PHPLIB-536: Add spec tests for default write concern #726

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
Mar 18, 2020
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
10 changes: 10 additions & 0 deletions tests/SpecTests/CommandExpectations.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ public static function fromCrud(array $expectedEvents)
return $o;
}

public static function fromReadWriteConcern(array $expectedEvents)
{
$o = new self($expectedEvents);

$o->ignoreCommandFailed = true;
$o->ignoreCommandSucceeded = true;

return $o;
}

public static function fromRetryableReads(array $expectedEvents)
{
$o = new self($expectedEvents);
Expand Down
46 changes: 32 additions & 14 deletions tests/SpecTests/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,21 @@ public static function fromCrud(stdClass $test, $databaseName, $collectionName)
return $o;
}

public static function fromReadWriteConcern(stdClass $test, $databaseName, $collectionName)
{
$o = new self($databaseName, $collectionName);

if (isset($test->outcome->collection->name)) {
$o->outcomeCollectionName = $test->outcome->collection->name;
}

$clientOptions = isset($test->clientOptions) ? (array) $test->clientOptions : [];

$o->client = new Client(FunctionalTestCase::getUri(), $clientOptions);

return $o;
}

public static function fromRetryableReads(stdClass $test, $databaseName, $collectionName, $bucketName)
{
$o = new self($databaseName, $collectionName);
Expand Down Expand Up @@ -253,12 +268,13 @@ public function getClient()
return $this->useEncryptedClient && $this->encryptedClient ? $this->encryptedClient : $this->client;
}

public function getCollection(array $collectionOptions = [])
public function getCollection(array $collectionOptions = [], array $databaseOptions = [])
{
return $this->selectCollection(
$this->databaseName,
$this->collectionName,
$this->prepareOptions($collectionOptions)
$collectionOptions,
$databaseOptions
);
}

Expand Down Expand Up @@ -312,13 +328,17 @@ public function prepareOptions(array $options)
throw new LogicException('Unsupported writeConcern args: ' . implode(',', array_keys($diff)));
}

$w = $writeConcern['w'];
$wtimeout = $writeConcern['wtimeout'] ?? 0;
$j = $writeConcern['j'] ?? null;
if (! empty($writeConcern)) {
$w = $writeConcern['w'];
$wtimeout = $writeConcern['wtimeout'] ?? 0;
$j = $writeConcern['j'] ?? null;

$options['writeConcern'] = isset($j)
? new WriteConcern($w, $wtimeout, $j)
: new WriteConcern($w, $wtimeout);
$options['writeConcern'] = isset($j)
? new WriteConcern($w, $wtimeout, $j)
: new WriteConcern($w, $wtimeout);
} else {
unset($options['writeConcern']);
}
}

return $options;
Expand Down Expand Up @@ -380,13 +400,11 @@ public function replaceCommandSessionPlaceholder(stdClass $command)
}
}

public function selectCollection($databaseName, $collectionName, array $collectionOptions = [])
public function selectCollection($databaseName, $collectionName, array $collectionOptions = [], array $databaseOptions = [])
{
return $this->getClient()->selectCollection(
$databaseName,
$collectionName,
$this->prepareOptions($collectionOptions)
);
return $this
->selectDatabase($databaseName, $databaseOptions)
->selectCollection($collectionName, $this->prepareOptions($collectionOptions));
}

public function selectDatabase($databaseName, array $databaseOptions = [])
Expand Down
5 changes: 5 additions & 0 deletions tests/SpecTests/ErrorExpectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public static function fromCrud(stdClass $result)
return $o;
}

public static function fromReadWriteConcern(stdClass $operation)
{
return self::fromGenericOperation($operation);
}

public static function fromRetryableReads(stdClass $operation)
{
$o = new self();
Expand Down
31 changes: 29 additions & 2 deletions tests/SpecTests/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ final class Operation
/** @var string|null */
private $databaseName;

/** @var array */
private $databaseOptions = [];

/** @var string */
private $name;

Expand Down Expand Up @@ -184,6 +187,24 @@ public static function fromCrud(stdClass $operation)
return $o;
}

public static function fromReadWriteConcern(stdClass $operation)
{
$o = new self($operation);

$o->errorExpectation = ErrorExpectation::fromReadWriteConcern($operation);
$o->resultExpectation = ResultExpectation::fromReadWriteConcern($operation, $o->getResultAssertionType());

if (isset($operation->databaseOptions)) {
$o->databaseOptions = (array) $operation->databaseOptions;
}

if (isset($operation->collectionOptions)) {
$o->collectionOptions = (array) $operation->collectionOptions;
}

return $o;
}

public static function fromRetryableReads(stdClass $operation)
{
$o = new self($operation);
Expand Down Expand Up @@ -277,7 +298,7 @@ private function execute(FunctionalTestCase $test, Context $context)

return $this->executeForClient($client, $context);
case self::OBJECT_COLLECTION:
$collection = $context->getCollection($this->collectionOptions);
$collection = $context->getCollection($this->collectionOptions, $this->databaseOptions);

return $this->executeForCollection($collection, $context);
case self::OBJECT_DATABASE:
Expand All @@ -289,7 +310,7 @@ private function execute(FunctionalTestCase $test, Context $context)

return $this->executeForGridFSBucket($bucket, $context);
case self::OBJECT_SELECT_COLLECTION:
$collection = $context->selectCollection($this->databaseName, $this->collectionName, $this->collectionOptions);
$collection = $context->selectCollection($this->databaseName, $this->collectionName, $this->collectionOptions, $this->databaseOptions);

return $this->executeForCollection($collection, $context);
case self::OBJECT_SELECT_DATABASE:
Expand Down Expand Up @@ -367,6 +388,11 @@ private function executeForCollection(Collection $collection, Context $context)
$args['keys'],
array_diff_key($args, ['keys' => 1])
);
case 'dropIndex':
return $collection->dropIndex(
$args['name'],
array_diff_key($args, ['name' => 1])
);
case 'count':
case 'countDocuments':
case 'find':
Expand Down Expand Up @@ -738,6 +764,7 @@ private function getResultAssertionTypeForCollection()
case 'countDocuments':
return ResultExpectation::ASSERT_SAME;
case 'createIndex':
case 'dropIndex':
return ResultExpectation::ASSERT_MATCHES_DOCUMENT;
case 'distinct':
case 'estimatedDocumentCount':
Expand Down
113 changes: 113 additions & 0 deletions tests/SpecTests/ReadWriteConcernSpecTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace MongoDB\Tests\SpecTests;

use stdClass;
use function basename;
use function dirname;
use function file_get_contents;
use function glob;

/**
* @see https://github.com/mongodb/specifications/tree/master/source/read-write-concern
*/
class ReadWriteConcernSpecTest extends FunctionalTestCase
{
/** @var array */
private static $incompleteTests = [];

/**
* Assert that the expected and actual command documents match.
*
* @param stdClass $expected Expected command document
* @param stdClass $actual Actual command document
*/
public static function assertCommandMatches(stdClass $expected, stdClass $actual)
{
foreach ($expected as $key => $value) {
if ($value === null) {
static::assertObjectNotHasAttribute($key, $actual);
unset($expected->{$key});
}
}

static::assertDocumentsMatch($expected, $actual);
}

/**
* Execute an individual test case from the specification.
*
* @dataProvider provideTests
* @param stdClass $test Individual "tests[]" document
* @param array $runOn Top-level "runOn" array with server requirements
* @param array $data Top-level "data" array to initialize collection
* @param string $databaseName Name of database under test
* @param string $collectionName Name of collection under test
*/
public function testReadWriteConcern(stdClass $test, array $runOn = null, array $data, $databaseName = null, $collectionName = null)
{
if (isset(self::$incompleteTests[$this->dataDescription()])) {
$this->markTestIncomplete(self::$incompleteTests[$this->dataDescription()]);
}

if (isset($runOn)) {
$this->checkServerRequirements($runOn);
}

if (isset($test->skipReason)) {
$this->markTestSkipped($test->skipReason);
}

$databaseName = $databaseName ?? $this->getDatabaseName();
$collectionName = $collectionName ?? $this->getCollectionName();

$context = Context::fromReadWriteConcern($test, $databaseName, $collectionName);
$this->setContext($context);

$this->dropTestAndOutcomeCollections();
$this->insertDataFixtures($data);

if (isset($test->failPoint)) {
$this->configureFailPoint($test->failPoint);
}

if (isset($test->expectations)) {
$commandExpectations = CommandExpectations::fromReadWriteConcern($test->expectations);
$commandExpectations->startMonitoring();
}

foreach ($test->operations as $operation) {
Operation::fromReadWriteConcern($operation)->assert($this, $context);
}

if (isset($commandExpectations)) {
$commandExpectations->stopMonitoring();
$commandExpectations->assert($this, $context);
}

if (isset($test->outcome->collection->data)) {
$this->assertOutcomeCollectionData($test->outcome->collection->data);
}
}

public function provideTests()
{
$testArgs = [];

foreach (glob(__DIR__ . '/read-write-concern/operation/*.json') as $filename) {
$json = $this->decodeJson(file_get_contents($filename));
$group = basename(dirname($filename)) . '/' . basename($filename, '.json');
$runOn = $json->runOn ?? null;
$data = $json->data ?? [];
$databaseName = $json->database_name ?? null;
$collectionName = $json->collection_name ?? null;

foreach ($json->tests as $test) {
$name = $group . ': ' . $test->description;
$testArgs[$name] = [$test, $runOn, $data, $databaseName, $collectionName];
}
}

return $testArgs;
}
}
13 changes: 13 additions & 0 deletions tests/SpecTests/ResultExpectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ public static function fromCrud(stdClass $operation, $defaultAssertionType)
return new self($assertionType, $expectedValue);
}

public static function fromReadWriteConcern(stdClass $operation, $defaultAssertionType)
{
if (property_exists($operation, 'result') && ! self::isErrorResult($operation->result)) {
$assertionType = $operation->result === null ? self::ASSERT_NULL : $defaultAssertionType;
$expectedValue = $operation->result;
} else {
$assertionType = self::ASSERT_NOTHING;
$expectedValue = null;
}

return new self($assertionType, $expectedValue);
}

public static function fromRetryableReads(stdClass $operation, $defaultAssertionType)
{
if (property_exists($operation, 'result') && ! self::isErrorResult($operation->result)) {
Expand Down
Loading