PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 123 lines | 94 code | 14 blank | 15 comment | 6 complexity | 6e8950f0767fbcc25510d77a9cea8672 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.Collections.Generic;
  17. using MongoDB.Bson;
  18. namespace MongoDB.Driver.Tests.Specifications.command_monitoring
  19. {
  20. public class BulkWriteTest : CrudOperationTestBase
  21. {
  22. private List<WriteModel<BsonDocument>> _requests;
  23. private BulkWriteOptions _options = new BulkWriteOptions();
  24. protected override void Execute(IMongoCollection<BsonDocument> collection, bool async)
  25. {
  26. if (collection.Settings.WriteConcern == null)
  27. {
  28. collection = collection.WithWriteConcern(WriteConcern.Acknowledged);
  29. }
  30. if (async)
  31. {
  32. collection.BulkWriteAsync(_requests, _options).GetAwaiter().GetResult();
  33. }
  34. else
  35. {
  36. collection.BulkWrite(_requests, _options);
  37. }
  38. }
  39. protected override bool TrySetArgument(string name, BsonValue value)
  40. {
  41. switch (name)
  42. {
  43. case "requests":
  44. _requests = ParseRequests(value.AsBsonArray);
  45. return true;
  46. case "options":
  47. _options = ParseOptions(value.AsBsonDocument);
  48. return true;
  49. }
  50. return false;
  51. }
  52. // private methods
  53. private BulkWriteOptions ParseOptions(BsonDocument value)
  54. {
  55. var options = new BulkWriteOptions();
  56. foreach (var option in value.Elements)
  57. {
  58. switch (option.Name)
  59. {
  60. case "ordered":
  61. options.IsOrdered = option.Value.ToBoolean();
  62. break;
  63. default:
  64. throw new FormatException($"Unexpected option: ${option.Name}.");
  65. }
  66. }
  67. return options;
  68. }
  69. private List<WriteModel<BsonDocument>> ParseRequests(BsonArray requests)
  70. {
  71. var result = new List<WriteModel<BsonDocument>>();
  72. foreach (BsonDocument request in requests)
  73. {
  74. var name = request["name"].AsString;
  75. var arguments = request["arguments"].AsBsonDocument;
  76. switch (name)
  77. {
  78. case "deleteOne":
  79. result.Add(ParseDeleteOne(arguments));
  80. break;
  81. case "insertOne":
  82. result.Add(ParseInsertOne(arguments));
  83. break;
  84. case "updateOne":
  85. result.Add(ParseUpdateOne(arguments));
  86. break;
  87. }
  88. }
  89. return result;
  90. }
  91. private DeleteOneModel<BsonDocument> ParseDeleteOne(BsonDocument request)
  92. {
  93. var filter = new BsonDocumentFilterDefinition<BsonDocument>((BsonDocument)request["filter"]);
  94. return new DeleteOneModel<BsonDocument>(filter);
  95. }
  96. private InsertOneModel<BsonDocument> ParseInsertOne(BsonDocument request)
  97. {
  98. return new InsertOneModel<BsonDocument>((BsonDocument)request["document"]);
  99. }
  100. private UpdateOneModel<BsonDocument> ParseUpdateOne(BsonDocument request)
  101. {
  102. var filter = new BsonDocumentFilterDefinition<BsonDocument>((BsonDocument)request["filter"]);
  103. var update = new BsonDocumentUpdateDefinition<BsonDocument>((BsonDocument)request["update"]);
  104. var model = new UpdateOneModel<BsonDocument>(filter, update);
  105. model.IsUpsert = request.GetValue("upsert", false).ToBoolean();
  106. return model;
  107. }
  108. }
  109. }