-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGasolinePumpCarNonOverlappingExampleTests.cs
62 lines (60 loc) · 3.33 KB
/
GasolinePumpCarNonOverlappingExampleTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using ExampleClasses.GasStation;
using FluentAssertions;
using NUnit.Framework;
using TimeSlice;
namespace TimeSliceTests
{
/// <summary>
/// Test cases that are not thought to cover every edge case but rather demonstrate the main ideas of this library.
/// In this test class we cover <see cref="TimeDependentCollection{TRelation,TParent,TChild}" />s.
/// </summary>
/// <remarks>To understand these tests, first have a look at <seealso cref="GasolinePumpCarRelationExampleTests" /></remarks>
public class GasolinePumpCarNonOverlappingExampleTests
{
/// <summary>
/// demonstrates the use of the <see cref="TimeDependentCollection{TRelation,TParent,TChild}" /> with <see cref="TimeDependentCollectionType.PreventOverlaps" />
/// </summary>
[Test]
public void TestMultipleCarsAtOneGasolinePump()
{
var myCar = new Car();
var otherCar = new Car();
var theSinglePump = new GasolinePump();
// if my car is standing next to the only available gasoline pump for five minutes the allocation looks like this:
var myGasolinePumpAllocation = new GasolinePumpAllocation
{
Start = new DateTimeOffset(2021, 8, 1, 12, 0, 0, TimeSpan.Zero),
End = new DateTimeOffset(2021, 8, 1, 12, 5, 0, TimeSpan.Zero),
Parent = theSinglePump,
Child = myCar
};
var anotherGasolinePumpAllocation = new GasolinePumpAllocation
{
Start = new DateTimeOffset(2021, 8, 1, 12, 10, 0, TimeSpan.Zero), // a bit later
End = new DateTimeOffset(2021, 8, 1, 13, 20, 0, TimeSpan.Zero),
Parent = theSinglePump, // same pump
Child = otherCar // but different car
};
// collections are initialized by providing the common parent (the "1" in the 1:n cardinality)
var collection = new GasolinePumpAllocationCollection(theSinglePump);
collection.Add(myGasolinePumpAllocation); // this describes, that my car uses the gasoline pump
collection.Add(anotherGasolinePumpAllocation); // another car may use the same gasoline pump but only at a different time
// ...12:00....12:05..12:10.....12:20. ....... (time) --->
// .....|XXXXXXXX|.....|XXXXXXXXXX|........... (gasoline pump is occupied)
// [ my car )
// [other car)
collection.IsValid().Should().BeTrue(); // collections, that don't allow overlaps are valid if there are no overlaps
// we can try to make another car use the same gasoline pump in a time frame that overlaps with another allocation
var conflictingAllocation = new GasolinePumpAllocation
{
Start = new DateTimeOffset(2021, 8, 1, 12, 07, 0, TimeSpan.Zero), // overlap => conflict
End = new DateTimeOffset(2021, 8, 1, 12, 12, 0, TimeSpan.Zero),
Parent = theSinglePump, // same parking lot
Child = new Car()
};
collection.Add(conflictingAllocation);
collection.IsValid().Should().BeFalse(); // => if you always check for validity (e.g. before saving a collection or in some kind of middleware), you'll be fine
}
}
}