Skip to content

Test collection/index creation within transaction #722

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 2 commits into from
Mar 10, 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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ jobs:
- stage: Test
php: "7.3"
env:
- SERVER_VERSION=4.3.3
- SERVER_VERSION=4.3.4

# Test other server configurations
- stage: Test
Expand Down
2 changes: 1 addition & 1 deletion tests/SpecTests/CommandExpectations.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static function fromTransactions(array $expectedEvents)
* configureFailPoint needs to be ignored as the targetedFailPoint
* operation will be caught by command monitoring and is also not
* present in the expected commands in spec tests. */
$o->ignoredCommandNames = ['buildInfo', 'getParameter', 'configureFailPoint'];
$o->ignoredCommandNames = ['buildInfo', 'getParameter', 'configureFailPoint', 'listCollections', 'listIndexes'];

return $o;
}
Expand Down
84 changes: 84 additions & 0 deletions tests/SpecTests/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\GridFS\Bucket;
use MongoDB\Model\CollectionInfo;
use MongoDB\Model\IndexInfo;
use MongoDB\Operation\FindOneAndReplace;
use MongoDB\Operation\FindOneAndUpdate;
use stdClass;
use function array_diff_key;
use function array_map;
use function fclose;
use function fopen;
use function iterator_to_array;
use function MongoDB\is_last_pipeline_operator_write;
use function MongoDB\with_transaction;
use function stream_get_contents;
Expand Down Expand Up @@ -359,6 +362,11 @@ private function executeForCollection(Collection $collection, Context $context)
array_map([$this, 'prepareBulkWriteRequest'], $args['requests']),
$options
);
case 'createIndex':
return $collection->createIndex(
$args['keys'],
array_diff_key($args, ['keys' => 1])
);
case 'count':
case 'countDocuments':
case 'find':
Expand Down Expand Up @@ -466,6 +474,16 @@ private function executeForDatabase(Database $database, Context $context)
$args['pipeline'],
array_diff_key($args, ['pipeline' => 1])
);
case 'createCollection':
return $database->createCollection(
$args['collection'],
array_diff_key($args, ['collection' => 1])
);
case 'dropCollection':
return $database->dropCollection(
$args['collection'],
array_diff_key($args, ['collection' => 1])
);
case 'listCollections':
return $database->listCollections($args);
case 'runCommand':
Expand Down Expand Up @@ -570,6 +588,36 @@ private function executeForTestRunner(FunctionalTestCase $test, Context $context
$context->replaceArgumentSessionPlaceholder($args);

switch ($this->name) {
case 'assertCollectionExists':
$databaseName = $args['database'];
$collectionName = $args['collection'];

$test->assertContains($collectionName, $this->getCollectionNames($context, $databaseName));

return null;
case 'assertCollectionNotExists':
$databaseName = $args['database'];
$collectionName = $args['collection'];

$test->assertNotContains($collectionName, $this->getCollectionNames($context, $databaseName));

return null;
case 'assertIndexExists':
$databaseName = $args['database'];
$collectionName = $args['collection'];
$indexName = $args['index'];

$test->assertContains($indexName, $this->getIndexNames($context, $databaseName, $collectionName));

return null;
case 'assertIndexNotExists':
$databaseName = $args['database'];
$collectionName = $args['collection'];
$indexName = $args['index'];

$test->assertNotContains($indexName, $this->getIndexNames($context, $databaseName, $collectionName));

return null;
case 'assertSessionPinned':
$test->assertInstanceOf(Session::class, $args['session']);
$test->assertInstanceOf(Server::class, $args['session']->getServer());
Expand Down Expand Up @@ -599,6 +647,37 @@ private function executeForTestRunner(FunctionalTestCase $test, Context $context
}
}

/**
* @param string $databaseName
*
* @return array
*/
private function getCollectionNames(Context $context, $databaseName)
{
return array_map(
function (CollectionInfo $collectionInfo) {
return $collectionInfo->getName();
},
iterator_to_array($context->selectDatabase($databaseName)->listCollections())
);
}

/**
* @param string $databaseName
* @param string $collectionName
*
* @return array
*/
private function getIndexNames(Context $context, $databaseName, $collectionName)
{
return array_map(
function (IndexInfo $indexInfo) {
return $indexInfo->getName();
},
iterator_to_array($context->selectCollection($databaseName, $collectionName)->listIndexes())
);
}

/**
* @throws LogicException if the operation object is unsupported
*/
Expand Down Expand Up @@ -657,6 +736,9 @@ private function getResultAssertionTypeForCollection()
return ResultExpectation::ASSERT_BULKWRITE;
case 'count':
case 'countDocuments':
return ResultExpectation::ASSERT_SAME;
case 'createIndex':
return ResultExpectation::ASSERT_MATCHES_DOCUMENT;
case 'distinct':
case 'estimatedDocumentCount':
return ResultExpectation::ASSERT_SAME;
Expand Down Expand Up @@ -700,6 +782,8 @@ private function getResultAssertionTypeForDatabase()
case 'aggregate':
case 'listCollections':
return ResultExpectation::ASSERT_SAME_DOCUMENTS;
case 'createCollection':
case 'dropCollection':
case 'runCommand':
return ResultExpectation::ASSERT_MATCHES_DOCUMENT;
case 'watch':
Expand Down
204 changes: 204 additions & 0 deletions tests/SpecTests/transactions/create-collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
{
"runOn": [
{
"minServerVersion": "4.3.4",
"topology": [
"replicaset",
"sharded"
]
}
],
"database_name": "transaction-tests",
"collection_name": "test",
"data": [],
"tests": [
{
"description": "explicitly create collection using create command",
"operations": [
{
"name": "dropCollection",
"object": "database",
"arguments": {
"collection": "test"
}
},
{
"name": "startTransaction",
"object": "session0"
},
{
"name": "createCollection",
"object": "database",
"arguments": {
"session": "session0",
"collection": "test"
}
},
{
"name": "assertCollectionNotExists",
"object": "testRunner",
"arguments": {
"database": "transaction-tests",
"collection": "test"
}
},
{
"name": "commitTransaction",
"object": "session0"
},
{
"name": "assertCollectionExists",
"object": "testRunner",
"arguments": {
"database": "transaction-tests",
"collection": "test"
}
}
],
"expectations": [
{
"command_started_event": {
"command": {
"drop": "test",
"writeConcern": null
},
"command_name": "drop",
"database_name": "transaction-tests"
}
},
{
"command_started_event": {
"command": {
"create": "test",
"lsid": "session0",
"txnNumber": {
"$numberLong": "1"
},
"startTransaction": true,
"autocommit": false,
"writeConcern": null
},
"command_name": "create",
"database_name": "transaction-tests"
}
},
{
"command_started_event": {
"command": {
"commitTransaction": 1,
"lsid": "session0",
"txnNumber": {
"$numberLong": "1"
},
"startTransaction": null,
"autocommit": false,
"writeConcern": null
},
"command_name": "commitTransaction",
"database_name": "admin"
}
}
]
},
{
"description": "implicitly create collection using insert",
"operations": [
{
"name": "dropCollection",
"object": "database",
"arguments": {
"collection": "test"
}
},
{
"name": "startTransaction",
"object": "session0"
},
{
"name": "insertOne",
"object": "collection",
"arguments": {
"session": "session0",
"document": {
"_id": 1
}
},
"result": {
"insertedId": 1
}
},
{
"name": "assertCollectionNotExists",
"object": "testRunner",
"arguments": {
"database": "transaction-tests",
"collection": "test"
}
},
{
"name": "commitTransaction",
"object": "session0"
},
{
"name": "assertCollectionExists",
"object": "testRunner",
"arguments": {
"database": "transaction-tests",
"collection": "test"
}
}
],
"expectations": [
{
"command_started_event": {
"command": {
"drop": "test",
"writeConcern": null
},
"command_name": "drop",
"database_name": "transaction-tests"
}
},
{
"command_started_event": {
"command": {
"insert": "test",
"documents": [
{
"_id": 1
}
],
"ordered": true,
"readConcern": null,
"lsid": "session0",
"txnNumber": {
"$numberLong": "1"
},
"startTransaction": true,
"autocommit": false,
"writeConcern": null
},
"command_name": "insert",
"database_name": "transaction-tests"
}
},
{
"command_started_event": {
"command": {
"commitTransaction": 1,
"lsid": "session0",
"txnNumber": {
"$numberLong": "1"
},
"startTransaction": null,
"autocommit": false,
"writeConcern": null
},
"command_name": "commitTransaction",
"database_name": "admin"
}
}
]
}
]
}
Loading