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

Commit fc33b0c

Browse files
committed
regex validator tests
1 parent 72c4ad0 commit fc33b0c

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)