sharee.bike-App/SharedBusinessLogic.Tests/Model/State/TestStateRequestedInfo.cs

116 lines
4.7 KiB
C#
Raw Normal View History

2023-05-11 17:39:28 +02:00
using System;
2021-07-12 21:31:46 +02:00
using System.Collections.Generic;
2022-08-30 15:42:25 +02:00
using NUnit.Framework;
2024-04-09 12:53:23 +02:00
using ShareeBike.Model.State;
2021-07-12 21:31:46 +02:00
2024-04-09 12:53:23 +02:00
namespace SharedBusinessLogic.Tests
2021-07-12 21:31:46 +02:00
{
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
[TestFixture]
public class TestStateRequestedInfo
{
[Test]
public void TestConstruct_FromApp()
{
2024-04-09 12:53:23 +02:00
Assert.That(
new StateRequestedInfo(() => new DateTime(2017, 09, 20, 12, 1, 0), new DateTime(2017, 09, 20, 12, 0, 0), TimeSpan.FromMinutes(15), "a@b", null).Value, Is.EqualTo(InUseStateEnum.Reserved));
2022-09-06 16:08:19 +02:00
2024-04-09 12:53:23 +02:00
Assert.That(
new StateRequestedInfo(() => new DateTime(2017, 09, 20, 12, 1, 0), new DateTime(2017, 09, 20, 12, 0, 0), TimeSpan.FromMinutes(15), "a@b", null).MailAddress, Is.EqualTo("a@b"));
2022-09-06 16:08:19 +02:00
2024-04-09 12:53:23 +02:00
Assert.That(
new StateRequestedInfo(() => new DateTime(2017, 09, 20, 12, 1, 0), new DateTime(2017, 09, 20, 12, 0, 0), TimeSpan.FromMinutes(15), "a@b", null).Code, Is.Null);
2022-09-06 16:08:19 +02:00
2024-04-09 12:53:23 +02:00
Assert.That(
new StateRequestedInfo(() => new DateTime(2017, 09, 20, 12, 1, 0), new DateTime(2017, 09, 20, 12, 0, 0), TimeSpan.FromMinutes(15), "a@b", null).From, Is.EqualTo(new DateTime(2017, 09, 20, 12, 0, 0)));
2022-09-06 16:08:19 +02:00
2024-04-09 12:53:23 +02:00
Assert.That(
new StateRequestedInfo(() => new DateTime(2017, 09, 20, 12, 1, 0), new DateTime(2017, 09, 20, 12, 0, 0), TimeSpan.FromMinutes(15), "a@b", null).GetIsStillReserved(out TimeSpan? l_oRemainigTime), Is.True);
2022-09-06 16:08:19 +02:00
2024-04-09 12:53:23 +02:00
Assert.That(
l_oRemainigTime.Value.Minutes, Is.EqualTo(14));
2022-09-06 16:08:19 +02:00
}
[Test]
public void TestConstruct_FromWebserver()
{
2024-04-09 12:53:23 +02:00
Assert.That(
new StateRequestedInfo(() => new DateTime(2017, 09, 20), new DateTime(2017, 09, 19), TimeSpan.FromMinutes(15), "a@b", "372").Value, Is.EqualTo(InUseStateEnum.Reserved));
2022-09-06 16:08:19 +02:00
2024-04-09 12:53:23 +02:00
Assert.That(
new StateRequestedInfo(() => new DateTime(2017, 09, 20), new DateTime(2017, 09, 19), TimeSpan.FromMinutes(15), "a@b", "372").MailAddress, Is.EqualTo("a@b"));
2022-09-06 16:08:19 +02:00
2024-04-09 12:53:23 +02:00
Assert.That(
new StateRequestedInfo(() => new DateTime(2017, 09, 20), new DateTime(2017, 09, 19), TimeSpan.FromMinutes(15), "a@b", "372").Code, Is.EqualTo("372"));
2022-09-06 16:08:19 +02:00
2024-04-09 12:53:23 +02:00
Assert.That(
new StateRequestedInfo(() => new DateTime(2017, 09, 20), new DateTime(2017, 09, 19), TimeSpan.FromMinutes(15), "a@b", "372").From, Is.EqualTo(new DateTime(2017, 09, 19)));
2022-09-06 16:08:19 +02:00
2024-04-09 12:53:23 +02:00
Assert.That(
new StateRequestedInfo(() => new DateTime(2017, 09, 20, 12, 12, 0), new DateTime(2017, 09, 20, 12, 0, 0), TimeSpan.FromMinutes(15), "a@b", "372").GetIsStillReserved(out TimeSpan? remainingTime), Is.True);
2022-09-06 16:08:19 +02:00
2024-04-09 12:53:23 +02:00
Assert.That(
remainingTime.Value.Minutes, Is.EqualTo(3));
2022-09-06 16:08:19 +02:00
}
[Test]
public void TestTryUpdateOnTimeElapsed()
{
var l_oReservedAt = new DateTime(2017, 09, 20, 22, 01, 00);
var l_oDateTimeMock = new DateTimeMocker(new List<DateTime> {
l_oReservedAt.Add(new TimeSpan(0, 4, 0)), // Reserved for 4 mns
2021-07-12 21:31:46 +02:00
l_oReservedAt.Add(new TimeSpan(0, 16, 0)) // Time elapsed since booking 16 mns
});
2023-06-06 12:00:24 +02:00
var l_oReservedInfo = new StateRequestedInfo(l_oDateTimeMock.GetDateTime, l_oReservedAt, TimeSpan.FromMinutes(15), "a@b", null);
2024-04-09 12:53:23 +02:00
Assert.That(l_oReservedInfo.MailAddress, Is.EqualTo("a@b"));
Assert.That(l_oReservedInfo.From, Is.EqualTo(new DateTime(2017, 09, 20, 22, 01, 00)), "a@b");
2022-09-06 16:08:19 +02:00
// Invoke first update (after simulated 4mns)
2024-04-09 12:53:23 +02:00
Assert.That(l_oReservedInfo.GetIsStillReserved(out TimeSpan? remainingTime), Is.True);
Assert.That(remainingTime.Value.Minutes, Is.EqualTo(11));
Assert.That(l_oReservedInfo.From, Is.EqualTo(new DateTime(2017, 09, 20, 22, 01, 00)), "a@b");
2022-09-06 16:08:19 +02:00
// Invoke second update (after simulated 16 mns)
2024-04-09 12:53:23 +02:00
Assert.That(l_oReservedInfo.GetIsStillReserved(out remainingTime), Is.False);
Assert.That(remainingTime, Is.Null);
Assert.That(l_oReservedInfo.From, Is.EqualTo(new DateTime(2017, 09, 20, 22, 01, 00)), "a@b");
2022-09-06 16:08:19 +02:00
}
2023-06-06 12:00:24 +02:00
[Test]
public void TestReservationTimeSpanCtor() =>
Assert.That(new StateRequestedInfo().MaxReservationTimeSpan, Is.EqualTo(TimeSpan.Zero));
[Test]
public void TestGetIsStillReserved() =>
Assert.That(new StateRequestedInfo(
() => new DateTime(2023, 5, 11, 14, 42, 0, 100), // one hundred millisecond after bike was reserved
new DateTime(2023, 5, 11, 14, 42, 0, 0),
TimeSpan.FromSeconds(1),
"a@b",
"").GetIsStillReserved(out _),
Is.True);
[Test]
public void TestGetIsStillReservedExpired() =>
Assert.That(new StateRequestedInfo(
() => new DateTime(2023, 5, 11, 14, 42, 0, 100), // one hundred millisecond after bike was reserved
new DateTime(2023, 5, 11, 14, 42, 0, 0),
TimeSpan.FromMilliseconds(50),
"a@b",
"").GetIsStillReserved(out _),
Is.False);
[Test]
public void TestGetIsStillReservedInvalidMaxReservationTimeSpan() =>
Assert.That(new StateRequestedInfo(
() => new DateTime(2023, 5, 11, 14, 42, 0, 1), // one hundred millisecond after bike was reserved
new DateTime(2023, 5, 11, 14, 42, 0, 0),
TimeSpan.Zero,
"a@b",
"").GetIsStillReserved(out _),
Is.False);
2022-09-06 16:08:19 +02:00
}
2021-07-12 21:31:46 +02:00
}