PageRenderTime 71ms CodeModel.GetById 40ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 113 lines | 90 code | 9 blank | 14 comment | 6 complexity | 6e2e6a52d17cdbb5144ad4a4d70d87ce 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 FluentAssertions;
  17. using MongoDB.Bson;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. namespace MongoDB.Driver.Tests.Specifications.crud
  21. {
  22. public class FindOneAndUpdateTest : CrudOperationWithResultTestBase<BsonDocument>
  23. {
  24. private BsonDocument _filter;
  25. private FindOneAndUpdateOptions<BsonDocument> _options = new FindOneAndUpdateOptions<BsonDocument>();
  26. private BsonValue _update;
  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 "update":
  35. _update = 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 "arrayFilters":
  53. var arrayFilters = new List<ArrayFilterDefinition>();
  54. foreach (BsonDocument arrayFilterDocument in value.AsBsonArray)
  55. {
  56. var arrayFilter = new BsonDocumentArrayFilterDefinition<BsonDocument>(arrayFilterDocument);
  57. arrayFilters.Add(arrayFilter);
  58. }
  59. _options.ArrayFilters = arrayFilters;
  60. return true;
  61. case "hint":
  62. _options.Hint = value;
  63. return true;
  64. }
  65. return false;
  66. }
  67. protected override BsonDocument ConvertExpectedResult(BsonValue expectedResult)
  68. {
  69. if (expectedResult.IsBsonNull)
  70. {
  71. return null;
  72. }
  73. return (BsonDocument)expectedResult;
  74. }
  75. protected override BsonDocument ExecuteAndGetResult(IMongoDatabase database, IMongoCollection<BsonDocument> collection, bool async)
  76. {
  77. UpdateDefinition<BsonDocument> updateDefinition = null;
  78. if (_update is BsonDocument updateDocument)
  79. {
  80. updateDefinition = new BsonDocumentUpdateDefinition<BsonDocument>(updateDocument);
  81. }
  82. else if (_update is BsonArray stages)
  83. {
  84. var pipeline = new BsonDocumentStagePipelineDefinition<BsonDocument, BsonDocument>(stages.Cast<BsonDocument>());
  85. updateDefinition = new PipelineUpdateDefinition<BsonDocument>(pipeline);
  86. }
  87. if (async)
  88. {
  89. return collection
  90. .FindOneAndUpdateAsync(_filter, updateDefinition, _options)
  91. .GetAwaiter()
  92. .GetResult();
  93. }
  94. else
  95. {
  96. return collection.FindOneAndUpdate(_filter, updateDefinition, _options);
  97. }
  98. }
  99. protected override void VerifyResult(BsonDocument actualResult, BsonDocument expectedResult)
  100. {
  101. actualResult.Should().Be(expectedResult);
  102. }
  103. }
  104. }