b0e6f8c1b6d3e7377005e1170e5d488306c22816.svn-base 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package booking
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/longjoy/micro-go-course/section19/cargo/mock"
  6. shipping "github.com/longjoy/micro-go-course/section19/cargo/model"
  7. )
  8. func TestBookNewCargo(t *testing.T) {
  9. var (
  10. origin = shipping.SESTO
  11. destination = shipping.AUMEL
  12. deadline = time.Date(2015, time.November, 10, 23, 0, 0, 0, time.UTC)
  13. )
  14. var cargos mockCargoRepository
  15. s := NewService(&cargos, nil, nil, nil)
  16. id, err := s.BookNewCargo(origin, destination, deadline)
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. c, err := cargos.Find(id)
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. if c.TrackingID != id {
  25. t.Errorf("c.TrackingID = %s; want = %s", c.TrackingID, id)
  26. }
  27. if c.Origin != origin {
  28. t.Errorf("c.Origin = %s; want = %s", c.Origin, origin)
  29. }
  30. if c.RouteSpecification.Destination != destination {
  31. t.Errorf("c.RouteSpecification.Destination = %s; want = %s",
  32. c.RouteSpecification.Destination, destination)
  33. }
  34. if c.RouteSpecification.ArrivalDeadline != deadline {
  35. t.Errorf("c.RouteSpecification.ArrivalDeadline = %s; want = %s",
  36. c.RouteSpecification.ArrivalDeadline, deadline)
  37. }
  38. }
  39. type stubRoutingService struct{}
  40. func (s *stubRoutingService) FetchRoutesForSpecification(rs shipping.RouteSpecification) []shipping.Itinerary {
  41. legs := []shipping.Leg{
  42. {LoadLocation: rs.Origin, UnloadLocation: rs.Destination},
  43. }
  44. return []shipping.Itinerary{
  45. {Legs: legs},
  46. }
  47. }
  48. func TestRequestPossibleRoutesForCargo(t *testing.T) {
  49. var (
  50. origin = shipping.SESTO
  51. destination = shipping.AUMEL
  52. deadline = time.Date(2015, time.November, 10, 23, 0, 0, 0, time.UTC)
  53. )
  54. var cargos mockCargoRepository
  55. var rs stubRoutingService
  56. s := NewService(&cargos, nil, nil, &rs)
  57. r := s.RequestPossibleRoutesForCargo("no_such_id")
  58. if len(r) != 0 {
  59. t.Errorf("len(r) = %d; want = %d", len(r), 0)
  60. }
  61. id, err := s.BookNewCargo(origin, destination, deadline)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. i := s.RequestPossibleRoutesForCargo(id)
  66. if len(i) != 1 {
  67. t.Errorf("len(i) = %d; want = %d", len(i), 1)
  68. }
  69. }
  70. func TestAssignCargoToRoute(t *testing.T) {
  71. var cargos mockCargoRepository
  72. var rs stubRoutingService
  73. s := NewService(&cargos, nil, nil, &rs)
  74. var (
  75. origin = shipping.SESTO
  76. destination = shipping.AUMEL
  77. deadline = time.Date(2015, time.November, 10, 23, 0, 0, 0, time.UTC)
  78. )
  79. id, err := s.BookNewCargo(origin, destination, deadline)
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. i := s.RequestPossibleRoutesForCargo(id)
  84. if len(i) != 1 {
  85. t.Errorf("len(i) = %d; want = %d", len(i), 1)
  86. }
  87. if err := s.AssignCargoToRoute(id, i[0]); err != nil {
  88. t.Fatal(err)
  89. }
  90. if err := s.AssignCargoToRoute("no_such_id", shipping.Itinerary{}); err != ErrInvalidArgument {
  91. t.Errorf("err = %s; want = %s", err, ErrInvalidArgument)
  92. }
  93. }
  94. func TestChangeCargoDestination(t *testing.T) {
  95. var cargos mockCargoRepository
  96. var locations mock.LocationRepository
  97. locations.FindFn = func(loc shipping.UNLocode) (*shipping.Location, error) {
  98. if loc != shipping.AUMEL {
  99. return nil, shipping.ErrUnknownLocation
  100. }
  101. return shipping.Melbourne, nil
  102. }
  103. var rs stubRoutingService
  104. s := NewService(&cargos, &locations, nil, &rs)
  105. c := shipping.NewCargo("ABC", shipping.RouteSpecification{
  106. Origin: shipping.SESTO,
  107. Destination: shipping.CNHKG,
  108. ArrivalDeadline: time.Date(2015, time.November, 10, 23, 0, 0, 0, time.UTC),
  109. })
  110. if err := s.ChangeDestination("no_such_id", shipping.SESTO); err != shipping.ErrUnknownCargo {
  111. t.Errorf("err = %s; want = %s", err, shipping.ErrUnknownCargo)
  112. }
  113. if err := cargos.Store(c); err != nil {
  114. t.Fatal(err)
  115. }
  116. if err := s.ChangeDestination(c.TrackingID, "no_such_unlocode"); err != shipping.ErrUnknownLocation {
  117. t.Errorf("err = %s; want = %s", err, shipping.ErrUnknownLocation)
  118. }
  119. if c.RouteSpecification.Destination != shipping.CNHKG {
  120. t.Errorf("c.RouteSpecification.Destination = %s; want = %s",
  121. c.RouteSpecification.Destination, shipping.CNHKG)
  122. }
  123. if err := s.ChangeDestination(c.TrackingID, shipping.AUMEL); err != nil {
  124. t.Fatal(err)
  125. }
  126. uc, err := cargos.Find(c.TrackingID)
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. if uc.RouteSpecification.Destination != shipping.AUMEL {
  131. t.Errorf("uc.RouteSpecification.Destination = %s; want = %s",
  132. uc.RouteSpecification.Destination, shipping.AUMEL)
  133. }
  134. }
  135. func TestLoadCargo(t *testing.T) {
  136. deadline := time.Date(2015, time.November, 10, 23, 0, 0, 0, time.UTC)
  137. var cargos mock.CargoRepository
  138. cargos.FindFn = func(id shipping.TrackingID) (*shipping.Cargo, error) {
  139. return &shipping.Cargo{
  140. TrackingID: "test_id",
  141. Origin: shipping.SESTO,
  142. RouteSpecification: shipping.RouteSpecification{
  143. Origin: shipping.SESTO,
  144. Destination: shipping.AUMEL,
  145. ArrivalDeadline: deadline,
  146. },
  147. }, nil
  148. }
  149. s := NewService(&cargos, nil, nil, nil)
  150. c, err := s.LoadCargo("test_id")
  151. if err != nil {
  152. t.Fatal(err)
  153. }
  154. if c.TrackingID != "test_id" {
  155. t.Errorf("c.TrackingID = %s; want = %s", c.TrackingID, "test_id")
  156. }
  157. if c.Origin != "SESTO" {
  158. t.Errorf("c.Origin = %s; want = %s", c.Origin, "SESTO")
  159. }
  160. if c.Destination != "AUMEL" {
  161. t.Errorf("c.Destination = %s; want = %s", c.Origin, "AUMEL")
  162. }
  163. if c.ArrivalDeadline != deadline {
  164. t.Errorf("c.ArrivalDeadline = %s; want = %s", c.ArrivalDeadline, deadline)
  165. }
  166. if c.Misrouted {
  167. t.Errorf("cargo should not be misrouted")
  168. }
  169. if c.Routed {
  170. t.Errorf("cargo should not have been routed")
  171. }
  172. if len(c.Legs) != 0 {
  173. t.Errorf("len(c.Legs) = %d; want = %d", len(c.Legs), 0)
  174. }
  175. }
  176. type mockCargoRepository struct {
  177. cargo *shipping.Cargo
  178. }
  179. func (r *mockCargoRepository) Store(c *shipping.Cargo) error {
  180. r.cargo = c
  181. return nil
  182. }
  183. func (r *mockCargoRepository) Find(id shipping.TrackingID) (*shipping.Cargo, error) {
  184. if r.cargo != nil {
  185. return r.cargo, nil
  186. }
  187. return nil, shipping.ErrUnknownCargo
  188. }
  189. func (r *mockCargoRepository) FindAll() []*shipping.Cargo {
  190. return []*shipping.Cargo{r.cargo}
  191. }