Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix KeyNotFoundException in PersistenceFluentExtensions #646

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading