Skip to content

Commit f48202b

Browse files
committed
CSHARP-1439: Add document validation support to create collection methods.
1 parent 0bdc165 commit f48202b

File tree

10 files changed

+329
-5
lines changed

10 files changed

+329
-5
lines changed

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

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,65 @@ public void CreateCommand_should_return_expected_result_when_UsePowerOf2Sizes_is
230230
result.Should().Be(expectedResult);
231231
}
232232

233+
[Test]
234+
public void CreateCommand_should_return_expected_result_when_ValidationAction_is_set(
235+
[Values(DocumentValidationAction.Error, DocumentValidationAction.Warn)]
236+
DocumentValidationAction value)
237+
{
238+
var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
239+
{
240+
ValidationAction = value
241+
};
242+
var expectedResult = new BsonDocument
243+
{
244+
{ "create", _collectionNamespace.CollectionName },
245+
{ "validationAction", value.ToString().ToLower() }
246+
};
247+
248+
var result = subject.CreateCommand();
249+
250+
result.Should().Be(expectedResult);
251+
}
252+
253+
[Test]
254+
public void CreateCommand_should_return_expected_result_when_ValidationLevel_is_set(
255+
[Values(DocumentValidationLevel.Moderate, DocumentValidationLevel.Off)]
256+
DocumentValidationLevel value)
257+
{
258+
var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
259+
{
260+
ValidationLevel = value
261+
};
262+
var expectedResult = new BsonDocument
263+
{
264+
{ "create", _collectionNamespace.CollectionName },
265+
{ "validationLevel", value.ToString().ToLower() }
266+
};
267+
268+
var result = subject.CreateCommand();
269+
270+
result.Should().Be(expectedResult);
271+
}
272+
273+
[Test]
274+
public void CreateCommand_should_return_expected_result_when_Validator_is_set()
275+
{
276+
var value = new BsonDocument("x", 1);
277+
var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
278+
{
279+
Validator = value
280+
};
281+
var expectedResult = new BsonDocument
282+
{
283+
{ "create", _collectionNamespace.CollectionName },
284+
{ "validator", value }
285+
};
286+
287+
var result = subject.CreateCommand();
288+
289+
result.Should().Be(expectedResult);
290+
}
291+
233292
[Test]
234293
[RequiresServer("DropCollection")]
235294
public void Execute_should_create_collection(
@@ -455,6 +514,46 @@ public void UsePowerOf2Sizes_should_work()
455514
subject.UsePowerOf2Sizes.Should().BeTrue();
456515
}
457516

517+
[Test]
518+
public void ValidationAction_should_work(
519+
[Values(null, DocumentValidationAction.Error, DocumentValidationAction.Warn)]
520+
DocumentValidationAction? value)
521+
{
522+
var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings);
523+
subject.ValidationAction.Should().BeNull();
524+
525+
subject.ValidationAction = value;
526+
527+
subject.ValidationAction.Should().Be(value);
528+
}
529+
530+
[Test]
531+
public void ValidationLevel_should_work(
532+
[Values(null, DocumentValidationLevel.Moderate, DocumentValidationLevel.Off)]
533+
DocumentValidationLevel? value)
534+
{
535+
var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings);
536+
subject.ValidationLevel.Should().BeNull();
537+
538+
subject.ValidationLevel = value;
539+
540+
subject.ValidationLevel.Should().Be(value);
541+
}
542+
543+
[Test]
544+
public void Validator_should_work(
545+
[Values(null, "{ x : 1 }")]
546+
string json)
547+
{
548+
var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings);
549+
var value = json == null ? null : BsonDocument.Parse(json);
550+
subject.Validator.Should().BeNull();
551+
552+
subject.Validator = value;
553+
554+
subject.Validator.Should().Be(value);
555+
}
556+
458557
// helper methods
459558
private void DropCollection()
460559
{

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public class CreateCollectionOperation : IWriteOperation<BsonDocument>
3838
private readonly MessageEncoderSettings _messageEncoderSettings;
3939
private BsonDocument _storageEngine;
4040
private bool? _usePowerOf2Sizes;
41+
private DocumentValidationAction? _validationAction;
42+
private DocumentValidationLevel? _validationLevel;
43+
private BsonDocument _validator;
4144

4245
// constructors
4346
/// <summary>
@@ -148,6 +151,42 @@ public bool? UsePowerOf2Sizes
148151
set { _usePowerOf2Sizes = value; }
149152
}
150153

154+
/// <summary>
155+
/// Gets or sets the validation action.
156+
/// </summary>
157+
/// <value>
158+
/// The validation action.
159+
/// </value>
160+
public DocumentValidationAction? ValidationAction
161+
{
162+
get { return _validationAction; }
163+
set { _validationAction = value; }
164+
}
165+
166+
/// <summary>
167+
/// Gets or sets the validation level.
168+
/// </summary>
169+
/// <value>
170+
/// The validation level.
171+
/// </value>
172+
public DocumentValidationLevel? ValidationLevel
173+
{
174+
get { return _validationLevel; }
175+
set { _validationLevel = value; }
176+
}
177+
178+
/// <summary>
179+
/// Gets or sets the validator.
180+
/// </summary>
181+
/// <value>
182+
/// The validator.
183+
/// </value>
184+
public BsonDocument Validator
185+
{
186+
get { return _validator; }
187+
set { _validator = value; }
188+
}
189+
151190
// methods
152191
internal BsonDocument CreateCommand()
153192
{
@@ -159,7 +198,10 @@ internal BsonDocument CreateCommand()
159198
{ "size", () => _maxSize.Value, _maxSize.HasValue },
160199
{ "max", () => _maxDocuments.Value, _maxDocuments.HasValue },
161200
{ "flags", () => _usePowerOf2Sizes.Value ? 1 : 0, _usePowerOf2Sizes.HasValue},
162-
{ "storageEngine", () => _storageEngine, _storageEngine != null }
201+
{ "storageEngine", () => _storageEngine, _storageEngine != null },
202+
{ "validator", _validator, _validator != null },
203+
{ "validationAction", () => _validationAction.Value.ToString().ToLower(), _validationAction.HasValue },
204+
{ "validationLevel", () => _validationLevel.Value.ToString().ToLower(), _validationLevel.HasValue }
163205
};
164206
}
165207

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* Copyright 2015 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+
namespace MongoDB.Driver.Core.Operations
17+
{
18+
/// <summary>
19+
/// Represents the document validation action.
20+
/// </summary>
21+
public enum DocumentValidationAction
22+
{
23+
/// <summary>
24+
/// Validation failures result in an error.
25+
/// </summary>
26+
Error = 0, // the default
27+
/// <summary>
28+
/// Validation failures result in a warning.
29+
/// </summary>
30+
Warn
31+
}
32+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* Copyright 2015 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+
namespace MongoDB.Driver.Core.Operations
17+
{
18+
/// <summary>
19+
/// Represents the document validation level.
20+
/// </summary>
21+
public enum DocumentValidationLevel
22+
{
23+
/// <summary>
24+
/// Strict document validation.
25+
/// </summary>
26+
Strict = 0, // the default
27+
/// <summary>
28+
/// Moderate document validation.
29+
/// </summary>
30+
Moderate,
31+
/// <summary>
32+
/// No document validation.
33+
/// </summary>
34+
Off
35+
}
36+
}

src/MongoDB.Driver.Core/MongoDB.Driver.Core.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@
137137
<Compile Include="Core\Misc\SemaphoreSlimRequest.cs" />
138138
<Compile Include="Core\Operations\CreateIndexesUsingCommandOperation.cs" />
139139
<Compile Include="Core\Operations\CreateIndexesUsingInsertOperation.cs" />
140+
<Compile Include="Core\Operations\DocumentValidationAction.cs" />
141+
<Compile Include="Core\Operations\DocumentValidationLevel.cs" />
140142
<Compile Include="Core\Operations\ListCollectionsUsingCommandOperation.cs" />
141143
<Compile Include="Core\Operations\ListCollectionsUsingQueryOperation.cs" />
142144
<Compile Include="Core\Operations\ListIndexesUsingCommandOperation.cs" />

src/MongoDB.Driver.Tests/MongoDatabaseImplTests.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.Threading.Tasks;
2020
using FluentAssertions;
2121
using MongoDB.Bson;
22+
using MongoDB.Bson.Serialization;
2223
using MongoDB.Bson.Serialization.Attributes;
2324
using MongoDB.Driver.Core.Bindings;
2425
using MongoDB.Driver.Core.Clusters;
@@ -77,7 +78,10 @@ public async Task CreateCollectionAsync_should_execute_the_CreateCollectionOpera
7778
MaxDocuments = 10,
7879
MaxSize = 11,
7980
StorageEngine = storageEngine,
80-
UsePowerOf2Sizes = false
81+
UsePowerOf2Sizes = false,
82+
ValidationAction = DocumentValidationAction.Warn,
83+
ValidationLevel = DocumentValidationLevel.Off,
84+
Validator = new BsonDocument("x", 1)
8185
};
8286
await _subject.CreateCollectionAsync("bar", options, CancellationToken.None);
8387

@@ -92,6 +96,12 @@ public async Task CreateCollectionAsync_should_execute_the_CreateCollectionOpera
9296
op.MaxSize.Should().Be(options.MaxSize);
9397
op.StorageEngine.Should().Be(storageEngine);
9498
op.UsePowerOf2Sizes.Should().Be(options.UsePowerOf2Sizes);
99+
op.ValidationAction.Should().Be(options.ValidationAction);
100+
op.ValidationLevel.Should().Be(options.ValidationLevel);
101+
var serializerRegistry = options.SerializerRegistry ?? BsonSerializer.SerializerRegistry;
102+
var documentSerializer = options.DocumentSerializer ?? serializerRegistry.GetSerializer<BsonDocument>();
103+
var renderedValidator = options.Validator.Render(documentSerializer, serializerRegistry);
104+
op.Validator.Should().Be(renderedValidator);
95105
}
96106

97107
[Test]

0 commit comments

Comments
 (0)