Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit 38444b5

Browse files
committed
Added unit tests for HeaderFieldRegexValidator
1 parent 356c94e commit 38444b5

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System.Text.RegularExpressions;
2+
using ServiceLayer.Mesh.FileTypes.NbssAppointmentEvents.Validation;
3+
4+
namespace ServiceLayer.Mesh.Tests.FileTypes.NbssAppointmentEvents.Validation;
5+
6+
public partial class HeaderFieldRegexValidatorTests
7+
{
8+
private const string FieldName = "TestField";
9+
private const string MissingCode = "ERR001";
10+
private const string InvalidFormatCode = "ERR002";
11+
private readonly Regex _pattern = TestRegex();
12+
13+
[Fact]
14+
public void Validate_NullValue_ShouldReturnMissingError()
15+
{
16+
// Arrange
17+
var file = TestDataBuilder.BuildValidParsedFile();
18+
file.FileHeader!.ExtractId = null;
19+
20+
var validator = new HeaderFieldRegexValidator(x => x.ExtractId, FieldName, _pattern, MissingCode, InvalidFormatCode);
21+
22+
// Act
23+
var errors = validator.Validate(file).ToList();
24+
25+
// Assert
26+
errors.ShouldContainValidationError(FieldName, $"{FieldName} is missing", MissingCode, ValidationErrorScope.Header);
27+
}
28+
29+
[Theory]
30+
[InlineData("")]
31+
[InlineData("invalid")]
32+
public void Validate_ValueNotMatchingPattern_ShouldReturnInvalidFormatError(string invalidValue)
33+
{
34+
// Arrange
35+
var file = TestDataBuilder.BuildValidParsedFile();
36+
file.FileHeader!.ExtractId = invalidValue;
37+
38+
var validator = new HeaderFieldRegexValidator(x => x.ExtractId, FieldName, _pattern, MissingCode, InvalidFormatCode);
39+
40+
// Act
41+
var errors = validator.Validate(file).ToList();
42+
43+
// Assert
44+
errors.ShouldContainValidationError(FieldName, $"{FieldName} is in an invalid format", InvalidFormatCode,ValidationErrorScope.Header);
45+
}
46+
47+
[Theory]
48+
[InlineData("AB12")]
49+
[InlineData("CD34")]
50+
public void Validate_ValueMatchingPattern_ShouldReturnNoErrors(string validValue)
51+
{
52+
// Arrange
53+
var file = TestDataBuilder.BuildValidParsedFile();
54+
file.FileHeader!.ExtractId = validValue;
55+
56+
var validator = new HeaderFieldRegexValidator(x => x.ExtractId, FieldName, _pattern, MissingCode, InvalidFormatCode);
57+
58+
// Act
59+
var errors = validator.Validate(file).ToList();
60+
61+
// Assert
62+
Assert.Empty(errors);
63+
}
64+
65+
[GeneratedRegex(@"^[A-Z]{2}\d{2}$", RegexOptions.Compiled)]
66+
private static partial Regex TestRegex();
67+
}

tests/ServiceLayer.Mesh.Tests/FileTypes/NbssAppointmentEvents/Validation/RegexValidatorTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public void Validate_ValueNotMatchingPattern_ShouldReturnInvalidFormatError(stri
5656
[InlineData("CD34")]
5757
public void Validate_ValueMatchingPattern_ShouldReturnNoErrors(string validValue)
5858
{
59+
// Arrange
5960
var record = new FileDataRecord
6061
{
6162
RowNumber = 3
@@ -64,8 +65,10 @@ public void Validate_ValueMatchingPattern_ShouldReturnNoErrors(string validValue
6465

6566
var validator = new RegexValidator(FieldName, _pattern, MissingCode, InvalidFormatCode);
6667

68+
// Act
6769
var errors = validator.Validate(record).ToList();
6870

71+
// Assert
6972
Assert.Empty(errors);
7073
}
7174

0 commit comments

Comments
 (0)