Tests: Add UnitTests folder
All checks were successful
Test / Run tests (push) Successful in 2m24s

This commit is contained in:
2025-06-03 20:57:53 +02:00
parent 623f55f5b0
commit 3493ff6df1
31 changed files with 31 additions and 31 deletions

View File

@ -0,0 +1,110 @@
using System.Net.Sockets;
using System.Net;
using System.Text;
namespace Tests.UnitTests.WeighingTests {
public abstract class MockScale : IDisposable {
protected readonly TcpListener Server;
public int IdentNr { get; set; } = 0;
public int Tare { get; set; } = 0;
public int Weight { get; set; } = 0;
public string? Error { get; set; } = null;
protected MockScale(int port) {
Server = new TcpListener(IPAddress.Loopback, port);
Server.Start(4);
}
public void Dispose() {
Server.Dispose();
GC.SuppressFinalize(this);
}
}
public class CommandMockScale : MockScale {
private readonly Func<string, int, int, string?, int, (string, bool)> Handler;
private readonly Thread ServerThread;
private bool IsRunning = true;
public CommandMockScale(int port, Func<string, int, int, string?, int, (string, bool)> handler) :
base(port) {
Handler = handler;
ServerThread = new Thread(new ParameterizedThreadStart(Serve));
ServerThread.Start();
}
private async void Serve(object? parameters) {
byte[] buffer = new byte[16];
while (IsRunning) {
try {
using var client = await Server.AcceptTcpClientAsync();
if (client == null) continue;
using var stream = client.GetStream();
while (true) {
int read = await stream.ReadAsync(buffer);
var (res, inc) = Handler(Encoding.ASCII.GetString(buffer, 0, read), Weight, Tare, Error, IdentNr + 1);
if (inc) IdentNr++;
await stream.WriteAsync(Encoding.ASCII.GetBytes(res));
}
} catch (Exception) {
// ignore
}
}
}
public new void Dispose() {
IsRunning = false;
ServerThread.Interrupt();
ServerThread.Join();
base.Dispose();
}
}
public class EventMockScale : MockScale {
private readonly Func<int, int, string?, int, (string, bool)> Handler;
private readonly Thread ServerThread;
private TcpClient? Client;
private bool IsRunning = true;
public EventMockScale(int port, Func<int, int, string?, int, (string, bool)> handler) :
base(port) {
Handler = handler;
ServerThread = new Thread(new ParameterizedThreadStart(Serve));
ServerThread.Start();
}
private async void Serve(object? parameters) {
while (IsRunning) {
try {
Client = await Server.AcceptTcpClientAsync();
} catch (Exception) {
// ignore
}
}
}
public async Task Weigh(int weight, int tare = 0) {
Weight = weight;
Tare = tare;
await Weigh();
}
public async Task Weigh() {
var (res, inc) = Handler(Weight, Tare, Error, IdentNr + 1);
if (inc) IdentNr++;
await Client!.GetStream().WriteAsync(Encoding.ASCII.GetBytes(res));
}
public new void Dispose() {
Client?.Dispose();
IsRunning = false;
ServerThread.Interrupt();
ServerThread.Join();
base.Dispose();
}
}
}

View File

@ -0,0 +1,181 @@
namespace Tests.UnitTests.WeighingTests {
public static class ScaleHandlers {
public static (string, bool) Handle_IT3000A(string req, int weight, int tare, string? error, int identNr) {
var modes = error?.Split(';') ?? [];
var overloaded = modes.Contains("overloaded");
var moving = modes.Contains("moving");
var invalid = modes.Contains("invalid");
var crc = modes.Contains("crc");
var unit = modes.Contains("unit");
Thread.Sleep(100);
if (invalid) {
return ("abcd\r\n", false);
} else if (!req.StartsWith('<') || !req.EndsWith('>')) {
return ("<31>\r\n", false);
}
req = req[1..^1];
bool incr;
if (req.Length > 3) {
return ("<32>\r\n", false);
} else if (req.StartsWith("RN")) {
incr = true;
} else if (req.StartsWith("RM")) {
incr = false;
} else {
return ("<32>\r\n", false);
}
if (overloaded) {
return ("<12>\r\n", false);
} else if (weight == 0) {
incr = false;
}
if (moving && incr)
return ("<13>\r\n", false);
string data = $"00{(moving ? 1 : 0)}0{new DateTime(2020, 10, 15, 12, 34, 0):dd.MM.yyHH:mm}{(incr ? identNr : 0),4}1" +
$"{weight + tare,8}{tare,8}{weight,8}{(unit ? "lb" : "kg")} {"1",3}";
ushort checksum = Elwig.Helpers.Utils.CalcCrc16Modbus(data);
if (crc) checksum += 10;
return ($"<{data}{checksum,8}>\r\n", incr);
}
public static (string, bool) Handle_IT6000E(string req, int weight, int tare, string? error, int identNr, string terminalNr) {
var modes = error?.Split(';') ?? [];
var overloaded = modes.Contains("overloaded");
var moving = modes.Contains("moving");
var invalid = modes.Contains("invalid");
var crc = modes.Contains("crc");
var unit = modes.Contains("unit");
if (invalid) {
return ("abcd\r\n", false);
} else if (!req.StartsWith('<') || !req.EndsWith('>')) {
return ("<31>\r\n", false);
}
req = req[1..^1];
bool incr;
if (req.Length > 3) {
return ("<32>\r\n", false);
} else if (req.StartsWith("RN")) {
incr = true;
} else if (req.StartsWith("RM")) {
incr = false;
} else {
return ("<32>\r\n", false);
}
if (overloaded)
return ("<12>\r\n", false);
if (moving && incr)
return ("<13>\r\n", false);
string data = $"00{(moving ? 1 : 0)}0{new DateTime(2020, 10, 8, 8, 47, 0):dd.MM.yyHH:mm}{(incr ? identNr : 0),4}1" +
$"{weight + tare,8}{tare,8}{weight,8}{(unit ? "lb" : "kg")} {terminalNr,3}";
ushort checksum = Elwig.Helpers.Utils.CalcCrc16Modbus(data);
if (crc) checksum += 10;
return ($"<{data}{checksum,8}>\r\n", incr);
}
public static (string, bool) Handle_L246(string req, int weight, int tare, string? error, int identNr) {
var modes = error?.Split(';') ?? [];
var overloaded = modes.Contains("overloaded");
var moving = modes.Contains("moving");
var invalid = modes.Contains("invalid");
var crc = modes.Contains("crc");
var unit = modes.Contains("unit");
Thread.Sleep(100);
if (invalid) {
return ("abcd\r\n", false);
} else if (!req.StartsWith('<') || !req.EndsWith('>')) {
return ("<31>\r\n", false);
}
req = req[1..^1];
bool incr;
if (req.Length > 3) {
return ("<32>\r\n", false);
} else if (req.StartsWith("RN")) {
incr = true;
} else if (req.StartsWith("RM")) {
incr = false;
} else {
return ("<32>\r\n", false);
}
if (overloaded)
return ("<12>\r\n", false);
if (moving && incr)
return ("<13>\r\n", false);
string data = $"00{(moving ? 1 : 0)}0{new DateTime(2020, 10, 17, 14, 23, 0):dd.MM.yyHH:mm}{(incr ? identNr : 0),4}1" +
$"{weight + tare,8}{tare,8}{weight,8}{(unit ? "lb" : "kg")} {"001",3}";
ushort checksum = Elwig.Helpers.Utils.CalcCrc16Modbus(data);
if (crc) checksum += 10;
return ($"<{data}{checksum,8}>\r\n", incr);
}
public static (string, bool) HandleDMA02(string req, int weight, int tare, string? error, int identNr) {
var modes = error?.Split(';') ?? [];
var overloaded = modes.Contains("overloaded");
var moving = modes.Contains("moving");
var invalid = modes.Contains("invalid");
Thread.Sleep(100);
if (invalid) {
return ("abcd\x03", false);
} else if (req != "\x05" && req != "?") {
return ("\x0002ES\x03", false);
}
bool incr;
if (req == "?") {
incr = false;
} else {
incr = true;
}
if (overloaded) {
return ("\x0002ES\x03", false);
} else if (weight == 0) {
incr = false;
}
if (moving && incr)
return ("\x0002EM\x03", false);
string data = $" {(moving ? "M" : "S")}{weight + tare,7}{tare,7}{weight,7} 1{(incr ? identNr : 0),6}{new DateTime(2020, 10, 15, 12, 34, 0):yyyyMMddHHmmss}";
return ($"\x0002{data}\x03", incr);
}
public static (string, bool) HandleDMA03Baby(string req, int weight, int tare, string? error, int identNr) {
return HandleDMA02(req, weight, tare, error, identNr);
}
public static (string, bool) Handle_L320(int weight, int tare, string? error, int identNr) {
var modes = error?.Split(';') ?? [];
var invalid = modes.Contains("invalid");
var unit = modes.Contains("unit");
Thread.Sleep(100);
if (invalid) {
return ("abcd", false);
}
bool incr = true;
return ($" {new DateTime(2020, 9, 28, 9, 8, 0):dd.MM.yy HH:mm} {identNr,4} {weight,9}{(unit ? "lb" : "kg")} ", incr);
}
}
}

View File

@ -0,0 +1,56 @@
using Elwig.Helpers.Weighing;
namespace Tests.UnitTests.WeighingTests {
[TestFixture]
public class ScaleTestBadenL320 {
private EventMockScale Mock;
private AveryEventScale Scale;
[OneTimeSetUp]
public void SetupScale() {
Mock = new EventMockScale(12345, ScaleHandlers.Handle_L320);
Scale = new("1", "L320", "tcp://127.0.0.1:12345");
}
[OneTimeTearDown]
public void TeardownScale() {
Mock.Dispose();
Scale.Dispose();
}
[SetUp]
public void ResetScale() {
Mock.IdentNr = 0;
Mock.Weight = 0;
Mock.Error = null;
}
[Test]
public async Task Test_01_Normal() {
WeighingResult? res = null;
Scale.WeighingEvent += (sender, evt) => {
res = evt.Result;
};
await Mock.Weigh(2345);
await Task.Delay(100);
Assert.That(res, Is.Not.Null);
Assert.That(res, Is.EqualTo(new WeighingResult {
NetWeight = 2345,
WeighingId = "1", FullWeighingId = $"2020-09-28/1",
Date = new DateOnly(2020, 9, 28), Time = new TimeOnly(9, 8),
}));
await Mock.Weigh(4215);
await Task.Delay(100);
Assert.That(res, Is.Not.Null);
Assert.That(res, Is.EqualTo(new WeighingResult {
NetWeight = 4215,
WeighingId = "2", FullWeighingId = $"2020-09-28/2",
Date = new DateOnly(2020, 9, 28),
Time = new TimeOnly(9, 8),
}));
}
}
}

View File

@ -0,0 +1,108 @@
using Elwig.Helpers.Weighing;
namespace Tests.UnitTests.WeighingTests {
[TestFixture]
class ScaleTestGrInzersdorfL246 {
private MockScale Mock;
private SysTecITScale Scale;
[OneTimeSetUp]
public void SetupScale() {
Mock = new CommandMockScale(12345, ScaleHandlers.Handle_L246);
Scale = new("1", "L246", "tcp://127.0.0.1:12345");
}
[OneTimeTearDown]
public void TeardownScale() {
Mock.Dispose();
Scale.Dispose();
}
[SetUp]
public void ResetScale() {
Mock.IdentNr = 0;
Mock.Weight = 0;
Mock.Error = null;
}
[Test]
public async Task Test_01_CurrentWeight() {
Mock.Weight = 1235;
Assert.That(await Scale!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
GrossWeight = 1235, TareWeight = 0, NetWeight = 1235,
Date = new DateOnly(2020, 10, 17), Time = new TimeOnly(14, 23),
}));
Mock.Weight = 1240;
Assert.That(await Scale!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
GrossWeight = 1240, TareWeight = 0, NetWeight = 1240,
Date = new DateOnly(2020, 10, 17), Time = new TimeOnly(14, 23),
}));
Mock.Weight = 1245;
Assert.That(await Scale!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
GrossWeight = 1245, TareWeight = 0, NetWeight = 1245,
Date = new DateOnly(2020, 10, 17), Time = new TimeOnly(14, 23),
}));
}
[Test]
public async Task Test_02_Normal() {
Mock.Weight = 1235;
Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
GrossWeight = 1235, TareWeight = 0, NetWeight = 1235,
WeighingId = "1", FullWeighingId = $"2020-10-17/1",
Date = new DateOnly(2020, 10, 17), Time = new TimeOnly(14, 23),
}));
Mock.Weight = 3335;
Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
GrossWeight = 3335, TareWeight = 0, NetWeight = 3335,
WeighingId = "2", FullWeighingId = $"2020-10-17/2",
Date = new DateOnly(2020, 10, 17), Time = new TimeOnly(14, 23),
}));
Mock.Weight = 6420;
Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
GrossWeight = 6420, TareWeight = 0, NetWeight = 6420,
WeighingId = "3", FullWeighingId = $"2020-10-17/3",
Date = new DateOnly(2020, 10, 17), Time = new TimeOnly(14, 23),
}));
}
[Test]
public void Test_03_Moving() {
Mock.Weight = 1_000;
Mock.Error = "moving";
IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
Assert.That(ex.Message, Contains.Substring("Waage in Bewegung"));
}
[Test]
public void Test_04_Overloaded() {
Mock.Weight = 10_000;
Mock.Error = "overloaded";
IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
Assert.That(ex.Message, Contains.Substring("Waage in Überlast"));
}
[Test]
public void Test_05_InvalidResponse() {
Mock.Weight = 1_000;
Mock.Error = "invalid";
Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
}
[Test]
public void Test_06_InvalidCrc() {
Mock.Weight = 1_000;
Mock.Error = "crc";
IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
Assert.That(ex.Message, Contains.Substring("Invalid CRC16 checksum"));
}
[Test]
public void Test_07_InvalidUnit() {
Mock.Weight = 1_000;
Mock.Error = "unit";
IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
}
}
}

View File

@ -0,0 +1,115 @@
using Elwig.Helpers.Weighing;
namespace Tests.UnitTests.WeighingTests {
[TestFixture]
public class ScaleTestHaugsdorfDMA02 {
private MockScale Mock;
private GassnerScale Scale;
[OneTimeSetUp]
public void SetupScale() {
Mock = new CommandMockScale(12345, ScaleHandlers.HandleDMA02);
Scale = new("1", "DMA02", "tcp://127.0.0.1:12345");
}
[OneTimeTearDown]
public void TeardownScale() {
Mock.Dispose();
Scale.Dispose();
}
[SetUp]
public void ResetScale() {
Mock.IdentNr = 0;
Mock.Weight = 0;
Mock.Tare = 0;
Mock.Error = null;
}
[Test]
public async Task Test_01_CurrentWeight() {
Mock.Weight = 1234;
Mock.Tare = 0;
Assert.That(await Scale!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
GrossWeight = 1234,
TareWeight = 0,
NetWeight = 1234,
Date = new DateOnly(2020, 10, 15),
Time = new TimeOnly(12, 34),
}));
Mock.Weight = 1235;
Mock.Tare = 46;
Assert.That(await Scale!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
GrossWeight = 1281,
TareWeight = 46,
NetWeight = 1235,
Date = new DateOnly(2020, 10, 15),
Time = new TimeOnly(12, 34),
}));
Mock.Weight = 1236;
Mock.Tare = 92;
Assert.That(await Scale!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
GrossWeight = 1328,
TareWeight = 92,
NetWeight = 1236,
Date = new DateOnly(2020, 10, 15),
Time = new TimeOnly(12, 34),
}));
}
[Test]
public async Task Test_02_Normal() {
Mock.Weight = 1234;
Mock.Tare = 92;
Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
GrossWeight = 1326,
TareWeight = 92,
NetWeight = 1234,
WeighingId = "1",
FullWeighingId = "1",
Date = new DateOnly(2020, 10, 15),
Time = new TimeOnly(12, 34),
}));
Mock.Weight = 3333;
Mock.Tare = 41;
Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
GrossWeight = 3374,
TareWeight = 41,
NetWeight = 3333,
WeighingId = "2",
FullWeighingId = "2",
Date = new DateOnly(2020, 10, 15),
Time = new TimeOnly(12, 34),
}));
Mock.Weight = 4321;
Mock.Tare = 0;
Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
GrossWeight = 4321,
TareWeight = 0,
NetWeight = 4321,
WeighingId = "3",
FullWeighingId = "3",
Date = new DateOnly(2020, 10, 15),
Time = new TimeOnly(12, 34),
}));
}
[Test]
public void Test_03_Moving() {
Mock.Weight = 1_000;
Mock.Tare = 41;
Mock.Error = "moving";
IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
Assert.That(ex.Message, Contains.Substring("Waage in Bewegung"));
}
[Test]
public void Test_04_InvalidResponse() {
Mock.Weight = 1_000;
Mock.Tare = 41;
Mock.Error = "invalid";
Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
}
}
}

View File

@ -0,0 +1,108 @@
using Elwig.Helpers.Weighing;
namespace Tests.UnitTests.WeighingTests {
[TestFixture]
class ScaleTestMatzenIT3000A {
private MockScale Mock;
private SysTecITScale Scale;
[OneTimeSetUp]
public void SetupScale() {
Mock = new CommandMockScale(12345, ScaleHandlers.Handle_IT3000A);
Scale = new("1", "IT3000A", "tcp://127.0.0.1:12345");
}
[OneTimeTearDown]
public void TeardownScale() {
Mock.Dispose();
Scale.Dispose();
}
[SetUp]
public void ResetScale() {
Mock.IdentNr = 0;
Mock.Weight = 0;
Mock.Error = null;
}
[Test]
public async Task Test_01_CurrentWeight() {
Mock.Weight = 1234;
Assert.That(await Scale!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
GrossWeight = 1234, TareWeight = 0, NetWeight = 1234,
Date = new DateOnly(2020, 10, 15), Time = new TimeOnly(12, 34),
}));
Mock.Weight = 1235;
Assert.That(await Scale!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
GrossWeight = 1235, TareWeight = 0, NetWeight = 1235,
Date = new DateOnly(2020, 10, 15), Time = new TimeOnly(12, 34),
}));
Mock.Weight = 1236;
Assert.That(await Scale!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
GrossWeight = 1236, TareWeight = 0, NetWeight = 1236,
Date = new DateOnly(2020, 10, 15), Time = new TimeOnly(12, 34),
}));
}
[Test]
public async Task Test_02_Normal() {
Mock.Weight = 1234;
Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
GrossWeight = 1234, TareWeight = 0, NetWeight = 1234,
WeighingId = "1", FullWeighingId = $"2020-10-15/1",
Date = new DateOnly(2020, 10, 15), Time = new TimeOnly(12, 34),
}));
Mock.Weight = 3333;
Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
GrossWeight = 3333, TareWeight = 0, NetWeight = 3333,
WeighingId = "2", FullWeighingId = $"2020-10-15/2",
Date = new DateOnly(2020, 10, 15), Time = new TimeOnly(12, 34),
}));
Mock.Weight = 4321;
Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
GrossWeight = 4321, TareWeight = 0, NetWeight = 4321,
WeighingId = "3", FullWeighingId = $"2020-10-15/3",
Date = new DateOnly(2020, 10, 15), Time = new TimeOnly(12, 34),
}));
}
[Test]
public void Test_03_Moving() {
Mock.Weight = 1_000;
Mock.Error = "moving";
IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
Assert.That(ex.Message, Contains.Substring("Waage in Bewegung"));
}
[Test]
public void Test_04_Overloaded() {
Mock.Weight = 10_000;
Mock.Error = "overloaded";
IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
Assert.That(ex.Message, Contains.Substring("Waage in Überlast"));
}
[Test]
public void Test_05_InvalidResponse() {
Mock.Weight = 1_000;
Mock.Error = "invalid";
Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
}
[Test]
public void Test_06_InvalidCrc() {
Mock.Weight = 1_000;
Mock.Error = "crc";
IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
Assert.That(ex.Message, Contains.Substring("Invalid CRC16 checksum"));
}
[Test]
public void Test_07_InvalidUnit() {
Mock.Weight = 1_000;
Mock.Error = "unit";
IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
}
}
}

View File

@ -0,0 +1,115 @@
using Elwig.Helpers.Weighing;
namespace Tests.UnitTests.WeighingTests {
[TestFixture]
public class ScaleTestSitzendorfDMA03Baby {
private MockScale Mock;
private GassnerScale Scale;
[OneTimeSetUp]
public void SetupScale() {
Mock = new CommandMockScale(12345, ScaleHandlers.HandleDMA03Baby);
Scale = new("1", "DMA03-baby", "tcp://127.0.0.1:12345");
}
[OneTimeTearDown]
public void TeardownScale() {
Mock.Dispose();
Scale.Dispose();
}
[SetUp]
public void ResetScale() {
Mock.IdentNr = 0;
Mock.Weight = 0;
Mock.Tare = 0;
Mock.Error = null;
}
[Test]
public async Task Test_01_CurrentWeight() {
Mock.Weight = 1234;
Mock.Tare = 0;
Assert.That(await Scale!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
GrossWeight = 1234,
TareWeight = 0,
NetWeight = 1234,
Date = new DateOnly(2020, 10, 15),
Time = new TimeOnly(12, 34),
}));
Mock.Weight = 1235;
Mock.Tare = 46;
Assert.That(await Scale!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
GrossWeight = 1281,
TareWeight = 46,
NetWeight = 1235,
Date = new DateOnly(2020, 10, 15),
Time = new TimeOnly(12, 34),
}));
Mock.Weight = 1236;
Mock.Tare = 92;
Assert.That(await Scale!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
GrossWeight = 1328,
TareWeight = 92,
NetWeight = 1236,
Date = new DateOnly(2020, 10, 15),
Time = new TimeOnly(12, 34),
}));
}
[Test]
public async Task Test_02_Normal() {
Mock.Weight = 1234;
Mock.Tare = 92;
Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
GrossWeight = 1326,
TareWeight = 92,
NetWeight = 1234,
WeighingId = "1",
FullWeighingId = "1",
Date = new DateOnly(2020, 10, 15),
Time = new TimeOnly(12, 34),
}));
Mock.Weight = 3333;
Mock.Tare = 41;
Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
GrossWeight = 3374,
TareWeight = 41,
NetWeight = 3333,
WeighingId = "2",
FullWeighingId = "2",
Date = new DateOnly(2020, 10, 15),
Time = new TimeOnly(12, 34),
}));
Mock.Weight = 4321;
Mock.Tare = 0;
Assert.That(await Scale!.Weigh(), Is.EqualTo(new WeighingResult {
GrossWeight = 4321,
TareWeight = 0,
NetWeight = 4321,
WeighingId = "3",
FullWeighingId = "3",
Date = new DateOnly(2020, 10, 15),
Time = new TimeOnly(12, 34),
}));
}
[Test]
public void Test_03_Moving() {
Mock.Weight = 1_000;
Mock.Tare = 41;
Mock.Error = "moving";
IOException ex = Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
Assert.That(ex.Message, Contains.Substring("Waage in Bewegung"));
}
[Test]
public void Test_04_InvalidResponse() {
Mock.Weight = 1_000;
Mock.Tare = 41;
Mock.Error = "invalid";
Assert.ThrowsAsync<IOException>(async () => await Scale!.Weigh());
}
}
}

View File

@ -0,0 +1,128 @@
using Elwig.Helpers.Weighing;
namespace Tests.UnitTests.WeighingTests {
[TestFixture]
class ScaleTestWolkersdorfIT6000E {
private MockScale MockA;
private MockScale MockB;
private SysTecITScale ScaleA;
private SysTecITScale ScaleB;
[OneTimeSetUp]
public void SetupScale() {
MockA = new CommandMockScale(12345, (req, weight, tare, error, identNr) => ScaleHandlers.Handle_IT6000E(req, weight, tare, error, identNr, "A"));
MockB = new CommandMockScale(12346, (req, weight, tare, error, identNr) => ScaleHandlers.Handle_IT6000E(req, weight, tare, error, identNr, "B"));
ScaleA = new("A", "IT3000E", "tcp://127.0.0.1:12345");
ScaleB = new("B", "IT3000E", "tcp://127.0.0.1:12346");
}
[OneTimeTearDown]
public void TeardownScale() {
MockA.Dispose();
MockB.Dispose();
ScaleA.Dispose();
ScaleB.Dispose();
}
[SetUp]
public void ResetScale() {
MockA.IdentNr = 0;
MockA.Weight = 0;
MockA.Error = null;
MockB.IdentNr = 0;
MockB.Weight = 0;
MockB.Error = null;
}
[Test]
public async Task Test_01_CurrentWeight() {
MockA.Weight = 1234;
Assert.That(await ScaleA!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
GrossWeight = 1234, TareWeight = 0, NetWeight = 1234,
Date = new DateOnly(2020, 10, 8), Time = new TimeOnly(8, 47),
}));
MockB.Weight = 3456;
Assert.That(await ScaleB!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
GrossWeight = 3456, TareWeight = 0, NetWeight = 3456,
Date = new DateOnly(2020, 10, 8), Time = new TimeOnly(8, 47),
}));
MockA.Weight = 1236;
Assert.That(await ScaleA!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
GrossWeight = 1236, TareWeight = 0, NetWeight = 1236,
Date = new DateOnly(2020, 10, 8), Time = new TimeOnly(8, 47),
}));
MockB.Weight = 3457;
Assert.That(await ScaleB!.GetCurrentWeight(), Is.EqualTo(new WeighingResult {
GrossWeight = 3457, TareWeight = 0, NetWeight = 3457,
Date = new DateOnly(2020, 10, 8), Time = new TimeOnly(8, 47),
}));
}
[Test]
public async Task Test_02_Normal() {
MockA.Weight = 1234;
Assert.That(await ScaleA!.Weigh(), Is.EqualTo(new WeighingResult {
GrossWeight = 1234, TareWeight = 0, NetWeight = 1234,
WeighingId = "1", FullWeighingId = $"2020-10-08/1",
Date = new DateOnly(2020, 10, 8), Time = new TimeOnly(8, 47),
}));
MockB.Weight = 3456;
Assert.That(await ScaleB!.Weigh(), Is.EqualTo(new WeighingResult {
GrossWeight = 3456, TareWeight = 0, NetWeight = 3456,
WeighingId = "1", FullWeighingId = $"2020-10-08/1",
Date = new DateOnly(2020, 10, 8), Time = new TimeOnly(8, 47),
}));
MockA.Weight = 4321;
Assert.That(await ScaleA!.Weigh(), Is.EqualTo(new WeighingResult {
GrossWeight = 4321, TareWeight = 0, NetWeight = 4321,
WeighingId = "2", FullWeighingId = $"2020-10-08/2",
Date = new DateOnly(2020, 10, 8), Time = new TimeOnly(8, 47),
}));
MockB.Weight = 3333;
Assert.That(await ScaleB!.Weigh(), Is.EqualTo(new WeighingResult {
GrossWeight = 3333, TareWeight = 0, NetWeight = 3333,
WeighingId = "2", FullWeighingId = $"2020-10-08/2",
Date = new DateOnly(2020, 10, 8), Time = new TimeOnly(8, 47),
}));
}
[Test]
public void Test_03_Moving() {
MockA.Weight = 1_000;
MockA.Error = "moving";
IOException ex = Assert.ThrowsAsync<IOException>(async () => await ScaleA!.Weigh());
Assert.That(ex.Message, Contains.Substring("Waage in Bewegung"));
}
[Test]
public void Test_04_Overloaded() {
MockA.Weight = 10_000;
MockA.Error = "overloaded";
IOException ex = Assert.ThrowsAsync<IOException>(async () => await ScaleA!.Weigh());
Assert.That(ex.Message, Contains.Substring("Waage in Überlast"));
}
[Test]
public void Test_05_InvalidResponse() {
MockA.Weight = 1_000;
MockA.Error = "invalid";
Assert.ThrowsAsync<IOException>(async () => await ScaleA!.Weigh());
}
[Test]
public void Test_06_InvalidCrc() {
MockA.Weight = 1_000;
MockA.Error = "crc";
IOException ex = Assert.ThrowsAsync<IOException>(async () => await ScaleA!.Weigh());
Assert.That(ex.Message, Contains.Substring("Invalid CRC16 checksum"));
}
[Test]
public void Test_07_InvalidUnit() {
MockA.Weight = 1_000;
MockA.Error = "unit";
IOException ex = Assert.ThrowsAsync<IOException>(async () => await ScaleA!.Weigh());
}
}
}

View File

@ -0,0 +1,6 @@
namespace Tests.UnitTests.WeighingTests {
public static class Utils {
public static DateOnly Today => DateOnly.FromDateTime(DateTime.Now);
public static TimeOnly Time => new(DateTime.Now.Hour, DateTime.Now.Minute);
}
}