PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 116 lines | 84 code | 18 blank | 14 comment | 10 complexity | cba01aa20117e1a52229b04ec9362161 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 MongoDB.Driver.Core.Clusters;
  19. namespace MongoDB.Driver.Tests.Specifications.crud
  20. {
  21. public abstract class CrudOperationTestBase : ICrudOperationTest
  22. {
  23. public Exception ActualException { get; set; }
  24. protected ClusterDescription ClusterDescription { get; private set; }
  25. public virtual void SkipIfNotSupported(BsonDocument arguments)
  26. {
  27. }
  28. public void Execute(
  29. ClusterDescription clusterDescription,
  30. IMongoDatabase database,
  31. IMongoCollection<BsonDocument> collection,
  32. BsonDocument arguments,
  33. BsonDocument outcome,
  34. bool isErrorExpected,
  35. bool async)
  36. {
  37. ClusterDescription = clusterDescription;
  38. foreach (var argument in arguments.Elements)
  39. {
  40. if (!TrySetArgument(argument.Name, argument.Value))
  41. {
  42. throw new NotImplementedException("The argument " + argument.Name + " has not been implemented in " + GetType());
  43. }
  44. }
  45. try
  46. {
  47. Execute(database, collection, outcome, async);
  48. }
  49. catch (Exception ex) when (isErrorExpected)
  50. {
  51. ActualException = ex;
  52. }
  53. AssertOutcome(outcome, database, collection);
  54. }
  55. protected virtual void AssertOutcome(BsonDocument outcome, IMongoDatabase database, IMongoCollection<BsonDocument> collection)
  56. {
  57. if (outcome != null && outcome.Contains("collection"))
  58. {
  59. var collectionToVerify = collection;
  60. if (outcome["collection"].AsBsonDocument.Contains("name"))
  61. {
  62. collectionToVerify = database.GetCollection<BsonDocument>(outcome["collection"]["name"].ToString());
  63. }
  64. VerifyCollection(collectionToVerify, (BsonArray)outcome["collection"]["data"]);
  65. }
  66. }
  67. protected virtual bool TrySetArgument(string name, BsonValue value)
  68. {
  69. return false;
  70. }
  71. protected abstract void Execute(IMongoDatabase database, IMongoCollection<BsonDocument> collection, BsonDocument outcome, bool async);
  72. protected virtual void VerifyCollection(IMongoCollection<BsonDocument> collection, BsonArray expectedData)
  73. {
  74. var data = collection.FindSync("{}").ToList();
  75. data.Should().BeEquivalentTo(expectedData);
  76. }
  77. }
  78. public abstract class CrudOperationWithResultTestBase<TResult> : CrudOperationTestBase
  79. {
  80. private TResult _result;
  81. protected sealed override void Execute(IMongoDatabase database, IMongoCollection<BsonDocument> collection, BsonDocument outcome, bool async)
  82. {
  83. _result = ExecuteAndGetResult(database, collection, async);
  84. }
  85. protected override void AssertOutcome(BsonDocument outcome, IMongoDatabase database, IMongoCollection<BsonDocument> collection)
  86. {
  87. if (outcome != null && outcome.Contains("result") && ActualException == null)
  88. {
  89. var expectedResult = ConvertExpectedResult(outcome["result"]);
  90. VerifyResult(_result, expectedResult);
  91. }
  92. base.AssertOutcome(outcome, database, collection);
  93. }
  94. protected abstract TResult ConvertExpectedResult(BsonValue expectedResult);
  95. protected abstract TResult ExecuteAndGetResult(IMongoDatabase database, IMongoCollection<BsonDocument> collection, bool async);
  96. protected abstract void VerifyResult(TResult actualResult, TResult expectedResult);
  97. }
  98. }