Skip to content

Commit

Permalink
Stores validation feedbacks in a dictionary to group duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
sakari-malkki committed Sep 11, 2024
1 parent dc3f1ba commit de923cb
Show file tree
Hide file tree
Showing 33 changed files with 1,009 additions and 1,039 deletions.
6 changes: 3 additions & 3 deletions Px.Utils.TestingApp/Commands/DatabaseValidationBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Px.Utils.TestingApp.Commands
{
internal class DatabaseValidationBenchmark : Benchmark
internal sealed class DatabaseValidationBenchmark : Benchmark
{
internal override string Help => "Validates a px path database.";

Expand Down Expand Up @@ -56,12 +56,12 @@ protected override void OneTimeBenchmarkSetup()
validator = new(TestFilePath);
}

protected void ValidationBenchmark()
private void ValidationBenchmark()
{
validator.Validate();
}

protected async Task ValidationBenchmarkAsync()
private async Task ValidationBenchmarkAsync()
{
await validator.ValidateAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ internal sealed class MetadataSyntaxValidationBenchmark : FileBenchmark

internal override string Description => "Benchmarks the metadata syntax validation of Px.Utils/Validation/SyntaxValidator.";

private Encoding encoding;
private SyntaxValidator validator;
private Stream stream;

internal MetadataSyntaxValidationBenchmark()
{
Expand All @@ -29,7 +27,7 @@ protected override void OneTimeBenchmarkSetup()

Stream stream = new FileStream(TestFilePath, FileMode.Open, FileAccess.Read);
PxFileMetadataReader reader = new();
encoding = reader.GetEncoding(stream);
Encoding encoding = reader.GetEncoding(stream);
validator = new(stream, encoding, TestFilePath, leaveStreamOpen: true);
}

Expand Down
2 changes: 1 addition & 1 deletion Px.Utils.TestingApp/Commands/PxFileValidationBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Px.Utils.TestingApp.Commands
{
internal class PxFileValidationBenchmark : FileBenchmark
internal sealed class PxFileValidationBenchmark : FileBenchmark
{
internal override string Help =>
"Runs through the whole px file validation process (metadata syntax- and contents-, data-) for the given file.";
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ namespace Px.Utils.UnitTests.Validation.ContentValidationTests
{
internal sealed class MockCustomContentValidationFunctions : CustomContentValidationFunctions
{
internal static ValidationFeedbackItem[]? MockFindKeywordFunction(ValidationStructuredEntry[] entries, ContentValidator validator)
internal static ValidationFeedback? MockFindKeywordFunction(ValidationStructuredEntry[] entries, ContentValidator validator)
{
return null;
}

internal static ValidationFeedbackItem[]? MockEntryFunction(ValidationStructuredEntry entry, ContentValidator validator)
internal static ValidationFeedback? MockEntryFunction(ValidationStructuredEntry entry, ContentValidator validator)
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void AllowedNumberValues(string allowedValue)
DataNumberValidator validator = new();
Encoding encoding = Encoding.UTF8;
List<byte> value = [.. encoding.GetBytes(allowedValue)];
ValidationFeedback? nullableFeedback = validator.Validate(value, EntryType.DataItem, encoding, 0, 0);
KeyValuePair<ValidationFeedbackKey, ValidationFeedbackValue>? nullableFeedback = validator.Validate(value, EntryType.DataItem, encoding, 0, 0, "foo");
Assert.IsNull(nullableFeedback);
}

Expand All @@ -48,13 +48,13 @@ public void NotAllowedNumberValue(string notAllowedValue)

Encoding encoding = Encoding.UTF8;
List<byte> value = [.. encoding.GetBytes(notAllowedValue)];
ValidationFeedback? nullableFeedback = validator.Validate(value, EntryType.DataItem, encoding, 0, 0);
KeyValuePair<ValidationFeedbackKey, ValidationFeedbackValue>? nullableFeedback = validator.Validate(value, EntryType.DataItem, encoding, 0, 0, "foo");

Assert.IsNotNull(nullableFeedback);
ValidationFeedback feedback = (ValidationFeedback)nullableFeedback;
Assert.AreEqual(ValidationFeedbackRule.DataValidationFeedbackInvalidNumber, feedback.Rule);
Assert.AreEqual(notAllowedValue, feedback.AdditionalInfo);
Assert.AreEqual(ValidationFeedbackLevel.Error, feedback.Level);
KeyValuePair<ValidationFeedbackKey, ValidationFeedbackValue> feedback = (KeyValuePair<ValidationFeedbackKey, ValidationFeedbackValue>)nullableFeedback;
Assert.AreEqual(ValidationFeedbackRule.DataValidationFeedbackInvalidNumber, feedback.Key.Rule);
Assert.AreEqual(notAllowedValue, feedback.Value.AdditionalInfo);
Assert.AreEqual(ValidationFeedbackLevel.Error, feedback.Key.Level);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void FirstSeparatorIsUsedAsReference()
{
DataSeparatorValidator validator = new();
List<byte> separator = [.. Encoding.UTF8.GetBytes(" ")];
ValidationFeedback? nullableFeedback = validator.Validate(separator, EntryType.DataItemSeparator, Encoding.UTF8, 1, 1);
KeyValuePair<ValidationFeedbackKey, ValidationFeedbackValue>? nullableFeedback = validator.Validate(separator, EntryType.DataItemSeparator, Encoding.UTF8, 1, 1, "foo");

Assert.IsNull(nullableFeedback);
}
Expand All @@ -22,14 +22,14 @@ public void InconsistentSeparator()
{
DataSeparatorValidator validator = new();
List<byte> separator = [.. Encoding.UTF8.GetBytes(" ")];
validator.Validate(separator, EntryType.DataItemSeparator, Encoding.UTF8, 1, 1);
validator.Validate(separator, EntryType.DataItemSeparator, Encoding.UTF8, 1, 1, "foo");
List<byte> otherSeparator = [.. Encoding.UTF8.GetBytes("\t")];
ValidationFeedback? nullableFeedback = validator.Validate(otherSeparator, EntryType.DataItemSeparator, Encoding.UTF8, 1, 1);
KeyValuePair<ValidationFeedbackKey, ValidationFeedbackValue>? nullableFeedback = validator.Validate(otherSeparator, EntryType.DataItemSeparator, Encoding.UTF8, 1, 1, "foo");

Assert.IsNotNull(nullableFeedback);
ValidationFeedback feedback = (ValidationFeedback)nullableFeedback;
Assert.AreEqual(ValidationFeedbackRule.DataValidationFeedbackInconsistentSeparator, feedback.Rule);
Assert.AreEqual(ValidationFeedbackLevel.Warning, feedback.Level);
KeyValuePair<ValidationFeedbackKey, ValidationFeedbackValue> feedback = (KeyValuePair<ValidationFeedbackKey, ValidationFeedbackValue>)nullableFeedback;
Assert.AreEqual(ValidationFeedbackRule.DataValidationFeedbackInconsistentSeparator, feedback.Key.Rule);
Assert.AreEqual(ValidationFeedbackLevel.Warning, feedback.Key.Level);


}
Expand All @@ -39,8 +39,8 @@ public void ConsistentSeparator()
{
DataSeparatorValidator validator = new();
List<byte> separator = [.. Encoding.UTF8.GetBytes(" ")];
validator.Validate(separator, EntryType.DataItemSeparator, Encoding.UTF8, 1, 1);
ValidationFeedback? nullableFeedback = validator.Validate(separator, EntryType.DataItemSeparator, Encoding.UTF8, 1, 1);
validator.Validate(separator, EntryType.DataItemSeparator, Encoding.UTF8, 1, 1, "foo");
KeyValuePair<ValidationFeedbackKey, ValidationFeedbackValue>? nullableFeedback = validator.Validate(separator, EntryType.DataItemSeparator, Encoding.UTF8, 1, 1, "foo");
Assert.IsNull(nullableFeedback);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void AllowedStrings(string allowedValue)
DataStringValidator validator = new();
Encoding encoding = Encoding.UTF8;
List<byte> value = [.. encoding.GetBytes(allowedValue)];
ValidationFeedback? nullableFeedback = validator.Validate(value, EntryType.DataItem, encoding, 0, 0);
KeyValuePair<ValidationFeedbackKey, ValidationFeedbackValue>? nullableFeedback = validator.Validate(value, EntryType.DataItem, encoding, 0, 0, "foo");

Assert.IsNull(nullableFeedback);
}
Expand All @@ -43,13 +43,13 @@ public void NotAllowedStringValue(string notAllowedValue)
Encoding encoding = Encoding.UTF8;
List<byte> value = [.. encoding.GetBytes(notAllowedValue)];

ValidationFeedback? nullableFeedback = validator.Validate(value, EntryType.DataItem, encoding, 0, 0);
KeyValuePair<ValidationFeedbackKey, ValidationFeedbackValue>? nullableFeedback = validator.Validate(value, EntryType.DataItem, encoding, 0, 0, "foo");

Assert.IsNotNull(nullableFeedback);
ValidationFeedback feedback = (ValidationFeedback)nullableFeedback;
Assert.AreEqual(ValidationFeedbackRule.DataValidationFeedbackInvalidString, feedback.Rule);
Assert.AreEqual(notAllowedValue, feedback.AdditionalInfo);
Assert.AreEqual(ValidationFeedbackLevel.Error, feedback.Level);
KeyValuePair<ValidationFeedbackKey, ValidationFeedbackValue> feedback = (KeyValuePair<ValidationFeedbackKey, ValidationFeedbackValue>)nullableFeedback;
Assert.AreEqual(ValidationFeedbackRule.DataValidationFeedbackInvalidString, feedback.Key.Rule);
Assert.AreEqual(notAllowedValue, feedback.Value.AdditionalInfo);
Assert.AreEqual(ValidationFeedbackLevel.Error, feedback.Key.Level);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public class DataStructureValidatorTest

public void AllowedTokenSequences(params EntryType[] tokenSequence)
{
List<ValidationFeedback> feedbacks = [];
ValidationFeedback feedbacks = [];
DataStructureValidator validator = new();
foreach (EntryType tokenType in tokenSequence)
{
ValidationFeedback? feedback = validator.Validate([], tokenType, Encoding.UTF8, 1, 1);
KeyValuePair<ValidationFeedbackKey, ValidationFeedbackValue>? feedback = validator.Validate([], tokenType, Encoding.UTF8, 1, 1, "foo");
if (feedback is not null)
{
feedbacks.Add((ValidationFeedback)feedback);
feedbacks.Add((KeyValuePair<ValidationFeedbackKey, ValidationFeedbackValue>)feedback);
}
}

Expand All @@ -44,23 +44,23 @@ public void AllowedTokenSequences(params EntryType[] tokenSequence)
[DataRow([EntryType.DataItem, EntryType.DataItemSeparator, EntryType.EndOfData])]
public void NotAllowedTokenSequences(params EntryType[] tokenSequence)
{
List<ValidationFeedback> feedbacks = [];
ValidationFeedback feedbacks = [];
DataStructureValidator validator = new();
foreach (EntryType tokenType in tokenSequence)
{
ValidationFeedback? feedback = validator.Validate([], tokenType, Encoding.UTF8, 1, 1);
KeyValuePair<ValidationFeedbackKey, ValidationFeedbackValue>? feedback = validator.Validate([], tokenType, Encoding.UTF8, 1, 1, "foo");
if (feedback is not null)
{
feedbacks.Add((ValidationFeedback)feedback);
feedbacks.Add((KeyValuePair<ValidationFeedbackKey, ValidationFeedbackValue>)feedback);
}
}

Assert.AreEqual(1, feedbacks.Count);
Assert.AreEqual(ValidationFeedbackRule.DataValidationFeedbackInvalidStructure, feedbacks[0].Rule);
Assert.AreEqual(ValidationFeedbackLevel.Error, feedbacks[0].Level);
Assert.AreEqual(ValidationFeedbackRule.DataValidationFeedbackInvalidStructure, feedbacks.First().Key.Rule);
Assert.AreEqual(ValidationFeedbackLevel.Error, feedbacks.First().Key.Level);
List<EntryType> expectedTokens = [ tokenSequence.Length > 1 ? tokenSequence[^2] : EntryType.Unknown, tokenSequence[^1] ];

Assert.AreEqual(string.Join(",", expectedTokens), feedbacks[0].AdditionalInfo);
Assert.AreEqual(string.Join(",", expectedTokens), feedbacks.First().Value[0].AdditionalInfo);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ public void TestValidateWithoutErrors()
stream.Seek(6, 0);
DataValidator validator = new(stream, 5, 4, "foo", 1, Encoding.Default);

ValidationFeedbackItem[] validationFeedbacks = validator.Validate().FeedbackItems;
ValidationFeedback validationFeedbacks = validator.Validate().FeedbackItems;

foreach (ValidationFeedbackItem validationFeedback in validationFeedbacks)
foreach (KeyValuePair<ValidationFeedbackKey, List<ValidationFeedbackValue>> validationFeedback in validationFeedbacks)
{
Logger.LogMessage($"Line {validationFeedback.Feedback.Line}, Char {validationFeedback.Feedback.Character}: "
+ $"{validationFeedback.Feedback.Rule} {validationFeedback.Feedback.AdditionalInfo}");
foreach (ValidationFeedbackValue instance in validationFeedback.Value)
{
Logger.LogMessage($"Line {instance.Line}, Char {instance.Character}: "
+ $"{validationFeedback.Key.Rule} {instance.AdditionalInfo}");
}
}
Assert.AreEqual(0, validationFeedbacks.Length);
Assert.AreEqual(0, validationFeedbacks.Count);
}

[TestMethod]
Expand All @@ -35,14 +38,17 @@ public async Task TestValidateAsyncWithoutErrors()
DataValidator validator = new(stream, 5, 4, "foo", 1, Encoding.Default);

ValidationResult result = await validator.ValidateAsync();
ValidationFeedbackItem[] validationFeedbacks = result.FeedbackItems;
ValidationFeedback validationFeedbacks = result.FeedbackItems;

foreach (ValidationFeedbackItem validationFeedback in validationFeedbacks)
foreach (KeyValuePair<ValidationFeedbackKey, List<ValidationFeedbackValue>> validationFeedback in validationFeedbacks)
{
Logger.LogMessage($"Line {validationFeedback.Feedback.Line}, Char {validationFeedback.Feedback.Character}: "
+ $"{validationFeedback.Feedback.Rule} {validationFeedback.Feedback.AdditionalInfo}");
foreach (ValidationFeedbackValue instance in validationFeedback.Value)
{
Logger.LogMessage($"Line {instance.Line}, Char {instance.Character}: "
+ $"{validationFeedback.Key.Rule} {instance.AdditionalInfo}");
}
}
Assert.AreEqual(0, validationFeedbacks.Length);
Assert.AreEqual(0, validationFeedbacks.Count);

}

Expand All @@ -53,14 +59,19 @@ public void TestValidateWithErrors()
stream.Seek(6, 0);
DataValidator validator = new(stream, 5, 4, "foo", 1, Encoding.Default);

ValidationFeedbackItem[] validationFeedbacks = validator.Validate().FeedbackItems;
ValidationFeedback validationFeedbacks = validator.Validate().FeedbackItems;

foreach (ValidationFeedbackItem validationFeedback in validationFeedbacks)
foreach (KeyValuePair<ValidationFeedbackKey, List<ValidationFeedbackValue>> validationFeedback in validationFeedbacks)
{
Logger.LogMessage($"Line {validationFeedback.Feedback.Line}, Char {validationFeedback.Feedback.Character}: "
+ $"{validationFeedback.Feedback.Rule} {validationFeedback.Feedback.AdditionalInfo}");
foreach (ValidationFeedbackValue instance in validationFeedback.Value)
{
Logger.LogMessage($"Line {instance.Line}, Char {instance.Character}: "
+ $"{validationFeedback.Key.Rule} {instance.AdditionalInfo}");
}
}
Assert.AreEqual(13, validationFeedbacks.Length);

Assert.AreEqual(7, validationFeedbacks.Count); // Unique feedbacks
Assert.AreEqual(13, validationFeedbacks.Values.SelectMany(f => f).Count()); // Total feedbacks including duplicates
}

[TestMethod]
Expand All @@ -71,14 +82,19 @@ public async Task TestValidateAsyncWithErrors()
DataValidator validator = new(stream, 5, 4, "foo", 1, Encoding.Default);

ValidationResult result = await validator.ValidateAsync();
ValidationFeedbackItem[] validationFeedbacks = result.FeedbackItems;
ValidationFeedback validationFeedbacks = result.FeedbackItems;

foreach (ValidationFeedbackItem validationFeedback in validationFeedbacks)
foreach (KeyValuePair<ValidationFeedbackKey, List<ValidationFeedbackValue>> validationFeedback in validationFeedbacks)
{
Logger.LogMessage($"Line {validationFeedback.Feedback.Line}, Char {validationFeedback.Feedback.Character}: "
+ $"{validationFeedback.Feedback.Rule} {validationFeedback.Feedback.AdditionalInfo}");
foreach (ValidationFeedbackValue instance in validationFeedback.Value)
{
Logger.LogMessage($"Line {instance.Line}, Char {instance.Character}: "
+ $"{validationFeedback.Key.Rule} {instance.AdditionalInfo}");
}
}
Assert.AreEqual(13, validationFeedbacks.Length);

Assert.AreEqual(7, validationFeedbacks.Count); // Unique feedbacks
Assert.AreEqual(13, validationFeedbacks.Values.SelectMany(f => f).Count()); // Total feedbacks including duplicates
}
}
}
Loading

0 comments on commit de923cb

Please sign in to comment.