Skip to content

Commit 1f4d86b

Browse files
GODRIVER-2587 Implement modifyCollection for the unified test runner (#1796)
(cherry picked from commit 4f21584)
1 parent c5b9705 commit 1f4d86b

File tree

4 files changed

+211
-1
lines changed

4 files changed

+211
-1
lines changed

mongo/integration/unified/database_operation_execution.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,37 @@ func executeListCollectionNames(ctx context.Context, operation *operation) (*ope
224224
return newValueResult(bsontype.Array, data, nil), nil
225225
}
226226

227+
func executeModifyCollection(ctx context.Context, operation *operation) (*operationResult, error) {
228+
db, err := entities(ctx).database(operation.Object)
229+
if err != nil {
230+
return nil, err
231+
}
232+
233+
collModCmd := bson.D{}
234+
235+
elems, _ := operation.Arguments.Elements()
236+
for _, elem := range elems {
237+
key := elem.Key()
238+
val := elem.Value()
239+
240+
switch key {
241+
case "collection":
242+
collModCmd = append(collModCmd, bson.E{"collMod", val.StringValue()})
243+
case "changeStreamPreAndPostImages":
244+
collModCmd = append(collModCmd, bson.E{"changeStreamPreAndPostImages", val.Document()})
245+
case "validator":
246+
collModCmd = append(collModCmd, bson.E{"validator", val.Document()})
247+
case "index":
248+
collModCmd = append(collModCmd, bson.E{"index", val.Document()})
249+
default:
250+
return nil, fmt.Errorf("unrecognized modifyCollection option %q", key)
251+
}
252+
}
253+
254+
res, err := db.RunCommand(ctx, collModCmd).Raw()
255+
return newDocumentResult(res, err), nil
256+
}
257+
227258
func executeRunCommand(ctx context.Context, operation *operation) (*operationResult, error) {
228259
db, err := entities(ctx).database(operation.Object)
229260
if err != nil {

mongo/integration/unified/operation.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ func (op *operation) run(ctx context.Context, loopDone <-chan struct{}) (*operat
136136
return executeListCollections(ctx, op)
137137
case "listCollectionNames":
138138
return executeListCollectionNames(ctx, op)
139+
case "modifyCollection":
140+
return executeModifyCollection(ctx, op)
139141
case "runCommand":
140142
return executeRunCommand(ctx, op)
141143
case "runCursorCommand":
@@ -259,7 +261,7 @@ func (op *operation) run(ctx context.Context, loopDone <-chan struct{}) (*operat
259261
return executeAddKeyAltName(ctx, op)
260262

261263
// Unsupported operations
262-
case "count", "listIndexNames", "modifyCollection":
264+
case "count", "listIndexNames":
263265
return nil, newSkipTestError(fmt.Sprintf("the %q operation is not supported", op.Name))
264266
default:
265267
return nil, fmt.Errorf("unrecognized entity operation %q", op.Name)
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
{
2+
"description": "modifyCollection-errorResponse",
3+
"schemaVersion": "1.12",
4+
"createEntities": [
5+
{
6+
"client": {
7+
"id": "client0",
8+
"observeEvents": [
9+
"commandStartedEvent"
10+
]
11+
}
12+
},
13+
{
14+
"database": {
15+
"id": "database0",
16+
"client": "client0",
17+
"databaseName": "collMod-tests"
18+
}
19+
},
20+
{
21+
"collection": {
22+
"id": "collection0",
23+
"database": "database0",
24+
"collectionName": "test"
25+
}
26+
}
27+
],
28+
"initialData": [
29+
{
30+
"collectionName": "test",
31+
"databaseName": "collMod-tests",
32+
"documents": [
33+
{
34+
"_id": 1,
35+
"x": 1
36+
},
37+
{
38+
"_id": 2,
39+
"x": 1
40+
}
41+
]
42+
}
43+
],
44+
"tests": [
45+
{
46+
"description": "modifyCollection prepareUnique violations are accessible",
47+
"runOnRequirements": [
48+
{
49+
"minServerVersion": "5.2"
50+
}
51+
],
52+
"operations": [
53+
{
54+
"name": "createIndex",
55+
"object": "collection0",
56+
"arguments": {
57+
"keys": {
58+
"x": 1
59+
}
60+
}
61+
},
62+
{
63+
"name": "modifyCollection",
64+
"object": "database0",
65+
"arguments": {
66+
"collection": "test",
67+
"index": {
68+
"keyPattern": {
69+
"x": 1
70+
},
71+
"prepareUnique": true
72+
}
73+
}
74+
},
75+
{
76+
"name": "insertOne",
77+
"object": "collection0",
78+
"arguments": {
79+
"document": {
80+
"_id": 3,
81+
"x": 1
82+
}
83+
},
84+
"expectError": {
85+
"errorCode": 11000
86+
}
87+
},
88+
{
89+
"name": "modifyCollection",
90+
"object": "database0",
91+
"arguments": {
92+
"collection": "test",
93+
"index": {
94+
"keyPattern": {
95+
"x": 1
96+
},
97+
"unique": true
98+
}
99+
},
100+
"expectError": {
101+
"isClientError": false,
102+
"errorCode": 359,
103+
"errorResponse": {
104+
"violations": [
105+
{
106+
"ids": [
107+
1,
108+
2
109+
]
110+
}
111+
]
112+
}
113+
}
114+
}
115+
]
116+
}
117+
]
118+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
description: "modifyCollection-errorResponse"
2+
3+
schemaVersion: "1.12"
4+
5+
createEntities:
6+
- client:
7+
id: &client0 client0
8+
observeEvents: [ commandStartedEvent ]
9+
- database:
10+
id: &database0 database0
11+
client: *client0
12+
databaseName: &database0Name collMod-tests
13+
- collection:
14+
id: &collection0 collection0
15+
database: *database0
16+
collectionName: &collection0Name test
17+
18+
initialData: &initialData
19+
- collectionName: *collection0Name
20+
databaseName: *database0Name
21+
documents:
22+
- { _id: 1, x: 1 }
23+
- { _id: 2, x: 1 }
24+
25+
tests:
26+
- description: "modifyCollection prepareUnique violations are accessible"
27+
runOnRequirements:
28+
- minServerVersion: "5.2" # SERVER-61158
29+
operations:
30+
- name: createIndex
31+
object: *collection0
32+
arguments:
33+
keys: { x: 1 }
34+
- name: modifyCollection
35+
object: *database0
36+
arguments:
37+
collection: *collection0Name
38+
index:
39+
keyPattern: { x: 1 }
40+
prepareUnique: true
41+
- name: insertOne
42+
object: *collection0
43+
arguments:
44+
document: { _id: 3, x: 1 }
45+
expectError:
46+
errorCode: 11000 # DuplicateKey
47+
- name: modifyCollection
48+
object: *database0
49+
arguments:
50+
collection: *collection0Name
51+
index:
52+
keyPattern: { x: 1 }
53+
unique: true
54+
expectError:
55+
isClientError: false
56+
errorCode: 359 # CannotConvertIndexToUnique
57+
errorResponse:
58+
violations:
59+
- { ids: [ 1, 2 ] }

0 commit comments

Comments
 (0)