This repository has been archived by the owner on Oct 13, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
cargo_test.go
184 lines (153 loc) · 3.82 KB
/
cargo_test.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package shipping
import (
"testing"
"time"
)
func TestConstruction(t *testing.T) {
id := NextTrackingID()
spec := RouteSpecification{
Origin: SESTO,
Destination: AUMEL,
ArrivalDeadline: time.Date(2009, time.March, 13, 0, 0, 0, 0, time.UTC),
}
c := NewCargo(id, spec)
if c.Delivery.RoutingStatus != NotRouted {
t.Errorf("RoutingStatus = %v; want = %v",
c.Delivery.RoutingStatus, NotRouted)
}
if c.Delivery.TransportStatus != NotReceived {
t.Errorf("TransportStatus = %v; want = %v",
c.Delivery.TransportStatus, NotReceived)
}
if c.Delivery.LastKnownLocation != "" {
t.Errorf("LastKnownLocation = %s; want = %s",
c.Delivery.LastKnownLocation, "")
}
}
func TestRoutingStatus(t *testing.T) {
good := Itinerary{
Legs: []Leg{
{LoadLocation: SESTO, UnloadLocation: AUMEL},
},
}
bad := Itinerary{
Legs: []Leg{
{LoadLocation: SESTO, UnloadLocation: CNHKG},
},
}
acceptOnlyGood := RouteSpecification{
Origin: SESTO,
Destination: AUMEL,
}
c := NewCargo("ABC", RouteSpecification{})
c.SpecifyNewRoute(acceptOnlyGood)
if c.Delivery.RoutingStatus != NotRouted {
t.Errorf("RoutingStatus = %v; want = %v",
c.Delivery.RoutingStatus, NotRouted)
}
c.AssignToRoute(bad)
if c.Delivery.RoutingStatus != Misrouted {
t.Errorf("RoutingStatus = %v; want = %v",
c.Delivery.RoutingStatus, Misrouted)
}
c.AssignToRoute(good)
if c.Delivery.RoutingStatus != Routed {
t.Errorf("RoutingStatus = %v; want = %v",
c.Delivery.RoutingStatus, Routed)
}
}
func TestLastKnownLocation_WhenNoEvents(t *testing.T) {
c := NewCargo("ABC", RouteSpecification{
Origin: SESTO,
Destination: CNHKG,
})
if c.Delivery.LastKnownLocation != "" {
t.Errorf("should be equal")
}
}
func TestLastKnownLocation_WhenReceived(t *testing.T) {
c := populateCargoReceivedInStockholm()
if c.Delivery.LastKnownLocation != SESTO {
t.Errorf("LastKnownLocation = %s; want = %s",
c.Delivery.LastKnownLocation, SESTO)
}
}
func TestLastKnownLocation_WhenClaimed(t *testing.T) {
c := populateCargoClaimedInMelbourne()
if c.Delivery.LastKnownLocation != AUMEL {
t.Errorf("LastKnownLocation = %s; want = %s",
c.Delivery.LastKnownLocation, AUMEL)
}
}
var routingStatusTests = []struct {
routingStatus RoutingStatus
expected string
}{
{NotRouted, "Not routed"},
{Misrouted, "Misrouted"},
{Routed, "Routed"},
{1000, ""},
}
func TestRoutingStatus_Stringer(t *testing.T) {
for _, tt := range routingStatusTests {
if tt.routingStatus.String() != tt.expected {
t.Errorf("routingStatus.String() = %s; want = %s",
tt.routingStatus.String(), tt.expected)
}
}
}
var transportStatusTests = []struct {
transportStatus TransportStatus
expected string
}{
{NotReceived, "Not received"},
{InPort, "In port"},
{OnboardCarrier, "Onboard carrier"},
{Claimed, "Claimed"},
{Unknown, "Unknown"},
{1000, ""},
}
func TestTransportStatus_Stringer(t *testing.T) {
for _, tt := range transportStatusTests {
if tt.transportStatus.String() != tt.expected {
t.Errorf("transportStatus.String() = %s; want = %s",
tt.transportStatus.String(), tt.expected)
}
}
}
func populateCargoReceivedInStockholm() *Cargo {
c := NewCargo("XYZ", RouteSpecification{
Origin: SESTO,
Destination: AUMEL,
})
e := HandlingEvent{
TrackingID: c.TrackingID,
Activity: HandlingActivity{
Type: Receive,
Location: SESTO,
},
}
hh := HandlingHistory{
HandlingEvents: []HandlingEvent{e},
}
c.DeriveDeliveryProgress(hh)
return c
}
func populateCargoClaimedInMelbourne() *Cargo {
c := NewCargo("XYZ", RouteSpecification{
Origin: SESTO,
Destination: AUMEL,
})
e := HandlingEvent{
TrackingID: c.TrackingID,
Activity: HandlingActivity{
Type: Claim,
Location: AUMEL,
},
}
hh := HandlingHistory{
HandlingEvents: []HandlingEvent{e},
}
c.DeriveDeliveryProgress(hh)
return c
}