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

/tests/MongoDB.Driver.Tests/Specifications/command-monitoring/CrudOperationTestBase.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 68 lines | 42 code | 10 blank | 16 comment | 2 complexity | 2e0be5d507a1ba9bbe4d585e749358e1 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 System.Threading.Tasks;
  17. using FluentAssertions;
  18. using MongoDB.Bson;
  19. using MongoDB.Driver.Core.Clusters;
  20. using MongoDB.Driver.Core.Misc;
  21. namespace MongoDB.Driver.Tests.Specifications.command_monitoring
  22. {
  23. public abstract class CrudOperationTestBase : ICrudOperationTest
  24. {
  25. protected ClusterDescription ClusterDescription { get; private set; }
  26. public virtual bool CanExecute(ClusterDescription clusterDescription, BsonDocument arguments, out string reason)
  27. {
  28. reason = null;
  29. return true;
  30. }
  31. public void Execute(ClusterDescription clusterDescription, IMongoDatabase database, IMongoCollection<BsonDocument> collection, BsonDocument arguments, bool async)
  32. {
  33. ClusterDescription = clusterDescription;
  34. foreach (var argument in arguments.Elements)
  35. {
  36. if (!TrySetArgument(argument.Name, argument.Value))
  37. {
  38. throw new NotImplementedException("The argument " + argument.Name + " has not been implemented in " + GetType());
  39. }
  40. }
  41. Execute(collection, async);
  42. }
  43. public virtual BsonDocument MassageReply(BsonDocument reply)
  44. {
  45. var massagedReply = new BsonDocument(reply);
  46. BsonValue ok;
  47. if (massagedReply.TryGetValue("ok", out ok))
  48. {
  49. // have to force every ok value into a double since the server
  50. // hasn't chosen a consistent representation.
  51. massagedReply["ok"] = ok.ToDouble();
  52. }
  53. return massagedReply;
  54. }
  55. protected abstract bool TrySetArgument(string name, BsonValue value);
  56. protected abstract void Execute(IMongoCollection<BsonDocument> collection, bool async);
  57. }
  58. }