Skip to content

Commit

Permalink
Fix error (#11)
Browse files Browse the repository at this point in the history
* Update ScheduleItemValidator.cs

* Update ScheduleItemValidator.cs

* Update ScheduleItemValidator.cs

* add test

* fix code

* add test for min amunt

* add min ratio

* fix this

* format

* fix

* .NotNull()

* - improve exist and new tests

* - update `TokenSchedule.Tests`

* - typo

---------

Co-authored-by: Stanislav Vysotskyi <[email protected]>
  • Loading branch information
Lomet and ArdenHide authored Jan 2, 2025
1 parent 80de9fd commit e102259
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/TokenSchedule.FluentValidation/ScheduleItemValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ namespace TokenSchedule.FluentValidation
{
public class ScheduleItemValidator : AbstractValidator<IValidatedScheduleItem>
{
public const decimal MinRatio = 0.000000000000000001m;
public ScheduleItemValidator()
{
RuleFor(item => item)
.Cascade(CascadeMode.Stop)
.NotNull()
.Must(item => item.Ratio > 0)
.WithMessage("Ratio must be positive.")
.Must(item => item.Ratio >= MinRatio)
.WithMessage("Ratio must be greater than or equal to 1e-18.")
.Must(item => item.StartDate < item.FinishDate!.Value)
.When(item => item.FinishDate.HasValue)
.When(item => item.FinishDate.HasValue, ApplyConditionTo.CurrentValidator)
.WithMessage("End time must be greater than start time.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,14 @@ internal void NullItem_ShouldThrowValidationException()
}

[Theory]
[InlineData(0)]
[InlineData(-1)]
[InlineData(-100)]
internal void NonPositiveRatio_ShouldThrowValidationException(decimal ratio)
[MemberData(nameof(InvalidRatios))]
internal void InvalidRatio_ShouldThrowValidationException(TestScheduleItem item)
{
var item = new TestScheduleItem(ratio, DateTime.UtcNow, DateTime.UtcNow.AddHours(1));

var testCode = () => _validator.ValidateAndThrow(item);

testCode.Should().Throw<ValidationException>()
.WithMessage("*Ratio must be positive.*");
.Which.Errors.Should().ContainSingle()
.Which.ErrorMessage.Should().Be("Ratio must be greater than or equal to 1e-18.");
}

[Fact]
Expand All @@ -54,7 +51,8 @@ internal void StartDateBiggerThanFinishDate_ShouldThrowValidationException()
var testCode = () => _validator.ValidateAndThrow(item);

testCode.Should().Throw<ValidationException>()
.WithMessage("*End time must be greater than start time.*");
.Which.Errors.Should().ContainSingle()
.Which.ErrorMessage.Should().Be("End time must be greater than start time.");
}

[Fact]
Expand All @@ -66,8 +64,21 @@ internal void StartDateEqualToFinishDate_ShouldThrowValidationException()
var testCode = () => _validator.ValidateAndThrow(item);

testCode.Should().Throw<ValidationException>()
.WithMessage("*End time must be greater than start time.*");
.Which.Errors.Should().ContainSingle()
.Which.ErrorMessage.Should().Be("End time must be greater than start time.");
}

public static List<object[]> InvalidRatios => new()
{
new object[] { new TestScheduleItem(0m, DateTime.UtcNow) },
new object[] { new TestScheduleItem(-1m, DateTime.UtcNow) },
new object[] { new TestScheduleItem(-100m, DateTime.UtcNow) },
new object[] { new TestScheduleItem(0.0000000000000000001m, DateTime.UtcNow) },
new object[] { new TestScheduleItem(0m, DateTime.UtcNow, DateTime.UtcNow.AddHours(1)) },
new object[] { new TestScheduleItem(-1m, DateTime.UtcNow, DateTime.UtcNow.AddHours(1)) },
new object[] { new TestScheduleItem(-100m, DateTime.UtcNow, DateTime.UtcNow.AddHours(1)) },
new object[] { new TestScheduleItem(0.0000000000000000001m, DateTime.UtcNow, DateTime.UtcNow.AddHours(1)) }
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public void EmptySchedule_ShouldThrowValidationException()
var testCode = () => _validator.ValidateAndThrow(schedule);

testCode.Should().Throw<ValidationException>()
.WithMessage("*Schedule must contain 1 or more elements.*");
.Which.Errors.Should().ContainSingle()
.Which.ErrorMessage.Should().Be("Schedule must contain 1 or more elements.");
}

[Fact]
Expand All @@ -45,7 +46,8 @@ public void SumOfRatiosNotEqualToOne_ShouldThrowValidationException()
var testCode = () => _validator.ValidateAndThrow(schedule);

testCode.Should().Throw<ValidationException>()
.WithMessage("*The sum of the ratios must be 1.*");
.Which.Errors.Should().ContainSingle()
.Which.ErrorMessage.Should().Be("The sum of the ratios must be 1.");
}

[Fact]
Expand All @@ -61,7 +63,8 @@ public void FirstItemNotEarliest_ShouldThrowValidationException()
var testCode = () => _validator.ValidateAndThrow(schedule);

testCode.Should().Throw<ValidationException>()
.WithMessage("*The first element must be the TGE (Token Generation Event).*");
.Which.Errors.Should().ContainSingle()
.Which.ErrorMessage.Should().Be("The first element must be the TGE (Token Generation Event).");
}

[Fact]
Expand All @@ -76,7 +79,8 @@ public void InvalidItemInSchedule_ShouldThrowValidationException()
var testCode = () => _validator.ValidateAndThrow(schedule);

testCode.Should().Throw<ValidationException>()
.WithMessage("*Ratio must be positive.*");
.Which.Errors.Should().ContainSingle()
.Which.ErrorMessage.Should().Be("Ratio must be greater than or equal to 1e-18.");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace TokenSchedule.FluentValidation.Tests;

public class TestScheduleItem : IValidatedScheduleItem
{
public TestScheduleItem(decimal ratio, DateTime startDate, DateTime? finishDate)
public TestScheduleItem(decimal ratio, DateTime startDate, DateTime? finishDate = null)
{
Ratio = ratio;
StartDate = startDate;
Expand Down
6 changes: 4 additions & 2 deletions tests/TokenSchedule.Tests/ScheduleItemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ internal void WhenFinishTimeLowerThenStartTime_ThrowException()
var testCode = () => _ = new ScheduleItem(0.1m, DateTime.Now, DateTime.Now.AddDays(-1));

testCode.Should().Throw<ValidationException>()
.WithMessage("*End time must be greater than start time.*");
.Which.Errors.Should().ContainSingle()
.Which.ErrorMessage.Should().Be("End time must be greater than start time.");
}

[Fact]
Expand All @@ -23,7 +24,8 @@ internal void WhenRatioIsNegative_ThrowException()
var testCode = () => _ = new ScheduleItem(-0.1m, DateTime.Now, DateTime.Now.AddDays(1));

testCode.Should().Throw<ValidationException>()
.WithMessage("*Ratio must be positive.*");
.Which.Errors.Should().ContainSingle()
.Which.ErrorMessage.Should().Be("Ratio must be greater than or equal to 1e-18.");
}
}
}
9 changes: 6 additions & 3 deletions tests/TokenSchedule.Tests/ScheduleManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public void WhenScheduleContainsZeroElements_ThrowException()
);

testCode.Should().Throw<ValidationException>()
.WithMessage("*Schedule must contain 1 or more elements.*");
.Which.Errors.Should().ContainSingle()
.Which.ErrorMessage.Should().Be("Schedule must contain 1 or more elements.");
}

[Fact]
Expand All @@ -95,7 +96,8 @@ public void WhenSumOfRatiosNotEqualOne_ThrowException()
);

testCode.Should().Throw<ValidationException>()
.WithMessage("*The sum of the ratios must be 1.*");
.Which.Errors.Should().ContainSingle()
.Which.ErrorMessage.Should().Be("The sum of the ratios must be 1.");
}

[Fact]
Expand All @@ -107,7 +109,8 @@ public void WhenFirstElementIsNotTGE_ThrowException()
);

testCode.Should().Throw<ValidationException>()
.WithMessage("*The first element must be the TGE (Token Generation Event).*");
.Which.Errors.Should().ContainSingle()
.Which.ErrorMessage.Should().Be("The first element must be the TGE (Token Generation Event).");
}
}
}

0 comments on commit e102259

Please sign in to comment.