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

/tests/MongoDB.Driver.Tests/PipelineDefinitionTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 104 lines | 74 code | 16 blank | 14 comment | 0 complexity | 9e26ec72861d9988ad7ba47cbf1b065c 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.Bson.Serialization;
  19. using MongoDB.Bson.Serialization.Attributes;
  20. using Xunit;
  21. namespace MongoDB.Driver.Tests
  22. {
  23. public class PipelineStagePipelineDefinitionTests
  24. {
  25. [Fact]
  26. public void Constructor_should_verify_the_inputs_and_outputs_of_the_stages_and_throw_when_intermediate_stage_is_invalid()
  27. {
  28. var stages = new IPipelineStageDefinition[]
  29. {
  30. new BsonDocumentPipelineStageDefinition<Person, BsonDocument>(new BsonDocument()),
  31. new BsonDocumentPipelineStageDefinition<BsonDocument, Pet>(new BsonDocument()),
  32. new BsonDocumentPipelineStageDefinition<BsonDocument, Person>(new BsonDocument())
  33. };
  34. var exception = Record.Exception(() => new PipelineStagePipelineDefinition<Person, Person>(stages));
  35. var e = exception.Should().BeOfType<ArgumentException>().Subject;
  36. e.ParamName.Should().Be("stages");
  37. e.Message.Should().Contain($"The input type to stage[2] was expected to be {typeof(Pet)}, but was {typeof(BsonDocument)}.");
  38. }
  39. [Fact]
  40. public void Constructor_should_verify_the_inputs_and_outputs_of_the_stages_and_throw_when_final_stage_is_invalid()
  41. {
  42. var stages = new IPipelineStageDefinition[]
  43. {
  44. new BsonDocumentPipelineStageDefinition<Person, BsonDocument>(new BsonDocument()),
  45. new BsonDocumentPipelineStageDefinition<BsonDocument, Pet>(new BsonDocument()),
  46. new BsonDocumentPipelineStageDefinition<Pet, BsonDocument>(new BsonDocument())
  47. };
  48. var exception = Record.Exception(() => new PipelineStagePipelineDefinition<Person, Person>(stages));
  49. var e = exception.Should().BeOfType<ArgumentException>().Subject;
  50. e.ParamName.Should().Be("stages");
  51. e.Message.Should().Contain($"The output type to the last stage was expected to be {typeof(Person)}, but was {typeof(BsonDocument)}.");
  52. }
  53. [Fact]
  54. public void Constructor_should_verify_the_inputs_and_outputs_of_the_stages()
  55. {
  56. var stages = new IPipelineStageDefinition[]
  57. {
  58. new BsonDocumentPipelineStageDefinition<Person, BsonDocument>(new BsonDocument()),
  59. new BsonDocumentPipelineStageDefinition<BsonDocument, Pet>(new BsonDocument()),
  60. new BsonDocumentPipelineStageDefinition<Pet, Person>(new BsonDocument())
  61. };
  62. Action act = () => new PipelineStagePipelineDefinition<Person, Person>(stages);
  63. act.ShouldNotThrow<ArgumentException>();
  64. }
  65. private void Assert<TDocument>(ProjectionDefinition<TDocument> projection, string expectedJson)
  66. {
  67. var documentSerializer = BsonSerializer.SerializerRegistry.GetSerializer<TDocument>();
  68. var renderedProjection = projection.Render(documentSerializer, BsonSerializer.SerializerRegistry);
  69. renderedProjection.Should().Be(expectedJson);
  70. }
  71. private ProjectionDefinitionBuilder<TDocument> CreateSubject<TDocument>()
  72. {
  73. return new ProjectionDefinitionBuilder<TDocument>();
  74. }
  75. private class Person
  76. {
  77. [BsonElement("fn")]
  78. public string FirstName { get; set; }
  79. [BsonElement("pets")]
  80. public Pet[] Pets { get; set; }
  81. }
  82. private class Pet
  83. {
  84. [BsonElement("name")]
  85. public string Name { get; set; }
  86. }
  87. }
  88. }