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

http://github.com/mongodb/mongo-csharp-driver · C# · 78 lines · 56 code · 8 blank · 14 comment · 3 complexity · 8dade46d2b0471079fa0d2bf02bb480f MD5 · raw file

  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.Threading.Tasks;
  16. using FluentAssertions;
  17. using MongoDB.Bson;
  18. namespace MongoDB.Driver.Tests.Specifications.crud
  19. {
  20. public class FindOneAndDeleteTest : CrudOperationWithResultTestBase<BsonDocument>
  21. {
  22. private BsonDocument _filter;
  23. private FindOneAndDeleteOptions<BsonDocument> _options = new FindOneAndDeleteOptions<BsonDocument>();
  24. protected override bool TrySetArgument(string name, BsonValue value)
  25. {
  26. switch (name)
  27. {
  28. case "filter":
  29. _filter = (BsonDocument)value;
  30. return true;
  31. case "projection":
  32. _options.Projection = (BsonDocument)value;
  33. return true;
  34. case "sort":
  35. _options.Sort = (BsonDocument)value;
  36. return true;
  37. case "collation":
  38. _options.Collation = Collation.FromBsonDocument(value.AsBsonDocument);
  39. return true;
  40. case "hint":
  41. _options.Hint = value;
  42. return true;
  43. }
  44. return false;
  45. }
  46. protected override BsonDocument ConvertExpectedResult(BsonValue expectedResult)
  47. {
  48. if (expectedResult.IsBsonNull)
  49. {
  50. return null;
  51. }
  52. return (BsonDocument)expectedResult;
  53. }
  54. protected override BsonDocument ExecuteAndGetResult(IMongoDatabase database, IMongoCollection<BsonDocument> collection, bool async)
  55. {
  56. if (async)
  57. {
  58. return collection.FindOneAndDeleteAsync(_filter, _options).GetAwaiter().GetResult();
  59. }
  60. else
  61. {
  62. return collection.FindOneAndDelete(_filter, _options);
  63. }
  64. }
  65. protected override void VerifyResult(BsonDocument actualResult, BsonDocument expectedResult)
  66. {
  67. actualResult.Should().Be(expectedResult);
  68. }
  69. }
  70. }