PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 74 lines | 53 code | 7 blank | 14 comment | 2 complexity | 04e502065c9fef47dc0f14f50c7279fd 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.Threading.Tasks;
  16. using FluentAssertions;
  17. using MongoDB.Bson;
  18. namespace MongoDB.Driver.Tests.Specifications.crud
  19. {
  20. public class CountTest : CrudOperationWithResultTestBase<long>
  21. {
  22. private BsonDocument _filter;
  23. private CountOptions _options = new CountOptions();
  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 "skip":
  32. _options.Skip = value.ToInt64();
  33. return true;
  34. case "limit":
  35. _options.Limit = value.ToInt64();
  36. return true;
  37. case "collation":
  38. _options.Collation = Collation.FromBsonDocument(value.AsBsonDocument);
  39. return true;
  40. }
  41. return false;
  42. }
  43. protected override long ConvertExpectedResult(BsonValue expectedResult)
  44. {
  45. return expectedResult.ToInt64();
  46. }
  47. protected override long ExecuteAndGetResult(IMongoDatabase database, IMongoCollection<BsonDocument> collection, bool async)
  48. {
  49. if (async)
  50. {
  51. #pragma warning disable 618
  52. return collection.CountAsync(_filter, _options).GetAwaiter().GetResult();
  53. #pragma warning restore
  54. }
  55. else
  56. {
  57. #pragma warning disable 618
  58. return collection.Count(_filter, _options);
  59. #pragma warning restore
  60. }
  61. }
  62. protected override void VerifyResult(long actualResult, long expectedResult)
  63. {
  64. actualResult.Should().Be(expectedResult);
  65. }
  66. }
  67. }