PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 82 lines | 61 code | 7 blank | 14 comment | 2 complexity | a88261a05d264542c5e7ccbc7bc4046e 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.Collections.Generic;
  16. using System.Linq;
  17. using System.Threading.Tasks;
  18. using FluentAssertions;
  19. using MongoDB.Bson;
  20. namespace MongoDB.Driver.Tests.Specifications.crud
  21. {
  22. public class FindTest : CrudOperationWithResultTestBase<List<BsonDocument>>
  23. {
  24. private BsonDocument _filter;
  25. private FindOptions<BsonDocument> _options = new FindOptions<BsonDocument>();
  26. protected override bool TrySetArgument(string name, BsonValue value)
  27. {
  28. switch (name)
  29. {
  30. case "filter":
  31. _filter = (BsonDocument)value;
  32. return true;
  33. case "allowDiskUse":
  34. _options.AllowDiskUse = value.ToBoolean();
  35. return true;
  36. case "sort":
  37. _options.Sort = value.ToBsonDocument();
  38. return true;
  39. case "limit":
  40. _options.Limit = value.ToInt32();
  41. return true;
  42. case "skip":
  43. _options.Skip = value.ToInt32();
  44. return true;
  45. case "batchSize":
  46. _options.BatchSize = value.ToInt32();
  47. return true;
  48. case "collation":
  49. _options.Collation = Collation.FromBsonDocument(value.AsBsonDocument);
  50. return true;
  51. }
  52. return false;
  53. }
  54. protected override List<BsonDocument> ConvertExpectedResult(BsonValue expectedResult)
  55. {
  56. return ((BsonArray)expectedResult).Select(x => x.ToBsonDocument()).ToList();
  57. }
  58. protected override List<BsonDocument> ExecuteAndGetResult(IMongoDatabase database, IMongoCollection<BsonDocument> collection, bool async)
  59. {
  60. if (async)
  61. {
  62. var cursor = collection.FindAsync(_filter, _options).GetAwaiter().GetResult();
  63. return cursor.ToListAsync().GetAwaiter().GetResult();
  64. }
  65. else
  66. {
  67. return collection.FindSync(_filter, _options).ToList();
  68. }
  69. }
  70. protected override void VerifyResult(List<BsonDocument> actualResult, List<BsonDocument> expectedResult)
  71. {
  72. actualResult.Should().Equal(expectedResult);
  73. }
  74. }
  75. }