Skip to content

Commit

Permalink
GODRIVER-2587 Implement modifyCollection for the unified test runner (#…
Browse files Browse the repository at this point in the history
…1796) [v1] (#1802)

Co-authored-by: Joy Wang <49984055+joyjwang@users.noreply.github.com>
  • Loading branch information
mongodb-drivers-pr-bot[bot] and joyjwang authored Sep 13, 2024
1 parent c5b9705 commit b133d71
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 1 deletion.
31 changes: 31 additions & 0 deletions mongo/integration/unified/database_operation_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,37 @@ func executeListCollectionNames(ctx context.Context, operation *operation) (*ope
return newValueResult(bsontype.Array, data, nil), nil
}

func executeModifyCollection(ctx context.Context, operation *operation) (*operationResult, error) {
db, err := entities(ctx).database(operation.Object)
if err != nil {
return nil, err
}

collModCmd := bson.D{}

elems, _ := operation.Arguments.Elements()
for _, elem := range elems {
key := elem.Key()
val := elem.Value()

switch key {
case "collection":
collModCmd = append(collModCmd, bson.E{"collMod", val.StringValue()})
case "changeStreamPreAndPostImages":
collModCmd = append(collModCmd, bson.E{"changeStreamPreAndPostImages", val.Document()})
case "validator":
collModCmd = append(collModCmd, bson.E{"validator", val.Document()})
case "index":
collModCmd = append(collModCmd, bson.E{"index", val.Document()})
default:
return nil, fmt.Errorf("unrecognized modifyCollection option %q", key)
}
}

res, err := db.RunCommand(ctx, collModCmd).Raw()
return newDocumentResult(res, err), nil
}

func executeRunCommand(ctx context.Context, operation *operation) (*operationResult, error) {
db, err := entities(ctx).database(operation.Object)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion mongo/integration/unified/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ func (op *operation) run(ctx context.Context, loopDone <-chan struct{}) (*operat
return executeListCollections(ctx, op)
case "listCollectionNames":
return executeListCollectionNames(ctx, op)
case "modifyCollection":
return executeModifyCollection(ctx, op)
case "runCommand":
return executeRunCommand(ctx, op)
case "runCursorCommand":
Expand Down Expand Up @@ -259,7 +261,7 @@ func (op *operation) run(ctx context.Context, loopDone <-chan struct{}) (*operat
return executeAddKeyAltName(ctx, op)

// Unsupported operations
case "count", "listIndexNames", "modifyCollection":
case "count", "listIndexNames":
return nil, newSkipTestError(fmt.Sprintf("the %q operation is not supported", op.Name))
default:
return nil, fmt.Errorf("unrecognized entity operation %q", op.Name)
Expand Down
118 changes: 118 additions & 0 deletions testdata/collection-management/modifyCollection-errorResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{
"description": "modifyCollection-errorResponse",
"schemaVersion": "1.12",
"createEntities": [
{
"client": {
"id": "client0",
"observeEvents": [
"commandStartedEvent"
]
}
},
{
"database": {
"id": "database0",
"client": "client0",
"databaseName": "collMod-tests"
}
},
{
"collection": {
"id": "collection0",
"database": "database0",
"collectionName": "test"
}
}
],
"initialData": [
{
"collectionName": "test",
"databaseName": "collMod-tests",
"documents": [
{
"_id": 1,
"x": 1
},
{
"_id": 2,
"x": 1
}
]
}
],
"tests": [
{
"description": "modifyCollection prepareUnique violations are accessible",
"runOnRequirements": [
{
"minServerVersion": "5.2"
}
],
"operations": [
{
"name": "createIndex",
"object": "collection0",
"arguments": {
"keys": {
"x": 1
}
}
},
{
"name": "modifyCollection",
"object": "database0",
"arguments": {
"collection": "test",
"index": {
"keyPattern": {
"x": 1
},
"prepareUnique": true
}
}
},
{
"name": "insertOne",
"object": "collection0",
"arguments": {
"document": {
"_id": 3,
"x": 1
}
},
"expectError": {
"errorCode": 11000
}
},
{
"name": "modifyCollection",
"object": "database0",
"arguments": {
"collection": "test",
"index": {
"keyPattern": {
"x": 1
},
"unique": true
}
},
"expectError": {
"isClientError": false,
"errorCode": 359,
"errorResponse": {
"violations": [
{
"ids": [
1,
2
]
}
]
}
}
}
]
}
]
}
59 changes: 59 additions & 0 deletions testdata/collection-management/modifyCollection-errorResponse.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
description: "modifyCollection-errorResponse"

schemaVersion: "1.12"

createEntities:
- client:
id: &client0 client0
observeEvents: [ commandStartedEvent ]
- database:
id: &database0 database0
client: *client0
databaseName: &database0Name collMod-tests
- collection:
id: &collection0 collection0
database: *database0
collectionName: &collection0Name test

initialData: &initialData
- collectionName: *collection0Name
databaseName: *database0Name
documents:
- { _id: 1, x: 1 }
- { _id: 2, x: 1 }

tests:
- description: "modifyCollection prepareUnique violations are accessible"
runOnRequirements:
- minServerVersion: "5.2" # SERVER-61158
operations:
- name: createIndex
object: *collection0
arguments:
keys: { x: 1 }
- name: modifyCollection
object: *database0
arguments:
collection: *collection0Name
index:
keyPattern: { x: 1 }
prepareUnique: true
- name: insertOne
object: *collection0
arguments:
document: { _id: 3, x: 1 }
expectError:
errorCode: 11000 # DuplicateKey
- name: modifyCollection
object: *database0
arguments:
collection: *collection0Name
index:
keyPattern: { x: 1 }
unique: true
expectError:
isClientError: false
errorCode: 359 # CannotConvertIndexToUnique
errorResponse:
violations:
- { ids: [ 1, 2 ] }

0 comments on commit b133d71

Please sign in to comment.