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

Commit 3c70870

Browse files
committed
max length validator tests
1 parent fc33b0c commit 3c70870

2 files changed

Lines changed: 116 additions & 3 deletions

File tree

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using ServiceLayer.Mesh.FileTypes.NbssAppointmentEvents.Models;
2+
using ServiceLayer.Mesh.FileTypes.NbssAppointmentEvents.Validation;
3+
4+
namespace ServiceLayer.Mesh.Tests.FileTypes.NbssAppointmentEvents.Validation;
5+
6+
public class MaxLengthValidatorTests
7+
{
8+
private const string FieldName = "TestField";
9+
private const string MissingCode = "ERR100";
10+
private const string TooLongCode = "ERR101";
11+
12+
[Theory]
13+
[InlineData(false, "TestField is missing or empty")]
14+
[InlineData(true, "TestField is missing")]
15+
public void Validate_NullValue_ShouldReturnMissingError(bool allowEmpty, string expectedError)
16+
{
17+
// Arrange
18+
var record = new FileDataRecord
19+
{
20+
RowNumber = 1
21+
};
22+
record.Fields.Clear();
23+
24+
var validator = new MaxLengthValidator(FieldName, 5, MissingCode, TooLongCode, allowEmpty);
25+
26+
// Act
27+
var errors = validator.Validate(record).ToList();
28+
29+
// Assert
30+
errors.ShouldContainValidationError(FieldName, expectedError, MissingCode, 1);
31+
}
32+
33+
[Fact]
34+
public void Validate_EmptyValueDisallowed_ShouldReturnMissingError()
35+
{
36+
// Arrange
37+
var record = new FileDataRecord
38+
{
39+
RowNumber = 2
40+
};
41+
record.Fields.Add(FieldName, "");
42+
43+
var validator = new MaxLengthValidator(FieldName, 5, MissingCode, TooLongCode);
44+
45+
// Act
46+
var errors = validator.Validate(record).ToList();
47+
48+
// Assert
49+
errors.ShouldContainValidationError(FieldName, "TestField is missing or empty", MissingCode, 2);
50+
}
51+
52+
[Fact]
53+
public void Validate_EmptyValueAllowed_ShouldReturnNoErrors()
54+
{
55+
// Arrange
56+
var record = new FileDataRecord
57+
{
58+
RowNumber = 3
59+
};
60+
record.Fields.Add(FieldName, "");
61+
62+
var validator = new MaxLengthValidator(FieldName, 5, MissingCode, TooLongCode, true);
63+
64+
// Act
65+
var errors = validator.Validate(record).ToList();
66+
67+
// Assert
68+
Assert.Empty(errors);
69+
}
70+
71+
[Theory]
72+
[InlineData(5, "123456")]
73+
[InlineData(7, "12345678")]
74+
public void Validate_ValueExceedingMaxLength_ShouldReturnTooLongError(int maxLength, string tooLongValue)
75+
{
76+
// Arrange
77+
var record = new FileDataRecord
78+
{
79+
RowNumber = 4
80+
};
81+
record.Fields.Add(FieldName, tooLongValue);
82+
83+
var validator = new MaxLengthValidator(FieldName, maxLength, MissingCode, TooLongCode);
84+
85+
// Act
86+
var errors = validator.Validate(record).ToList();
87+
88+
// Assert
89+
errors.ShouldContainValidationError(FieldName, $"TestField exceeds maximum length of {maxLength}", TooLongCode, 4);
90+
}
91+
92+
[Theory]
93+
[InlineData(5, "123")]
94+
[InlineData(6, "123456")]
95+
[InlineData(7, "123456")]
96+
public void Validate_ValueWithinMaxLength_ShouldReturnNoErrors(int maxLength, string validValue)
97+
{
98+
// Arrange
99+
var record = new FileDataRecord
100+
{
101+
RowNumber = 5
102+
};
103+
record.Fields.Add(FieldName, validValue);
104+
105+
var validator = new MaxLengthValidator(FieldName, maxLength, MissingCode, TooLongCode);
106+
107+
// Act
108+
var errors = validator.Validate(record).ToList();
109+
110+
// Assert
111+
Assert.Empty(errors);
112+
}
113+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class RegexValidatorTests
1212
private readonly Regex _pattern = new(@"^[A-Z]{2}\d{2}$", RegexOptions.Compiled);
1313

1414
[Fact]
15-
public void NullValue_ShouldReturnMissingError()
15+
public void Validate_NullValue_ShouldReturnMissingError()
1616
{
1717
// Arrange
1818
var record = new FileDataRecord
@@ -33,7 +33,7 @@ public void NullValue_ShouldReturnMissingError()
3333
[Theory]
3434
[InlineData("")]
3535
[InlineData("invalid")]
36-
public void ValueNotMatchingPattern_ShouldReturnInvalidFormatError(string invalidValue)
36+
public void Validate_ValueNotMatchingPattern_ShouldReturnInvalidFormatError(string invalidValue)
3737
{
3838
// Arrange
3939
var record = new FileDataRecord
@@ -54,7 +54,7 @@ public void ValueNotMatchingPattern_ShouldReturnInvalidFormatError(string invali
5454
[Theory]
5555
[InlineData("AB12")]
5656
[InlineData("CD34")]
57-
public void ValueMatchingPattern_ShouldReturnNoErrors(string validValue)
57+
public void Validate_ValueMatchingPattern_ShouldReturnNoErrors(string validValue)
5858
{
5959
var record = new FileDataRecord
6060
{

0 commit comments

Comments
 (0)