Tests: Rename Helpers/ to HelperTests/

This commit is contained in:
2024-01-22 20:59:25 +01:00
parent 89d20f4c42
commit 6cee604448
4 changed files with 4 additions and 4 deletions

View File

@ -0,0 +1,349 @@
using Elwig.Helpers;
using Elwig.Helpers.Billing;
namespace Tests.HelperTests {
[TestFixture]
public class BillingDataTest {
private static readonly string[] AttributeVariants = ["GV", "GVD", "GVK", "GVS", "GVZ", "WR", "WRS", "ZW", "ZWS", "ZWZ"];
[OneTimeSetUp]
public async Task SetupBilling() {
await BillingData.Init();
}
private static (string, string?) GetSortIdAttrId(string bucket) {
return (bucket[..2], bucket.Length > 2 ? bucket[2..] : null);
}
private static string GetQualId(double kmw) {
return kmw switch {
>= 17.0 => "KAB",
>= 15.0 => "QUW",
>= 14.0 => "LDW",
>= 10.6 => "RSW",
_ => "WEI",
};
}
private static void TestCalcOe(PaymentBillingData data, string bucket, double oe, decimal expected, string? qualid = null, bool geb = false) {
var (sortid, attrid) = GetSortIdAttrId(bucket);
var kmw = Utils.OeToKmw(oe);
var v = data.CalculatePrice(sortid, attrid, qualid ?? GetQualId(kmw), geb, oe, kmw);
Assert.That(Math.Round(v, 6), Is.EqualTo(expected));
}
private static void TestCalcKmw(PaymentBillingData data, string bucket, double kmw, decimal expected, string? qualid = null, bool geb = false) {
var (sortid, attrid) = GetSortIdAttrId(bucket);
var oe = Utils.KmwToOe(kmw);
var v = data.CalculatePrice(sortid, attrid, qualid ?? GetQualId(kmw), geb, oe, kmw);
Assert.That(Math.Round(v, 6), Is.EqualTo(expected));
}
[Test]
public void Test_01_Flatrate() {
var data = PaymentBillingData.FromJson("""
{
"mode": "elwig",
"version": 1,
"payment": 0.5,
"curves": []
}
""", AttributeVariants);
Assert.Multiple(() => {
TestCalcOe(data, "GV", 73, 0.5m);
TestCalcOe(data, "WRS", 74, 0.5m);
});
}
[Test]
public void Test_02_Simple() {
var data = PaymentBillingData.FromJson("""
{
"mode": "elwig",
"version": 1,
"payment": "curve:1",
"curves": [{
"id": 1,
"mode": "oe",
"data": {
"72.0oe": 0.25,
"15.0kmw": 0.5,
"83oe": 1
},
"geb": 0.10
}]
}
""", AttributeVariants);
Assert.Multiple(() => {
TestCalcOe(data, "GV", 70, 0.25m);
TestCalcOe(data, "GV", 72, 0.25m);
TestCalcOe(data, "GV", 73, 0.50m);
TestCalcOe(data, "GV", 74, 0.55m);
TestCalcOe(data, "GV", 80, 0.85m);
TestCalcOe(data, "GV", 83, 1.00m);
TestCalcOe(data, "GV", 90, 1.00m);
TestCalcOe(data, "GV", 73, 0.60m, geb: true);
TestCalcOe(data, "GV", 74, 0.65m, geb: true);
TestCalcOe(data, "GV", 80, 0.95m, geb: true);
TestCalcOe(data, "GV", 83, 1.10m, geb: true);
TestCalcOe(data, "GV", 90, 1.10m, geb: true);
});
}
[Test]
public void Test_03_GreaterThanAndLessThan() {
var data = PaymentBillingData.FromJson("""
{
"mode": "elwig",
"version": 1,
"payment": "curve:1",
"curves": [{
"id": 1,
"mode": "kmw",
"data": {
"<14kmw": 0.1,
"14kmw": 0.2,
"<15kmw": 0.25,
"15kmw": 0.5,
"17kmw": 1,
">17kmw": 1.25
}
}]
}
""", AttributeVariants);
Assert.Multiple(() => {
TestCalcKmw(data, "GV", 13.00, 0.10m);
TestCalcKmw(data, "GV", 13.50, 0.10m);
TestCalcKmw(data, "GV", 13.99, 0.10m);
TestCalcKmw(data, "GV", 14.00, 0.20m);
TestCalcKmw(data, "GV", 14.50, 0.225m);
TestCalcKmw(data, "GV", 15.00, 0.50m);
TestCalcKmw(data, "GV", 15.50, 0.625m);
TestCalcKmw(data, "GV", 16.00, 0.75m);
TestCalcKmw(data, "GV", 16.50, 0.875m);
TestCalcKmw(data, "GV", 17.00, 1.00m);
TestCalcKmw(data, "GV", 17.01, 1.25m);
TestCalcKmw(data, "GV", 17.50, 1.25m);
TestCalcKmw(data, "GV", 18.00, 1.25m);
TestCalcKmw(data, "GV", 18.50, 1.25m);
});
}
[Test]
public void Test_04_VariantsAndAttributes() {
var data = PaymentBillingData.FromJson("""
{
"mode": "elwig",
"version": 1,
"payment": {
"default": 0.10,
"GV/": 0.20,
"ZW": 0.25,
"/S": 0.15,
"GV/K": 0.30
},
"curves": []
}
""", AttributeVariants);
Assert.Multiple(() => {
TestCalcOe(data, "WR", 73, 0.10m);
TestCalcOe(data, "WRS", 73, 0.15m);
TestCalcOe(data, "GV", 73, 0.20m);
TestCalcOe(data, "GVD", 73, 0.10m);
TestCalcOe(data, "GVK", 73, 0.30m);
TestCalcOe(data, "GVS", 73, 0.15m);
TestCalcOe(data, "GVZ", 73, 0.10m);
TestCalcOe(data, "ZW", 73, 0.25m);
TestCalcOe(data, "ZWS", 73, 0.15m);
TestCalcOe(data, "ZWZ", 73, 0.25m);
});
}
[Test]
public void Test_05_QualityLevel() {
var data = PaymentBillingData.FromJson("""
{
"mode": "elwig",
"version": 1,
"payment": 0.5,
"quality": {
"WEI": {
"default": 0.25,
"GV": 0.3,
"/S": 0.2
}
},
"curves": []
}
""", AttributeVariants);
Assert.Multiple(() => {
TestCalcOe(data, "GV", 75, 0.30m, qualid: "WEI");
TestCalcOe(data, "ZW", 76, 0.25m, qualid: "WEI");
TestCalcOe(data, "GVS", 75, 0.20m, qualid: "WEI");
TestCalcOe(data, "GVK", 74, 0.30m, qualid: "WEI");
TestCalcOe(data, "ZWS", 73, 0.20m, qualid: "WEI");
TestCalcOe(data, "GV", 70, 0.5m);
TestCalcOe(data, "GV", 72, 0.5m);
TestCalcOe(data, "GV", 73, 0.5m);
TestCalcOe(data, "ZWS", 74, 0.5m);
TestCalcOe(data, "GVK", 80, 0.5m);
});
}
[Test]
public void Test_06_ModeOeAndKmw() {
var data = PaymentBillingData.FromJson("""
{
"mode": "elwig",
"version": 1,
"payment": {
"default": 1.0,
"GV": "curve:1",
"ZW": "curve:2"
},
"curves": [{
"id": 1,
"mode": "oe",
"data": {
"73oe": 2.0,
"17kmw": 3.0
}
}, {
"id": 2,
"mode": "kmw",
"data": {
"73oe": 2.0,
"17kmw": 3.0
}
}]
}
""", AttributeVariants);
Assert.Multiple(() => {
TestCalcKmw(data, "GV", 15.0, 2.0m);
TestCalcKmw(data, "GV", 15.5, 2.272727m);
TestCalcKmw(data, "GV", 16.0, 2.454545m);
TestCalcKmw(data, "GV", 16.5, 2.727273m);
TestCalcKmw(data, "GV", 17.0, 3.0m);
TestCalcKmw(data, "ZW", 15.0, 2.0m);
TestCalcKmw(data, "ZW", 15.5, 2.25m);
TestCalcKmw(data, "ZW", 16.0, 2.50m);
TestCalcKmw(data, "ZW", 16.5, 2.75m);
TestCalcKmw(data, "ZW", 17.0, 3.0m);
});
}
[Test]
public void Test_07_MultipleCurves() {
var data = PaymentBillingData.FromJson("""
{
"mode": "elwig",
"version": 1,
"payment": {
"default": 0.25,
"/S": "curve:1",
"GV/": 0.75,
"GV/K": "curve:2",
"WR/S": "curve:3"
},
"curves": [{
"id": 3,
"mode": "kmw",
"data": {
"73oe": 0.7,
"17kmw": 0.8
},
"geb": {
"15kmw": 0.8,
"17kmw": 0.95
}
}, {
"id": 1,
"mode": "kmw",
"data": {
"73oe": 0.5,
"17kmw": 0.6
},
"geb": 0.1
}, {
"id": 2,
"mode": "kmw",
"data": {
"15kmw": 0.6,
"17kmw": 0.7
},
"geb": {
"73oe": 0.65,
"17kmw": 0.80
}
}]
}
""", AttributeVariants);
Assert.Multiple(() => {
TestCalcKmw(data, "GV", 15.0, 0.75m);
TestCalcKmw(data, "GVS", 15.0, 0.50m);
TestCalcKmw(data, "GVS", 16.0, 0.55m);
TestCalcKmw(data, "GVS", 17.0, 0.60m);
TestCalcKmw(data, "GVS", 15.0, 0.60m, geb: true);
TestCalcKmw(data, "GVS", 16.0, 0.65m, geb: true);
TestCalcKmw(data, "GVS", 17.0, 0.70m, geb: true);
TestCalcKmw(data, "GVK", 15.0, 0.60m);
TestCalcKmw(data, "GVK", 16.0, 0.65m);
TestCalcKmw(data, "GVK", 17.0, 0.70m);
TestCalcKmw(data, "GVK", 15.0, 0.65m, geb: true);
TestCalcKmw(data, "GVK", 16.0, 0.725m, geb: true);
TestCalcKmw(data, "GVK", 17.0, 0.80m, geb: true);
TestCalcKmw(data, "WRS", 15.0, 0.70m);
TestCalcKmw(data, "WRS", 16.0, 0.75m);
TestCalcKmw(data, "WRS", 17.0, 0.80m);
TestCalcKmw(data, "WRS", 15.0, 0.80m, geb: true);
TestCalcKmw(data, "WRS", 16.0, 0.875m, geb: true);
TestCalcKmw(data, "WRS", 17.0, 0.95m, geb: true);
});
}
[Test]
public void Test_08_WgMaster() {
var data = PaymentBillingData.FromJson("""
{
"mode": "wgmaster",
"Grundbetrag": 0.033,
"GBZS": 0.0,
"Ausgabefaktor": 1.0,
"Rebelzuschlag": 0.0,
"AufschlagVolllieferanten": 0.0,
"AuszahlungSorten": {
"BL/": 0.097,
"BP/": 0.097,
"GV/K": "curve:1",
"SL/": 0.097,
"ZW/": 0.097,
"default": "curve:0"
},
"AuszahlungSortenQualitätsstufe": {
"WEI": 0.005
},
"Kurven": [{
"id": 0,
"mode": "oe",
"data": 0.033,
"geb": 0
}, {
"id": 1,
"mode": "oe",
"data": {
"88oe": 0.032,
"89oe": 0.065
}
}]
}
""", AttributeVariants);
Assert.Multiple(() => {
TestCalcOe(data, "GVK", 73, 0.032m);
TestCalcOe(data, "ZWS", 74, 0.033m);
TestCalcOe(data, "GV", 75, 0.005m, qualid: "WEI");
TestCalcOe(data, "GVK", 115, 0.065m);
});
}
}
}

View File

@ -0,0 +1,30 @@
using Elwig.Helpers;
using Microsoft.Data.Sqlite;
using System.Reflection;
namespace Tests.HelperTests {
[TestFixture]
public class BillingTest {
private SqliteConnection? Connection;
[OneTimeSetUp]
public async Task SetupDatabase() {
Connection = await AppDbContext.ConnectAsync();
await AppDbContext.ExecuteEmbeddedScript(Connection, Assembly.GetExecutingAssembly(), "Tests.Resources.BillingInsert.sql");
}
[OneTimeTearDown]
public async Task TeardownDatabase() {
if (Connection == null) return;
await AppDbContext.ExecuteEmbeddedScript(Connection, Assembly.GetExecutingAssembly(), "Tests.Resources.BillingDelete.sql");
await Connection.DisposeAsync();
Connection = null;
}
[Test]
public void Test() {
// TODO
}
}
}

View File

@ -0,0 +1,104 @@
using Elwig.Helpers;
namespace Tests.HelperTests {
[TestFixture]
public class UtilsTest {
private static readonly double[,] Gradation = new double[,] {
{ 14.0, 68.0 },
{ 15.0, 73.0 },
{ 17.1, 84.0 },
{ 19.0, 94.0 },
{ 21.0, 105.0 },
{ 25.0, 127.0 },
{ 27.1, 139.0 },
{ 30.0, 156.0 },
};
[Test]
public void Test_KmwToOe() {
Assert.Multiple(() => {
for (int i = 0; i < Gradation.GetLength(0); i++) {
Assert.That(Utils.KmwToOe(Gradation[i, 0]), Is.EqualTo(Gradation[i, 1]));
}
});
}
[Test]
public void Test_OeToKmw() {
Assert.Multiple(() => {
for (int i = 0; i < Gradation.GetLength(0); i++) {
Assert.That(Utils.OeToKmw(Gradation[i, 1]), Is.EqualTo(Gradation[i, 0]));
}
});
}
[Test]
public void Test_DecFromDb() {
Assert.Multiple(() => {
Assert.That(Utils.DecFromDb(10670, 3), Is.EqualTo(10.67M));
Assert.That(Utils.DecFromDb(-1009999, 4), Is.EqualTo(-100.9999M));
Assert.That(Utils.DecFromDb(1, 2), Is.EqualTo(0.01M));
});
}
[Test]
public void Test_DecToDb() {
Assert.Multiple(() => {
Assert.That(Utils.DecToDb(219.48M, 2), Is.EqualTo(21948));
Assert.That(Utils.DecToDb(-1.2345M, 4), Is.EqualTo(-12345));
Assert.That(Utils.DecToDb(99190, 0), Is.EqualTo(99190));
Assert.That(Utils.DecToDb(817.9099M, 3), Is.EqualTo(817910));
Assert.That(Utils.DecToDb(-5618.944M, 2), Is.EqualTo(-561894));
});
}
[Test]
public void Test_Modulo() {
Assert.Multiple(() => {
Assert.That(Utils.Modulo("1", 2), Is.EqualTo(1));
Assert.That(Utils.Modulo("12", 11), Is.EqualTo(1));
Assert.That(Utils.Modulo("65", 16), Is.EqualTo(1));
Assert.That(Utils.Modulo("91746381048364", 10), Is.EqualTo(4));
Assert.That(Utils.Modulo("210501700012345678131468", 97), Is.EqualTo(1));
Assert.Throws<ArgumentException>(() => Utils.Modulo("", 4));
Assert.Throws<ArgumentException>(() => Utils.Modulo("1ab", 5));
Assert.Throws<ArgumentException>(() => Utils.Modulo("123", 1));
Assert.Throws<ArgumentException>(() => Utils.Modulo("456", 0));
Assert.Throws<ArgumentException>(() => Utils.Modulo("789", -1));
});
}
[Test]
public void Test_SplitAddress() {
Assert.Multiple(() => {
Assert.That(Utils.SplitAddress("Winzerstraße 1"), Is.EqualTo(("Winzerstraße", "1")));
Assert.That(Utils.SplitAddress("Auf dem Feld 12"), Is.EqualTo(("Auf dem Feld", "12")));
Assert.That(Utils.SplitAddress("Winzerstraße 5a"), Is.EqualTo(("Winzerstraße", "5a")));
Assert.That(Utils.SplitAddress("Winzerstraße 1-3/2"), Is.EqualTo(("Winzerstraße", "1-3/2")));
Assert.That(Utils.SplitAddress("Winzerstraße 3/4/5"), Is.EqualTo(("Winzerstraße", "3/4/5")));
Assert.That(Utils.SplitAddress("Winzerstraße 7/2/4/77"), Is.EqualTo(("Winzerstraße", "7/2/4/77")));
Assert.That(Utils.SplitAddress("Winzerstraße 95b"), Is.EqualTo(("Winzerstraße", "95b")));
Assert.That(Utils.SplitAddress("Winzerstraße 1, TOP 3"), Is.EqualTo(("Winzerstraße", "1, TOP 3")));
});
}
[Test]
public void Test_SplitName() {
Assert.Multiple(() => {
Assert.That(Utils.SplitName("Max Bauer", "Bauer"), Is.EqualTo(("Bauer", "Max")));
Assert.That(Utils.SplitName("Bauer Max", "Bauer"), Is.EqualTo(("Bauer", "Max")));
Assert.That(Utils.SplitName("Max und Moritz Bauer", "Bauer"), Is.EqualTo(("Bauer", "Max und Moritz")));
Assert.That(Utils.SplitName("Bauer Max und Moritz", "Bauer"), Is.EqualTo(("Bauer", "Max und Moritz")));
Assert.That(Utils.SplitName("Bauer GesbR", "Bauer"), Is.EqualTo(("Bauer", "GesbR")));
Assert.That(Utils.SplitName("Max und Moritz Bauer GesbR", "Bauer"), Is.EqualTo(("Bauer", "Max und Moritz GesbR")));
Assert.That(Utils.SplitName("Bauer Max und Moritz GesbR", "Bauer"), Is.EqualTo(("Bauer", "Max und Moritz GesbR")));
Assert.That(Utils.SplitName("Weingut Bauer", "Bauer"), Is.EqualTo(("Bauer", "Weingut")));
Assert.That(Utils.SplitName("Bauer Weingut", "Bauer"), Is.EqualTo(("Bauer", "Weingut")));
Assert.That(Utils.SplitName("Max und Moritz Bauer und Mustermann", "Bauer"), Is.EqualTo(("Bauer und Mustermann", "Max und Moritz")));
Assert.That(Utils.SplitName("Bauer und Mustermann Max und Moritz", "Bauer"), Is.EqualTo(("Bauer und Mustermann", "Max und Moritz")));
Assert.That(Utils.SplitName("ABC GesbR", "Bauer"), Is.EqualTo(((string, string?))("ABC GesbR", null)));
});
}
}
}

View File

@ -0,0 +1,113 @@
using Elwig.Helpers;
using System.Windows.Controls;
namespace Tests.HelperTests {
[TestFixture]
[Apartment(ApartmentState.STA)]
public class ValidatorTest {
private static TextBox TB(string value, int caret = 0) {
return new() {
Text = value,
CaretIndex = caret,
};
}
[Test]
public void Test_CheckInteger_Simple() {
Assert.Multiple(() => {
Assert.That(Validator.CheckInteger(TB(""), true).IsValid, Is.False);
Assert.That(Validator.CheckInteger(TB(""), false).IsValid, Is.True);
Assert.That(Validator.CheckInteger(TB("123"), true).IsValid, Is.True);
Assert.That(Validator.CheckInteger(TB("456"), false).IsValid, Is.True);
Assert.That(Validator.CheckInteger(TB("1234"), false, 4).IsValid, Is.True);
Assert.That(Validator.CheckInteger(TB("4567"), false, 3).IsValid, Is.True);
});
}
[Test]
public void Test_CheckInteger_Caret() {
var tb = TB("1a2b3c", 2);
Assert.Multiple(() => {
Assert.That(Validator.CheckInteger(tb, true).IsValid, Is.True);
Assert.That(tb.Text, Is.EqualTo("123"));
Assert.That(tb.CaretIndex, Is.EqualTo(1));
});
}
[Test]
public void Test_CheckInteger_MaxLen() {
var tb = TB("1a2b3c4d5e", 4);
Assert.Multiple(() => {
Assert.That(Validator.CheckInteger(tb, true, 3).IsValid, Is.True);
Assert.That(tb.Text, Is.EqualTo("123"));
Assert.That(tb.CaretIndex, Is.EqualTo(2));
});
}
[Test]
public void Test_CheckPhoneNumber_Simple() {
Assert.Multiple(() => {
Assert.That(Validator.CheckPhoneNumber(TB(""), true).IsValid, Is.False);
Assert.That(Validator.CheckPhoneNumber(TB("+43"), false).IsValid, Is.False);
Assert.That(Validator.CheckPhoneNumber(TB("066412345678"), true).IsValid, Is.True);
Assert.That(Validator.CheckPhoneNumber(TB("0ab66412cd345678"), true).IsValid, Is.True);
});
}
[Test]
public void Test_CheckPhoneNumber_Format_1() {
var tb = TB("066412345678", 5);
Assert.Multiple(() => {
Assert.That(Validator.CheckPhoneNumber(tb, true).IsValid, Is.True);
Assert.That(tb.Text, Is.EqualTo("+43 664 12345678"));
Assert.That(tb.CaretIndex, Is.EqualTo(9));
});
}
[Test]
public void Test_CheckPhoneNumber_Format_2() {
var tb = TB("0a2574b1c2d34..", 7);
Assert.Multiple(() => {
Assert.That(Validator.CheckPhoneNumber(tb, true).IsValid, Is.True);
Assert.That(tb.Text, Is.EqualTo("+43 2574 1234"));
Assert.That(tb.CaretIndex, Is.EqualTo(8));
});
}
[Test]
public void Test_CheckEmailAddress_Simple() {
Assert.Multiple(() => {
Assert.That(Validator.CheckEmailAddress(TB(""), true).IsValid, Is.False);
Assert.That(Validator.CheckEmailAddress(TB("name"), false).IsValid, Is.False);
Assert.That(Validator.CheckEmailAddress(TB("@"), false).IsValid, Is.False);
Assert.That(Validator.CheckEmailAddress(TB("@test.com"), false).IsValid, Is.False);
Assert.That(Validator.CheckEmailAddress(TB("name@a"), true).IsValid, Is.False);
Assert.That(Validator.CheckEmailAddress(TB("name@a.com."), true).IsValid, Is.False);
Assert.That(Validator.CheckEmailAddress(TB("name@a.com.a"), true).IsValid, Is.False);
Assert.That(Validator.CheckEmailAddress(TB("name@a.com"), true).IsValid, Is.True);
Assert.That(Validator.CheckEmailAddress(TB("my.name@hello#.com"), true).IsValid, Is.True);
});
}
[Test]
public void Test_CheckEmailAddress_Format_1() {
var tb = TB("my . name . is @heinz#.com", 17);
Assert.Multiple(() => {
Assert.That(Validator.CheckEmailAddress(tb, true).IsValid, Is.True);
Assert.That(tb.Text, Is.EqualTo("my.name.is@heinz.com"));
Assert.That(tb.CaretIndex, Is.EqualTo(12));
});
}
[Test]
public void Test_CheckEmailAddress_Format_2() {
var tb = TB("sabine.müsterfrau@heinz#.com.b", 30);
Assert.Multiple(() => {
Assert.That(Validator.CheckEmailAddress(tb, true).IsValid, Is.False);
Assert.That(tb.Text, Is.EqualTo("sabine.müsterfrau@heinz.com.b"));
Assert.That(tb.CaretIndex, Is.EqualTo(29));
});
}
}
}