Skip to content

Commit 826d8db

Browse files
committed
CSHARP-4152: Add createCollection and collMod spec tests for changeStreamPreAndPostImages option
1 parent 1a56b9e commit 826d8db

File tree

10 files changed

+286
-11
lines changed

10 files changed

+286
-11
lines changed

src/MongoDB.Driver.Core/Core/Operations/CreateCollectionOperation.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ CreateCollectionOperation CreateInnerCollectionOperation(string collectionName)
6969
// fields
7070
private bool? _autoIndexId;
7171
private bool? _capped;
72+
private BsonDocument _changeStreamPreAndPostImages;
7273
private BsonDocument _clusteredIndex;
7374
private Collation _collation;
7475
private readonly CollectionNamespace _collectionNamespace;
@@ -128,6 +129,18 @@ public bool? Capped
128129
set { _capped = value; }
129130
}
130131

132+
/// <summary>
133+
/// Gets or sets a change streams pre and post images options.
134+
/// </summary>
135+
/// <value>
136+
/// change streams pre and post images options.
137+
/// </value>
138+
public BsonDocument ChangeStreamPreAndPostImages
139+
{
140+
get { return _changeStreamPreAndPostImages; }
141+
set { _changeStreamPreAndPostImages = value; }
142+
}
143+
131144
/// <summary>
132145
/// Gets or sets the collation.
133146
/// </summary>
@@ -361,7 +374,8 @@ internal BsonDocument CreateCommand(ICoreSessionHandle session)
361374
{ "writeConcern", writeConcern, writeConcern != null },
362375
{ "expireAfterSeconds", () => _expireAfter.Value.TotalSeconds, _expireAfter.HasValue },
363376
{ "timeseries", () => _timeSeriesOptions.ToBsonDocument(), _timeSeriesOptions != null },
364-
{ "encryptedFields", _encryptedFields, _encryptedFields != null }
377+
{ "encryptedFields", _encryptedFields, _encryptedFields != null },
378+
{ "changeStreamPreAndPostImages", _changeStreamPreAndPostImages, _changeStreamPreAndPostImages != null }
365379
};
366380
}
367381

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using MongoDB.Bson;
17+
using MongoDB.Bson.Serialization;
18+
using MongoDB.Bson.Serialization.Serializers;
19+
20+
namespace MongoDB.Driver
21+
{
22+
/// <summary>
23+
/// Change stream pre and post images options.
24+
/// </summary>
25+
public sealed class ChangeStreamPreAndPostImagesOptions : BsonDocumentBackedClass
26+
{
27+
// constructors
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="ChangeStreamPreAndPostImagesOptions"/> class.
30+
/// </summary>
31+
public ChangeStreamPreAndPostImagesOptions()
32+
: base(ChangeStreamPreAndPostImagesOptionsSerializer.Instance)
33+
{
34+
}
35+
36+
/// <summary>
37+
/// Initializes a new instance of the <see cref="ChangeStreamPreAndPostImagesOptions"/> class.
38+
/// </summary>
39+
public ChangeStreamPreAndPostImagesOptions(BsonDocument bsonDocument)
40+
: base(bsonDocument, ChangeStreamPreAndPostImagesOptionsSerializer.Instance)
41+
{
42+
}
43+
44+
// public properties
45+
/// <summary>
46+
/// Gets the backing document.
47+
/// </summary>
48+
new public BsonDocument BackingDocument => base.BackingDocument;
49+
50+
/// <summary>
51+
/// Gets or sets a value indicating whether ChangeStreamPreAndPostImages is enabled.
52+
/// </summary>
53+
public bool Enabled
54+
{
55+
get
56+
{
57+
return GetValue(nameof(Enabled), false);
58+
}
59+
set
60+
{
61+
SetValue(nameof(Enabled), value);
62+
}
63+
}
64+
}
65+
66+
internal sealed class ChangeStreamPreAndPostImagesOptionsSerializer : BsonDocumentBackedClassSerializer<ChangeStreamPreAndPostImagesOptions>
67+
{
68+
public static ChangeStreamPreAndPostImagesOptionsSerializer Instance { get; } = new ChangeStreamPreAndPostImagesOptionsSerializer();
69+
70+
// constructors
71+
public ChangeStreamPreAndPostImagesOptionsSerializer()
72+
{
73+
RegisterMember("Enabled", "enabled", new BooleanSerializer());
74+
}
75+
76+
// methods
77+
protected override ChangeStreamPreAndPostImagesOptions CreateInstance(BsonDocument backingDocument) =>
78+
new ChangeStreamPreAndPostImagesOptions(backingDocument);
79+
}
80+
}

src/MongoDB.Driver/CreateCollectionOptions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class CreateCollectionOptions
2727
// fields
2828
private bool? _autoIndexId;
2929
private bool? _capped;
30+
private ChangeStreamPreAndPostImagesOptions _changeStreamPreAndPostImagesOptions;
3031
private Collation _collation;
3132
private BsonDocument _encryptedFields;
3233
private TimeSpan? _expireAfter;
@@ -70,6 +71,15 @@ public bool? Capped
7071
set { _capped = value; }
7172
}
7273

74+
/// <summary>
75+
/// Gets or sets Gets or sets a change streams pre and post images options.
76+
/// </summary>
77+
public ChangeStreamPreAndPostImagesOptions ChangeStreamPreAndPostImagesOptions
78+
{
79+
get { return _changeStreamPreAndPostImagesOptions; }
80+
set { _changeStreamPreAndPostImagesOptions = value; }
81+
}
82+
7383
/// <summary>
7484
/// Gets or sets encrypted fields.
7585
/// </summary>
@@ -216,6 +226,7 @@ internal static CreateCollectionOptions<TDocument> CoercedFrom(CreateCollectionO
216226
AutoIndexId = options.AutoIndexId,
217227
Capped = options.Capped,
218228
Collation = options.Collation,
229+
ChangeStreamPreAndPostImagesOptions = options.ChangeStreamPreAndPostImagesOptions,
219230
EncryptedFields = options.EncryptedFields,
220231
ExpireAfter = options.ExpireAfter,
221232
IndexOptionDefaults = options.IndexOptionDefaults,

src/MongoDB.Driver/MongoDatabaseImpl.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ private IWriteOperation<BsonDocument> CreateCreateCollectionOperation<TDocument>
665665
cco.AutoIndexId = options.AutoIndexId;
666666
#pragma warning restore CS0618 // Type or member is obsolete
667667
cco.Capped = options.Capped;
668+
cco.ChangeStreamPreAndPostImages = options.ChangeStreamPreAndPostImagesOptions?.BackingDocument;
668669
cco.ClusteredIndex = clusteredIndex;
669670
cco.Collation = options.Collation;
670671
cco.ExpireAfter = options.ExpireAfter;

tests/MongoDB.Driver.Core.Tests/Core/Operations/CreateCollectionOperationTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public void constructor_should_initialize_subject()
8787
subject.AutoIndexId.Should().NotHaveValue();
8888
#pragma warning restore
8989
subject.Capped.Should().NotHaveValue();
90+
subject.ChangeStreamPreAndPostImages.Should().BeNull();
9091
subject.ClusteredIndex.Should().BeNull();
9192
subject.Collation.Should().BeNull();
9293
subject.EncryptedFields.Should().BeNull();
@@ -172,6 +173,29 @@ public void CreateCommand_should_return_expected_result_when_Capped_is_set(
172173
result.Should().Be(expectedResult);
173174
}
174175

176+
[Fact]
177+
public void CreateCommand_should_return_expected_result_when_ChangeStreamsPreAndPost_is_set()
178+
{
179+
var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
180+
{
181+
ChangeStreamPreAndPostImages = new BsonDocument()
182+
{
183+
{ "key", "value" }
184+
}
185+
};
186+
187+
var session = OperationTestHelper.CreateSession();
188+
189+
var result = subject.CreateCommand(session);
190+
191+
var expectedResult = new BsonDocument
192+
{
193+
{ "create", _collectionNamespace.CollectionName },
194+
{ "changeStreamPreAndPostImages", new BsonDocument() { { "key", "value" } } }
195+
};
196+
result.Should().Be(expectedResult);
197+
}
198+
175199
[Theory]
176200
[ParameterAttributeData]
177201
public void CreateCommand_should_return_expected_result_when_ClusteredIndex_is_set(

tests/MongoDB.Driver.Core.Tests/Core/Operations/OperationTestBase.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,13 @@ public virtual void Dispose()
6868
protected void CreateCollection(CollectionNamespace collectionNamespace, bool changeStreamPreAndPostImages = false)
6969
{
7070
var operation = new CreateCollectionOperation(collectionNamespace, _messageEncoderSettings);
71-
ExecuteOperation(operation);
7271

7372
if (changeStreamPreAndPostImages)
7473
{
75-
var command = new BsonDocument()
76-
{
77-
{ "collMod", _collectionNamespace.CollectionName },
78-
{ "changeStreamPreAndPostImages", new BsonDocument() { { "enabled", true } } }
79-
};
80-
81-
var collModOperation = new WriteCommandOperation<BsonDocument>(_collectionNamespace.DatabaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings);
82-
ExecuteOperation(collModOperation);
74+
operation.ChangeStreamPreAndPostImages = new BsonDocument() { { "enabled", true } };
8375
}
76+
77+
ExecuteOperation(operation);
8478
}
8579

8680
protected void Delete(BsonDocument filter)

tests/MongoDB.Driver.Tests/MongoDatabaseImplTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,14 @@ public void CreateCollection_should_execute_a_CreateCollectionOperation_when_opt
397397
var storageEngine = new BsonDocument("awesome", true);
398398
var validatorDocument = BsonDocument.Parse("{ x : 1 }");
399399
var validatorDefinition = (FilterDefinition<BsonDocument>)validatorDocument;
400+
var changeStreamPreAndPostImagesOptions = new ChangeStreamPreAndPostImagesOptions { Enabled = true };
401+
400402
#pragma warning disable 618
401403
var options = new CreateCollectionOptions<BsonDocument>
402404
{
403405
AutoIndexId = false,
404406
Capped = true,
407+
ChangeStreamPreAndPostImagesOptions = changeStreamPreAndPostImagesOptions,
405408
ClusteredIndex = clustered ? new ClusteredIndexOptions<BsonDocument>() : null,
406409
Collation = new Collation("en_US"),
407410
IndexOptionDefaults = new IndexOptionDefaults { StorageEngine = new BsonDocument("x", 1) },
@@ -449,6 +452,7 @@ public void CreateCollection_should_execute_a_CreateCollectionOperation_when_opt
449452
op.AutoIndexId.Should().Be(options.AutoIndexId);
450453
#pragma warning restore
451454
op.Capped.Should().Be(options.Capped);
455+
op.ChangeStreamPreAndPostImages.Should().Be(options.ChangeStreamPreAndPostImagesOptions.BackingDocument);
452456
if (clustered)
453457
{
454458
op.ClusteredIndex.Should().NotBeNull();
@@ -531,6 +535,7 @@ public void CreateCollection_should_execute_a_CreateCollectionOperation_when_opt
531535
op.AutoIndexId.Should().Be(options.AutoIndexId);
532536
#pragma warning restore
533537
op.Capped.Should().Be(options.Capped);
538+
op.ChangeStreamPreAndPostImages.Should().BeNull();
534539
op.ClusteredIndex.Should().BeNull();
535540
op.Collation.Should().BeSameAs(options.Collation);
536541
op.IndexOptionDefaults.ToBsonDocument().Should().Be(options.IndexOptionDefaults.ToBsonDocument());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
{
2+
"description": "createCollection-pre_and_post_images",
3+
"schemaVersion": "1.4",
4+
"runOnRequirements": [
5+
{
6+
"minServerVersion": "6.0",
7+
"serverless": "forbid"
8+
}
9+
],
10+
"createEntities": [
11+
{
12+
"client": {
13+
"id": "client0",
14+
"observeEvents": [
15+
"commandStartedEvent"
16+
]
17+
}
18+
},
19+
{
20+
"database": {
21+
"id": "database0",
22+
"client": "client0",
23+
"databaseName": "papi-tests"
24+
}
25+
},
26+
{
27+
"collection": {
28+
"id": "collection0",
29+
"database": "database0",
30+
"collectionName": "test"
31+
}
32+
}
33+
],
34+
"tests": [
35+
{
36+
"description": "createCollection with changeStreamPreAndPostImages enabled",
37+
"operations": [
38+
{
39+
"name": "dropCollection",
40+
"object": "database0",
41+
"arguments": {
42+
"collection": "test"
43+
}
44+
},
45+
{
46+
"name": "createCollection",
47+
"object": "database0",
48+
"arguments": {
49+
"collection": "test",
50+
"changeStreamPreAndPostImages": {
51+
"enabled": true
52+
}
53+
}
54+
},
55+
{
56+
"name": "assertCollectionExists",
57+
"object": "testRunner",
58+
"arguments": {
59+
"databaseName": "papi-tests",
60+
"collectionName": "test"
61+
}
62+
}
63+
],
64+
"expectEvents": [
65+
{
66+
"client": "client0",
67+
"events": [
68+
{
69+
"commandStartedEvent": {
70+
"command": {
71+
"drop": "test"
72+
},
73+
"databaseName": "papi-tests"
74+
}
75+
},
76+
{
77+
"commandStartedEvent": {
78+
"command": {
79+
"create": "test",
80+
"changeStreamPreAndPostImages": {
81+
"enabled": true
82+
}
83+
},
84+
"databaseName": "papi-tests"
85+
}
86+
}
87+
]
88+
}
89+
]
90+
}
91+
]
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
description: "createCollection-pre_and_post_images"
2+
3+
schemaVersion: "1.4"
4+
5+
runOnRequirements:
6+
- minServerVersion: "6.0"
7+
serverless: forbid
8+
9+
createEntities:
10+
- client:
11+
id: &client0 client0
12+
observeEvents: [ commandStartedEvent ]
13+
- database:
14+
id: &database0 database0
15+
client: *client0
16+
databaseName: &database0Name papi-tests
17+
- collection:
18+
id: &collection0 collection0
19+
database: *database0
20+
collectionName: &collection0Name test
21+
22+
tests:
23+
- description: "createCollection with changeStreamPreAndPostImages enabled"
24+
operations:
25+
- name: dropCollection
26+
object: *database0
27+
arguments:
28+
collection: *collection0Name
29+
- name: createCollection
30+
object: *database0
31+
arguments:
32+
collection: *collection0Name
33+
changeStreamPreAndPostImages: { enabled: true }
34+
- name: assertCollectionExists
35+
object: testRunner
36+
arguments:
37+
databaseName: *database0Name
38+
collectionName: *collection0Name
39+
expectEvents:
40+
- client: *client0
41+
events:
42+
- commandStartedEvent:
43+
command:
44+
drop: *collection0Name
45+
databaseName: *database0Name
46+
- commandStartedEvent:
47+
command:
48+
create: *collection0Name
49+
changeStreamPreAndPostImages: { enabled: true }
50+
databaseName: *database0Name

0 commit comments

Comments
 (0)