Skip to content

Commit

Permalink
CSHARP-3037: Correct types in ArgumentException message.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevbite authored and vincentkam committed Apr 8, 2020
1 parent 995a53f commit cddc136
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
31 changes: 13 additions & 18 deletions src/MongoDB.Driver/PipelineDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,34 +332,29 @@ public override RenderedPipelineDefinition<TOutput> Render(IBsonSerializer<TInpu

private static List<IPipelineStageDefinition> VerifyStages(List<IPipelineStageDefinition> stages)
{
var nextInputType = typeof(TInput);
var expectedInputType = typeof(TInput);
for (int i = 0; i < stages.Count; i++)
{
if (stages[i].InputType != nextInputType)
if (stages[i].InputType != expectedInputType)
{
var message = string.Format(
"The input type to stage[{0}] was expected to be {1}, but was {2}.",
i,
nextInputType,
stages[i].InputType);
throw new ArgumentException(message, "stages");
var message =
$"The input type to stage[{i}] was expected to be {expectedInputType}, but was {stages[i].InputType}.";
throw new ArgumentException(message, nameof(stages));
}

nextInputType = stages[i].OutputType;
expectedInputType = stages[i].OutputType;
}
var lastStageOutputType = expectedInputType;

if (nextInputType != typeof(TOutput))
if (lastStageOutputType != typeof(TOutput))
{
var message = string.Format(
"The output type to the last stage was expected to be {0}, but was {1}.",
nextInputType,
stages.Last().OutputType);
throw new ArgumentException(message, "stages");
var message =
$"The output type to the last stage was expected to be {typeof(TOutput)}, but was {lastStageOutputType}.";
throw new ArgumentException(message, nameof(stages));
}

return stages;
}
}

internal class OptimizingPipelineDefinition<TInput, TOutput> : PipelineDefinition<TInput, TOutput>
{
Expand All @@ -380,7 +375,7 @@ public override RenderedPipelineDefinition<TOutput> Render(IBsonSerializer<TInpu
{
var rendered = _wrapped.Render(inputSerializer, serializerRegistry);

// do some combining of $match documents if possible. This is optimized for the
// do some combining of $match documents if possible. This is optimized for the
// OfType case where we've added a discriminator as a match at the beginning of the pipeline.
if (rendered.Documents.Count > 1)
{
Expand All @@ -401,4 +396,4 @@ public override RenderedPipelineDefinition<TOutput> Render(IBsonSerializer<TInpu
return rendered;
}
}
}
}
33 changes: 26 additions & 7 deletions tests/MongoDB.Driver.Tests/PipelineDefinitionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,43 @@ namespace MongoDB.Driver.Tests
public class PipelineStagePipelineDefinitionTests
{
[Fact]
public void Constructor_should_verify_the_inputs_and_outputs_of_the_stages_and_throw_when_invalid()
public void Constructor_should_verify_the_inputs_and_outputs_of_the_stages_and_throw_when_intermediate_stage_is_invalid()
{
var stages = new IPipelineStageDefinition[]
var stages = new IPipelineStageDefinition[]
{
new BsonDocumentPipelineStageDefinition<Person, BsonDocument>(new BsonDocument()),
new BsonDocumentPipelineStageDefinition<BsonDocument, Pet>(new BsonDocument()),
new BsonDocumentPipelineStageDefinition<BsonDocument, Person>(new BsonDocument())
};

Action act = () => new PipelineStagePipelineDefinition<Person, Person>(stages);
var exception = Record.Exception(() => new PipelineStagePipelineDefinition<Person, Person>(stages));

act.ShouldThrow<ArgumentException>();
var e = exception.Should().BeOfType<ArgumentException>().Subject;
e.ParamName.Should().Be("stages");
e.Message.Should().Contain($"The input type to stage[2] was expected to be {typeof(Pet)}, but was {typeof(BsonDocument)}.");
}

[Fact]
public void Constructor_should_verify_the_inputs_and_outputs_of_the_stages_and_throw_when_final_stage_is_invalid()
{
var stages = new IPipelineStageDefinition[]
{
new BsonDocumentPipelineStageDefinition<Person, BsonDocument>(new BsonDocument()),
new BsonDocumentPipelineStageDefinition<BsonDocument, Pet>(new BsonDocument()),
new BsonDocumentPipelineStageDefinition<Pet, BsonDocument>(new BsonDocument())
};

var exception = Record.Exception(() => new PipelineStagePipelineDefinition<Person, Person>(stages));

var e = exception.Should().BeOfType<ArgumentException>().Subject;
e.ParamName.Should().Be("stages");
e.Message.Should().Contain($"The output type to the last stage was expected to be {typeof(Person)}, but was {typeof(BsonDocument)}.");
}

[Fact]
public void Constructor_should_verify_the_inputs_and_outputs_of_the_stages()
{
var stages = new IPipelineStageDefinition[]
var stages = new IPipelineStageDefinition[]
{
new BsonDocumentPipelineStageDefinition<Person, BsonDocument>(new BsonDocument()),
new BsonDocumentPipelineStageDefinition<BsonDocument, Pet>(new BsonDocument()),
Expand Down Expand Up @@ -71,7 +90,7 @@ private class Person
{
[BsonElement("fn")]
public string FirstName { get; set; }

[BsonElement("pets")]
public Pet[] Pets { get; set; }
}
Expand All @@ -80,6 +99,6 @@ private class Pet
{
[BsonElement("name")]
public string Name { get; set; }
}
}
}
}

0 comments on commit cddc136

Please sign in to comment.