716265a5dacd8f5a97a2828f3aa1292d7e7a51a8.svn-base 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package handling
  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. type stubEventHandler struct {
  9. events []interface{}
  10. }
  11. func (h *stubEventHandler) CargoWasHandled(e shipping.HandlingEvent) {
  12. h.events = append(h.events, e)
  13. }
  14. func TestRegisterHandlingEvent(t *testing.T) {
  15. var cargos mock.CargoRepository
  16. cargos.StoreFn = func(c *shipping.Cargo) error {
  17. return nil
  18. }
  19. cargos.FindFn = func(id shipping.TrackingID) (*shipping.Cargo, error) {
  20. if id == "no_such_id" {
  21. return nil, shipping.ErrUnknownCargo
  22. }
  23. return new(shipping.Cargo), nil
  24. }
  25. var voyages mock.VoyageRepository
  26. voyages.FindFn = func(n shipping.VoyageNumber) (*shipping.Voyage, error) {
  27. return new(shipping.Voyage), nil
  28. }
  29. var locations mock.LocationRepository
  30. locations.FindFn = func(l shipping.UNLocode) (*shipping.Location, error) {
  31. return nil, nil
  32. }
  33. var events mock.HandlingEventRepository
  34. events.StoreFn = func(e shipping.HandlingEvent) {}
  35. eh := &stubEventHandler{events: make([]interface{}, 0)}
  36. ef := shipping.HandlingEventFactory{
  37. CargoRepository: &cargos,
  38. VoyageRepository: &voyages,
  39. LocationRepository: &locations,
  40. }
  41. s := NewService(&events, ef, eh)
  42. var (
  43. completed = time.Date(2015, time.November, 10, 23, 0, 0, 0, time.UTC)
  44. id = shipping.TrackingID("ABC123")
  45. voyage = shipping.VoyageNumber("V100")
  46. )
  47. var err error
  48. err = cargos.Store(shipping.NewCargo(id, shipping.RouteSpecification{}))
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. err = s.RegisterHandlingEvent(completed, id, voyage, shipping.SESTO, shipping.Load)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. err = s.RegisterHandlingEvent(completed, "no_such_id", voyage, shipping.SESTO, shipping.Load)
  57. if err != shipping.ErrUnknownCargo {
  58. t.Errorf("err = %s; want = %s", err, shipping.ErrUnknownCargo)
  59. }
  60. if len(eh.events) != 1 {
  61. t.Errorf("len(eh.events) = %d; want = %d", len(eh.events), 1)
  62. }
  63. }