cargo_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package model
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestConstruction(t *testing.T) {
  7. id := NextTrackingID()
  8. spec := RouteSpecification{
  9. Origin: SESTO,
  10. Destination: AUMEL,
  11. ArrivalDeadline: time.Date(2009, time.March, 13, 0, 0, 0, 0, time.UTC),
  12. }
  13. c := NewCargo(id, spec)
  14. if c.Delivery.RoutingStatus != NotRouted {
  15. t.Errorf("RoutingStatus = %v; want = %v",
  16. c.Delivery.RoutingStatus, NotRouted)
  17. }
  18. if c.Delivery.TransportStatus != NotReceived {
  19. t.Errorf("TransportStatus = %v; want = %v",
  20. c.Delivery.TransportStatus, NotReceived)
  21. }
  22. if c.Delivery.LastKnownLocation != "" {
  23. t.Errorf("LastKnownLocation = %s; want = %s",
  24. c.Delivery.LastKnownLocation, "")
  25. }
  26. }
  27. func TestRoutingStatus(t *testing.T) {
  28. good := Itinerary{
  29. Legs: []Leg{
  30. {LoadLocation: SESTO, UnloadLocation: AUMEL},
  31. },
  32. }
  33. bad := Itinerary{
  34. Legs: []Leg{
  35. {LoadLocation: SESTO, UnloadLocation: CNHKG},
  36. },
  37. }
  38. acceptOnlyGood := RouteSpecification{
  39. Origin: SESTO,
  40. Destination: AUMEL,
  41. }
  42. c := NewCargo("ABC", RouteSpecification{})
  43. c.SpecifyNewRoute(acceptOnlyGood)
  44. if c.Delivery.RoutingStatus != NotRouted {
  45. t.Errorf("RoutingStatus = %v; want = %v",
  46. c.Delivery.RoutingStatus, NotRouted)
  47. }
  48. c.AssignToRoute(bad)
  49. if c.Delivery.RoutingStatus != Misrouted {
  50. t.Errorf("RoutingStatus = %v; want = %v",
  51. c.Delivery.RoutingStatus, Misrouted)
  52. }
  53. c.AssignToRoute(good)
  54. if c.Delivery.RoutingStatus != Routed {
  55. t.Errorf("RoutingStatus = %v; want = %v",
  56. c.Delivery.RoutingStatus, Routed)
  57. }
  58. }
  59. func TestLastKnownLocation_WhenNoEvents(t *testing.T) {
  60. c := NewCargo("ABC", RouteSpecification{
  61. Origin: SESTO,
  62. Destination: CNHKG,
  63. })
  64. if c.Delivery.LastKnownLocation != "" {
  65. t.Errorf("should be equal")
  66. }
  67. }
  68. func TestLastKnownLocation_WhenReceived(t *testing.T) {
  69. c := populateCargoReceivedInStockholm()
  70. if c.Delivery.LastKnownLocation != SESTO {
  71. t.Errorf("LastKnownLocation = %s; want = %s",
  72. c.Delivery.LastKnownLocation, SESTO)
  73. }
  74. }
  75. func TestLastKnownLocation_WhenClaimed(t *testing.T) {
  76. c := populateCargoClaimedInMelbourne()
  77. if c.Delivery.LastKnownLocation != AUMEL {
  78. t.Errorf("LastKnownLocation = %s; want = %s",
  79. c.Delivery.LastKnownLocation, AUMEL)
  80. }
  81. }
  82. var routingStatusTests = []struct {
  83. routingStatus RoutingStatus
  84. expected string
  85. }{
  86. {NotRouted, "Not routed"},
  87. {Misrouted, "Misrouted"},
  88. {Routed, "Routed"},
  89. {1000, ""},
  90. }
  91. func TestRoutingStatus_Stringer(t *testing.T) {
  92. for _, tt := range routingStatusTests {
  93. if tt.routingStatus.String() != tt.expected {
  94. t.Errorf("routingStatus.String() = %s; want = %s",
  95. tt.routingStatus.String(), tt.expected)
  96. }
  97. }
  98. }
  99. var transportStatusTests = []struct {
  100. transportStatus TransportStatus
  101. expected string
  102. }{
  103. {NotReceived, "Not received"},
  104. {InPort, "In port"},
  105. {OnboardCarrier, "Onboard carrier"},
  106. {Claimed, "Claimed"},
  107. {Unknown, "Unknown"},
  108. {1000, ""},
  109. }
  110. func TestTransportStatus_Stringer(t *testing.T) {
  111. for _, tt := range transportStatusTests {
  112. if tt.transportStatus.String() != tt.expected {
  113. t.Errorf("transportStatus.String() = %s; want = %s",
  114. tt.transportStatus.String(), tt.expected)
  115. }
  116. }
  117. }
  118. func populateCargoReceivedInStockholm() *Cargo {
  119. c := NewCargo("XYZ", RouteSpecification{
  120. Origin: SESTO,
  121. Destination: AUMEL,
  122. })
  123. e := HandlingEvent{
  124. TrackingID: c.TrackingID,
  125. Activity: HandlingActivity{
  126. Type: Receive,
  127. Location: SESTO,
  128. },
  129. }
  130. hh := HandlingHistory{
  131. HandlingEvents: []HandlingEvent{e},
  132. }
  133. c.DeriveDeliveryProgress(hh)
  134. return c
  135. }
  136. func populateCargoClaimedInMelbourne() *Cargo {
  137. c := NewCargo("XYZ", RouteSpecification{
  138. Origin: SESTO,
  139. Destination: AUMEL,
  140. })
  141. e := HandlingEvent{
  142. TrackingID: c.TrackingID,
  143. Activity: HandlingActivity{
  144. Type: Claim,
  145. Location: AUMEL,
  146. },
  147. }
  148. hh := HandlingHistory{
  149. HandlingEvents: []HandlingEvent{e},
  150. }
  151. c.DeriveDeliveryProgress(hh)
  152. return c
  153. }