PageRenderTime 49ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 99 lines | 77 code | 8 blank | 14 comment | 7 complexity | 0cb6f3fcb0eaf8c0fa16ffd9ecd9cc6c 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 FluentAssertions;
  18. using MongoDB.Bson;
  19. using MongoDB.Driver.Core.Misc;
  20. using MongoDB.Driver.Core.TestHelpers.XunitExtensions;
  21. namespace MongoDB.Driver.Tests.Specifications.crud
  22. {
  23. public class AggregateTest : CrudOperationWithResultTestBase<List<BsonDocument>>
  24. {
  25. private List<BsonDocument> _stages;
  26. private AggregateOptions _options = new AggregateOptions();
  27. public override void SkipIfNotSupported(BsonDocument arguments)
  28. {
  29. var lastStage = arguments["pipeline"].AsBsonArray.Last().AsBsonDocument;
  30. var lastStageName = lastStage.GetElement(0).Name;
  31. if (lastStageName == "$merge")
  32. {
  33. RequireServer.Check().Supports(Feature.AggregateMerge);
  34. }
  35. }
  36. protected override bool TrySetArgument(string name, BsonValue value)
  37. {
  38. switch (name)
  39. {
  40. case "pipeline":
  41. _stages = ((BsonArray)value).Cast<BsonDocument>().ToList();
  42. return true;
  43. case "allowDiskUse":
  44. _options.AllowDiskUse = value.ToBoolean();
  45. return true;
  46. case "batchSize":
  47. _options.BatchSize = (int)value;
  48. return true;
  49. case "collation":
  50. _options.Collation = Collation.FromBsonDocument(value.AsBsonDocument);
  51. return true;
  52. }
  53. return false;
  54. }
  55. protected override List<BsonDocument> ConvertExpectedResult(BsonValue expectedResult)
  56. {
  57. return ((BsonArray)expectedResult).Select(x => (BsonDocument)x).ToList();
  58. }
  59. protected override List<BsonDocument> ExecuteAndGetResult(IMongoDatabase database, IMongoCollection<BsonDocument> collection, bool async)
  60. {
  61. if (collection == null)
  62. {
  63. if (async)
  64. {
  65. var cursor = database.AggregateAsync<BsonDocument>(_stages, _options).GetAwaiter().GetResult();
  66. return cursor.ToListAsync().GetAwaiter().GetResult();
  67. }
  68. else
  69. {
  70. return database.Aggregate<BsonDocument>(_stages, _options).ToList();
  71. }
  72. }
  73. else
  74. {
  75. if (async)
  76. {
  77. var cursor = collection.AggregateAsync<BsonDocument>(_stages, _options).GetAwaiter().GetResult();
  78. return cursor.ToListAsync().GetAwaiter().GetResult();
  79. }
  80. else
  81. {
  82. return collection.Aggregate<BsonDocument>(_stages, _options).ToList();
  83. }
  84. }
  85. }
  86. protected override void VerifyResult(List<BsonDocument> actualResult, List<BsonDocument> expectedResult)
  87. {
  88. actualResult.Should().Equal(expectedResult);
  89. }
  90. }
  91. }