Skip to content

Commit

Permalink
fix KeyNotFoundException in PersistenceFluentExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
joem-msft committed Apr 20, 2024
1 parent b06ff35 commit f51e91a
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/Persistence.Tests/Extensions/PersistenceFluentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ private static void CompareYamlTree(YamlNode actualNode, YamlNode expectedNode,

actualMapping.Tag.Should().Be(expectedMapping.Tag, BecauseFormat_PropertyName_NodePath_NodeStart, nameof(actualNode.Tag), nodePath, actualNode.Start);

// Report mising/extra keys first
var actualMappingKeys = actualMapping.Children.Keys.Select(k => k.ToString()).ToArray();
var expectedMappingKeys = expectedMapping.Children.Keys.Select(k => k.ToString()).ToArray();
actualMappingKeys.Should().BeEquivalentTo(expectedMappingKeys, BecauseFormat_PropertyName_NodePath_NodeStart, nameof(actualMapping.Children), nodePath, actualNode.Start);

// Then dig down into each key expected
foreach (var (expectedKey, expectedValue) in expectedMapping.Children)
{
// Note: technically, YAML mapping keys can be any yaml node type (e.g. scalar, sequence, mapping, etc.)
Expand All @@ -142,13 +148,12 @@ private static void CompareYamlTree(YamlNode actualNode, YamlNode expectedNode,
throw new NotSupportedException($"Non-scalar key found in expected mapping at path '{nodePath}'.");
}

var actualValue = actualMapping[expectedKey];
var valueNodePath = $"{nodePath}{((YamlScalarNode)expectedKey).ToBreadcrumbPathSegment()}";
CompareYamlTree(actualValue, expectedValue, valueNodePath);
if (actualMapping.Children.TryGetValue(expectedKey, out var actualValue))
{
CompareYamlTree(actualValue, expectedValue, valueNodePath);
}
}

// Zip will only validate items that are in each list
actualMapping.Children.Should().HaveSameCount(expectedMapping.Children, BecauseFormat_PropertyName_NodePath_NodeStart, nameof(actualMapping.Children), nodePath, actualNode.Start);
}
else if (actualNode.NodeType == YamlNodeType.Alias)
{
Expand Down

0 comments on commit f51e91a

Please sign in to comment.