101 lines
4.0 KiB
C#
101 lines
4.0 KiB
C#
using NTP.Application.Refinery.Variables;
|
|
|
|
namespace NTP.Tests.Refinery;
|
|
|
|
public class VariablesRulesTests
|
|
{
|
|
private static SaveVariablesRequest Request(string start, string end, decimal diesel = 0.615m, decimal wage = 16.855m) => new()
|
|
{
|
|
StartDate = DateOnly.Parse(start), EndDate = DateOnly.Parse(end), DieselLiterPrice = diesel, TonWage = wage, TonPrice = 700,
|
|
DriverTip = 20, PermitDispatch = 2.5m, WaybillFee = 0.75m, Commission = 0.45m, TolerancePercentage = 0.005m, FirstAdvancePayment = 100,
|
|
};
|
|
|
|
private static readonly VariablesPeriod[] Existing =
|
|
[
|
|
new(1, new DateTime(2023, 7, 1), new DateTime(2023, 7, 31)),
|
|
new(2, new DateTime(2023, 8, 1), new DateTime(2023, 8, 31)),
|
|
];
|
|
|
|
[Fact]
|
|
public void A_free_period_is_accepted() =>
|
|
Assert.Null(VariablesRules.FirstError(Existing, Request("2023-09-01", "2023-09-30")));
|
|
|
|
[Fact]
|
|
public void Start_inside_another_period_is_refused_first()
|
|
{
|
|
var e = VariablesRules.FirstError(Existing, Request("2023-07-15", "2023-09-30"));
|
|
Assert.Equal(VariablesErrors.StartDateInPreviousPeriod, e!.Value.Code);
|
|
}
|
|
|
|
[Fact]
|
|
public void End_inside_another_period_is_refused()
|
|
{
|
|
var e = VariablesRules.FirstError(Existing, Request("2023-06-01", "2023-07-01"));
|
|
Assert.Equal(VariablesErrors.EndDateInPreviousPeriod, e!.Value.Code);
|
|
}
|
|
|
|
[Fact]
|
|
public void Period_that_swallows_another_is_refused()
|
|
{
|
|
var e = VariablesRules.FirstError(Existing, Request("2023-06-15", "2023-09-15"));
|
|
// the end (09-15) is free and the start (06-15) is free, but both July and August lie inside
|
|
Assert.Equal(VariablesErrors.DateOverlap, e!.Value.Code);
|
|
}
|
|
|
|
[Fact]
|
|
public void Period_edges_are_inclusive()
|
|
{
|
|
Assert.Equal(VariablesErrors.StartDateInPreviousPeriod, VariablesRules.FirstError(Existing, Request("2023-08-31", "2023-09-30"))!.Value.Code);
|
|
Assert.Null(VariablesRules.FirstError(Existing, Request("2023-09-01", "2023-09-01")));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(0)]
|
|
[InlineData(-1)]
|
|
[InlineData(2)]
|
|
[InlineData(3.5)]
|
|
public void Diesel_price_must_be_between_0_and_2_exclusive(double price)
|
|
{
|
|
var e = VariablesRules.FirstError([], Request("2024-01-01", "2024-01-31", diesel: (decimal)price));
|
|
Assert.Equal(VariablesErrors.DieselPriceNotCorrect, e!.Value.Code);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(0)]
|
|
[InlineData(100)]
|
|
[InlineData(250)]
|
|
public void Ton_wage_must_be_between_0_and_100_exclusive(double wage)
|
|
{
|
|
var e = VariablesRules.FirstError([], Request("2024-01-01", "2024-01-31", wage: (decimal)wage));
|
|
Assert.Equal(VariablesErrors.TonWageNotCorrect, e!.Value.Code);
|
|
}
|
|
|
|
[Fact]
|
|
public void Start_after_end_is_refused_last()
|
|
{
|
|
var e = VariablesRules.FirstError([], Request("2024-02-01", "2024-01-31"));
|
|
Assert.Equal(VariablesErrors.StartAfterEnd, e!.Value.Code);
|
|
// a bad price is reported before the date order
|
|
Assert.Equal(VariablesErrors.DieselPriceNotCorrect, VariablesRules.FirstError([], Request("2024-02-01", "2024-01-31", diesel: 5))!.Value.Code);
|
|
}
|
|
|
|
[Fact]
|
|
public void Values_are_stored_as_plain_invariant_text_for_the_nine_keys()
|
|
{
|
|
var v = VariablesRules.ToValues(Request("2024-01-01", "2024-01-31"));
|
|
Assert.Equal(VariablesRules.SetKeys.OrderBy(k => k), v.Keys.OrderBy(k => k));
|
|
Assert.Equal("0.615", v["DIESEL_LITER_PRICE"]);
|
|
Assert.Equal("700", v["TON_PRICE"]);
|
|
Assert.Equal("0.005", v["TOLERANCE_PERCENTAGE"]);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(700, 700, false)]
|
|
[InlineData(700, 750, true)]
|
|
public void Balances_are_recalculated_only_when_the_correction_differs_from_the_ton_price(double price, double correction, bool changes) =>
|
|
Assert.Equal(changes, VariablesRules.TonPriceChanges((decimal)price, (decimal)correction));
|
|
|
|
[Fact]
|
|
public void Without_a_ton_price_row_nothing_is_recalculated() => Assert.False(VariablesRules.TonPriceChanges(null, 750));
|
|
}
|