Skip to content

Commit 4f21584

Browse files
authored
GODRIVER-2587 Implement modifyCollection for the unified test runner (#1796)
1 parent 0b03704 commit 4f21584

File tree

4 files changed

+211
-1
lines changed

4 files changed

+211
-1
lines changed

internal/integration/unified/database_operation_execution.go

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

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

internal/integration/unified/operation.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ func (op *operation) run(ctx context.Context, loopDone <-chan struct{}) (*operat
145145
return executeListCollections(ctx, op)
146146
case "listCollectionNames":
147147
return executeListCollectionNames(ctx, op)
148+
case "modifyCollection":
149+
return executeModifyCollection(ctx, op)
148150
case "runCommand":
149151
return executeRunCommand(ctx, op)
150152
case "runCursorCommand":
@@ -268,7 +270,7 @@ func (op *operation) run(ctx context.Context, loopDone <-chan struct{}) (*operat
268270
return executeAddKeyAltName(ctx, op)
269271

270272
// Unsupported operations
271-
case "count", "listIndexNames", "modifyCollection":
273+
case "count", "listIndexNames":
272274
return nil, newSkipTestError(fmt.Sprintf("the %q operation is not supported", op.Name))
273275
default:
274276
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)