PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Driver.Tests/Specifications/crud/FindOneAndReplaceTest.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 110 lines | 84 code | 12 blank | 14 comment | 5 complexity | c0cc6bf8074dd647d388fcbb13331d4a MD5 | raw file
Possible License(s): Apache-2.0
  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. using System;
  16. using System.Linq;
  17. using FluentAssertions;
  18. using MongoDB.Bson;
  19. using MongoDB.Driver.Core.Misc;
  20. namespace MongoDB.Driver.Tests.Specifications.crud
  21. {
  22. public class FindOneAndReplaceTest : CrudOperationWithResultTestBase<BsonDocument>
  23. {
  24. private BsonDocument _filter;
  25. private BsonDocument _replacement;
  26. private FindOneAndReplaceOptions<BsonDocument> _options = new FindOneAndReplaceOptions<BsonDocument>();
  27. protected override bool TrySetArgument(string name, BsonValue value)
  28. {
  29. switch (name)
  30. {
  31. case "filter":
  32. _filter = (BsonDocument)value;
  33. return true;
  34. case "replacement":
  35. _replacement = (BsonDocument)value;
  36. return true;
  37. case "projection":
  38. _options.Projection = (BsonDocument)value;
  39. return true;
  40. case "sort":
  41. _options.Sort = (BsonDocument)value;
  42. return true;
  43. case "upsert":
  44. _options.IsUpsert = value.ToBoolean();
  45. return true;
  46. case "returnDocument":
  47. _options.ReturnDocument = (ReturnDocument)Enum.Parse(typeof(ReturnDocument), value.ToString());
  48. return true;
  49. case "collation":
  50. _options.Collation = Collation.FromBsonDocument(value.AsBsonDocument);
  51. return true;
  52. case "hint":
  53. _options.Hint = value;
  54. return true;
  55. }
  56. return false;
  57. }
  58. protected override BsonDocument ConvertExpectedResult(BsonValue expectedResult)
  59. {
  60. if (expectedResult.IsBsonNull)
  61. {
  62. return null;
  63. }
  64. return (BsonDocument)expectedResult;
  65. }
  66. protected override BsonDocument ExecuteAndGetResult(IMongoDatabase database, IMongoCollection<BsonDocument> collection, bool async)
  67. {
  68. if (async)
  69. {
  70. return collection.FindOneAndReplaceAsync(_filter, _replacement, _options).GetAwaiter().GetResult();
  71. }
  72. else
  73. {
  74. return collection.FindOneAndReplace(_filter, _replacement, _options);
  75. }
  76. }
  77. protected override void VerifyResult(BsonDocument actualResult, BsonDocument expectedResult)
  78. {
  79. actualResult.Should().Be(expectedResult);
  80. }
  81. protected override void VerifyCollection(IMongoCollection<BsonDocument> collection, BsonArray expectedData)
  82. {
  83. var data = collection.FindSync("{}").ToList();
  84. if (ClusterDescription.Servers[0].Version < new SemanticVersion(2, 6, 0) && _options.IsUpsert)
  85. {
  86. foreach (var doc in data)
  87. {
  88. doc.Remove("_id");
  89. }
  90. foreach (var doc in expectedData.Cast<BsonDocument>())
  91. {
  92. doc.Remove("_id");
  93. }
  94. }
  95. data.Should().BeEquivalentTo(expectedData);
  96. }
  97. }
  98. }