240 lines
9.0 KiB
C#
240 lines
9.0 KiB
C#
using System.IO.Compression;
|
|
using System.Text;
|
|
using NTP.Application.Grains.Orders;
|
|
using NTP.Domain.Grains.Orders;
|
|
|
|
namespace NTP.Tests;
|
|
|
|
public class TenderOrderRulesTests
|
|
{
|
|
[Theory]
|
|
[InlineData(41, 100, false, "PCKD")]
|
|
[InlineData(41, 350, true, "PCKD")] // packed wins over tipper destinations
|
|
[InlineData(38, 350, false, "BULK_GALLAB")]
|
|
[InlineData(38, 440, false, "BULK_GALLAB")]
|
|
[InlineData(38, 465, false, "BULK_GALLAB")]
|
|
[InlineData(38, 100, true, "BULK_GALLAB")] // tipper tick-box
|
|
[InlineData(38, 100, false, "BULK")]
|
|
public void Cargo_class_follows_legacy_createTenderOrder(long cargo, long dest, bool tipper, string expected)
|
|
=> Assert.Equal(expected, TenderOrderRules.ResolveCargoClass(cargo, dest, tipper));
|
|
|
|
[Theory]
|
|
[InlineData(0, false)]
|
|
[InlineData(1, true)]
|
|
[InlineData(200, true)]
|
|
[InlineData(201, false)]
|
|
public void Quantity_is_between_1_and_200(int q, bool ok) => Assert.Equal(ok, TenderOrderRules.IsQuantityValid(q));
|
|
|
|
[Fact]
|
|
public void Order_date_must_be_today_to_today_plus_4()
|
|
{
|
|
var today = new DateTime(2026, 11, 2, 15, 30, 0);
|
|
Assert.True(TenderOrderRules.IsAllowedOrderDate(new DateTime(2026, 11, 2), today));
|
|
Assert.True(TenderOrderRules.IsAllowedOrderDate(new DateTime(2026, 11, 6), today));
|
|
Assert.False(TenderOrderRules.IsAllowedOrderDate(new DateTime(2026, 11, 1), today));
|
|
Assert.False(TenderOrderRules.IsAllowedOrderDate(new DateTime(2026, 11, 7), today));
|
|
}
|
|
|
|
[Fact]
|
|
public void Delete_is_offered_only_for_dates_after_today()
|
|
{
|
|
var today = new DateTime(2026, 11, 2, 15, 30, 0);
|
|
Assert.False(TenderOrderRules.CanDelete(new DateTime(2026, 11, 2), today));
|
|
Assert.True(TenderOrderRules.CanDelete(new DateTime(2026, 11, 3), today));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(41, false)]
|
|
[InlineData(38, true)]
|
|
public void Type_is_required_except_for_packed(long cargo, bool required) => Assert.Equal(required, TenderOrderRules.IsTypeRequired(cargo));
|
|
|
|
[Fact]
|
|
public void Create_validator_requires_type_only_for_non_packed()
|
|
{
|
|
var v = new CreateTenderOrderValidator();
|
|
var ok = new CreateTenderOrderRequest(41, 1, 2, DateTime.Today, 1, 10, false, null);
|
|
Assert.True(v.Validate(ok).IsValid);
|
|
Assert.False(v.Validate(ok with { CargoType = 38 }).IsValid);
|
|
Assert.True(v.Validate(ok with { CargoType = 38, Type = "COMP" }).IsValid);
|
|
Assert.False(v.Validate(ok with { Quantity = 201 }).IsValid);
|
|
Assert.False(v.Validate(ok with { Shift = 0 }).IsValid);
|
|
}
|
|
}
|
|
|
|
public class QuotaRulesTests
|
|
{
|
|
[Theory]
|
|
[InlineData(10, 10, true)]
|
|
[InlineData(11, 10, true)]
|
|
[InlineData(9, 10, false)]
|
|
[InlineData(0, 0, true)]
|
|
public void New_quota_cannot_be_below_used_quota(int quota, int done, bool ok) => Assert.Equal(ok, QuotaRules.CanSetQuota(quota, done));
|
|
}
|
|
|
|
public class BillingCalculatorTests
|
|
{
|
|
[Fact]
|
|
public void Wage_is_rounded_to_three_decimals_per_ton()
|
|
{
|
|
// wage 14.67 per ton, 32,500 kg => 476.775
|
|
Assert.Equal(476.775m, BillingCalculator.Wage(14.67m, 32_500m));
|
|
// 14.902 * 31,337 / 1000 = 466.973... -> 3 decimals, half away from zero
|
|
Assert.Equal(Math.Round(14.902m * 31_337m / 1000m, 3, MidpointRounding.AwayFromZero), BillingCalculator.Wage(14.902m, 31_337m));
|
|
}
|
|
|
|
[Fact]
|
|
public void Shortage_fine_is_zero_inside_the_allowance()
|
|
{
|
|
// allowance 0.4 % of 30,000 = 120 kg; lost 100 kg
|
|
Assert.Equal(0m, BillingCalculator.ShortageFine(30_000m, 29_900m, 0.004m, 243.076m));
|
|
Assert.Equal(0m, BillingCalculator.ShortageFine(30_000m, 29_880m, 0.004m, 243.076m)); // exactly at the limit
|
|
}
|
|
|
|
[Fact]
|
|
public void Shortage_fine_charges_only_the_excess_loss()
|
|
{
|
|
// lost 200 kg, allowed 120 kg => 80 kg * 243.076 / 1000 = 19.44608 -> 19.446
|
|
Assert.Equal(19.446m, BillingCalculator.ShortageFine(30_000m, 29_800m, 0.004m, 243.076m));
|
|
}
|
|
|
|
[Fact]
|
|
public void Excess_received_weight_is_not_fined()
|
|
=> Assert.Equal(0m, BillingCalculator.ShortageFine(30_000m, 30_100m, 0.004m, 243.076m));
|
|
|
|
[Fact]
|
|
public void Net_weight_is_gross_minus_tare() => Assert.Equal(12_500m, BillingCalculator.NetWeight(20_000m, 7_500m));
|
|
|
|
[Fact]
|
|
public void Discharge_may_not_precede_load()
|
|
{
|
|
Assert.True(BillingCalculator.IsDischargeAfterLoad(new DateTime(2026, 1, 1), new DateTime(2026, 1, 1)));
|
|
Assert.True(BillingCalculator.IsDischargeAfterLoad(new DateTime(2026, 1, 1), new DateTime(2026, 1, 2)));
|
|
Assert.False(BillingCalculator.IsDischargeAfterLoad(new DateTime(2026, 1, 2), new DateTime(2026, 1, 1)));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(0, true)]
|
|
[InlineData(50000, true)]
|
|
[InlineData(50001, false)]
|
|
[InlineData(-1, false)]
|
|
public void Sent_weight_range(int w, bool ok) => Assert.Equal(ok, BillingCalculator.IsSentWeightInRange(w));
|
|
|
|
[Theory]
|
|
[InlineData(215, 14.67)]
|
|
[InlineData(220, 14.902)]
|
|
[InlineData(225, 17.219)]
|
|
[InlineData(38, 243.076)]
|
|
[InlineData(39, 235.006)]
|
|
[InlineData(40, 243.076)]
|
|
[InlineData(41, 235.006)]
|
|
public void Price_table_matches_legacy(long id, double expected) => Assert.Equal((decimal)expected, BillingPrices.Get(id));
|
|
|
|
[Fact]
|
|
public void Unknown_price_is_null()
|
|
{
|
|
Assert.Null(BillingPrices.Get(1));
|
|
Assert.Null(BillingPrices.Get(null));
|
|
}
|
|
|
|
[Fact]
|
|
public void Add_billing_validator_requires_the_form_fields()
|
|
{
|
|
var v = new AddBillingValidator();
|
|
var ok = new AddBillingRequest(1, 2, 3, "501", DateTime.Today, DateTime.Today, 215, 38, "T1");
|
|
Assert.True(v.Validate(ok).IsValid);
|
|
Assert.False(v.Validate(ok with { SerialNo = "" }).IsValid);
|
|
Assert.False(v.Validate(ok with { SerialNo = "S1" }).IsValid); // invoice number: digits only (legacy Float check)
|
|
Assert.False(v.Validate(ok with { Tn = "" }).IsValid);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(38, 38, true)]
|
|
[InlineData(38, 39, false)]
|
|
[InlineData(74, 74, true)]
|
|
[InlineData(74, 38, false)]
|
|
[InlineData(39, 41, true)]
|
|
[InlineData(41, 39, true)]
|
|
[InlineData(39, 40, false)]
|
|
[InlineData(40, 38, true)]
|
|
public void Invoice_keeps_one_cargo_family(long onInvoice, long entered, bool allowed)
|
|
=> Assert.Equal(allowed, BillingCalculator.IsCargoAllowedOnInvoice(onInvoice, entered));
|
|
}
|
|
|
|
public class GrainsWeightRulesTests
|
|
{
|
|
[Theory]
|
|
[InlineData("12345", true)]
|
|
[InlineData("1234", false)]
|
|
[InlineData("123456", false)]
|
|
[InlineData("12a45", false)]
|
|
[InlineData("", false)]
|
|
[InlineData(null, false)]
|
|
public void Weight_is_five_digits(string? w, bool ok) => Assert.Equal(ok, GrainsWeightRules.IsValidWeight(w));
|
|
|
|
[Fact]
|
|
public void Event_remarks_keep_the_legacy_wording()
|
|
=> Assert.Equal("الوزن القائم: 25000 الوزن الفارغ: 12000", GrainsWeightRules.EventRemarks("25000", "12000"));
|
|
}
|
|
|
|
public class ReportMathTests
|
|
{
|
|
[Fact]
|
|
public void Percent_rounding()
|
|
{
|
|
Assert.Equal(33.3m, ReportMath.Percent1(1, 3));
|
|
Assert.Equal(66.7m, ReportMath.Percent1(2, 3));
|
|
Assert.Equal(33.33m, ReportMath.Percent2(1, 3));
|
|
Assert.Equal(0m, ReportMath.Percent1(0, 0));
|
|
}
|
|
}
|
|
|
|
public class TabularExportTests
|
|
{
|
|
private static readonly ExportTable Sample = new(
|
|
["Title, with comma"], ["A", "B", "C"],
|
|
[new object?[] { "x\"y", 1.5m, new DateTime(2026, 11, 2) }],
|
|
["Total", 1.5m, null]);
|
|
|
|
[Fact]
|
|
public void Csv_has_bom_quotes_and_invariant_numbers()
|
|
{
|
|
var bytes = TabularExport.ToCsv([Sample]);
|
|
Assert.Equal(new byte[] { 0xEF, 0xBB, 0xBF }, bytes[..3]);
|
|
var text = Encoding.UTF8.GetString(bytes, 3, bytes.Length - 3);
|
|
Assert.Contains("\"Title, with comma\"\r\n", text);
|
|
Assert.Contains("\"x\"\"y\",1.5,02/11/2026\r\n", text);
|
|
Assert.Contains("Total,1.5,\r\n", text);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("=1+1", "'=1+1")]
|
|
[InlineData("@SUM(A1)", "'@SUM(A1)")]
|
|
[InlineData("-5", "-5")]
|
|
[InlineData("-cmd", "'-cmd")]
|
|
[InlineData("plain", "plain")]
|
|
public void Csv_neutralises_formulas(string input, string expected) => Assert.Equal(expected, TabularExport.Csv(input));
|
|
|
|
[Theory]
|
|
[InlineData(0, "A")]
|
|
[InlineData(25, "Z")]
|
|
[InlineData(26, "AA")]
|
|
[InlineData(27, "AB")]
|
|
[InlineData(701, "ZZ")]
|
|
public void Column_names(int index, string expected) => Assert.Equal(expected, TabularExport.ColumnName(index));
|
|
|
|
[Fact]
|
|
public void Xlsx_is_a_zip_with_the_sheet_and_rtl_flag()
|
|
{
|
|
var bytes = TabularExport.ToXlsx([Sample], rightToLeft: true);
|
|
using var zip = new ZipArchive(new MemoryStream(bytes));
|
|
Assert.NotNull(zip.GetEntry("xl/workbook.xml"));
|
|
var sheet = zip.GetEntry("xl/worksheets/sheet1.xml")!;
|
|
using var reader = new StreamReader(sheet.Open());
|
|
var xml = reader.ReadToEnd();
|
|
Assert.Contains("rightToLeft=\"1\"", xml);
|
|
Assert.Contains("<v>1.5</v>", xml);
|
|
Assert.Contains("x"y", xml);
|
|
System.Xml.Linq.XDocument.Parse(xml); // well-formed
|
|
}
|
|
}
|