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
| func TestTripLifecycle(t *testing.T) { c := auth.ContestWithAccontId(context.Background(), id.AccountID("account_id_lifecycle")) s := newService(c, t, &profileManager{}, &carManager{}) tid := id.TripID("621c60186800fc9e2ca14801") mgo.NewObjIDWithValue(tid) cases := []struct { name string now int64 op func() (*rentalpb.Trip, error) want string }{ { name: "create_trip", now: 10000, op: func() (*rentalpb.Trip, error) { e, err := s.CreateTrip(c, &rentalpb.CreateTripRequest{ Start: &rentalpb.Location{ Latitude: 34, Longitude: 122, }, CarId: "car123", }) if err != nil { return nil, err } return e.Trip, nil }, want: `{"account_id":"account_id_lifecycle","car_id":"car123","start":{"location":{"latitude":34,"longitude":122},"poi_name":"中关村","timestamp_sec":10000},"current":{"location":{"latitude":34,"longitude":122},"poi_name":"中关村","timestamp_sec":10000},"status":1}`, }, { name: "update_trip", now: 20000, op: func() (*rentalpb.Trip, error) { return s.UpdateTrip(c, &rentalpb.UpdateTripRequest{ Id: tid.String(), Current: &rentalpb.Location{ Latitude: 28.2, Longitude: 122, }, }) }, want: `{"account_id":"account_id_lifecycle","car_id":"car123","start":{"location":{"latitude":34,"longitude":122},"poi_name":"中关村","timestamp_sec":10000},"current":{"location":{"latitude":28.2,"longitude":122},"fee_cent":9116,"km_driven":359.56460921309167,"poi_name":"天安门","timestamp_sec":20000},"status":1}`, }, { name: "finish_trip", now: 30000, op: func() (*rentalpb.Trip, error) { return s.UpdateTrip(c, &rentalpb.UpdateTripRequest{ Id: tid.String(), EndTrip: true, }) }, want: `{"account_id":"account_id_lifecycle","car_id":"car123","start":{"location":{"latitude":34,"longitude":122},"poi_name":"中关村","timestamp_sec":10000},"current":{"location":{"latitude":28.2,"longitude":122},"fee_cent":17638,"km_driven":480.5067823692563,"poi_name":"天安门","timestamp_sec":30000},"end":{"location":{"latitude":28.2,"longitude":122},"fee_cent":17638,"km_driven":480.5067823692563,"poi_name":"天安门","timestamp_sec":30000},"status":2}`, }, { name: "query_trip", now: 40000, op: func() (*rentalpb.Trip, error) { return s.GetTrip(c, &rentalpb.GetTripRequest{ Id: tid.String(), }) }, want: `{"account_id":"account_id_lifecycle","car_id":"car123","start":{"location":{"latitude":34,"longitude":122},"poi_name":"中关村","timestamp_sec":10000},"current":{"location":{"latitude":28.2,"longitude":122},"fee_cent":17638,"km_driven":480.5067823692563,"poi_name":"天安门","timestamp_sec":30000},"end":{"location":{"latitude":28.2,"longitude":122},"fee_cent":17638,"km_driven":480.5067823692563,"poi_name":"天安门","timestamp_sec":30000},"status":2}`, }, } rand.Seed(1234) for _, cc := range cases { nowFunc = func() int64 { return cc.now } trip, err := cc.op() if err != nil { t.Errorf("%s: operation failed:%v", cc.name, err) continue } b, err := json.Marshal(trip) if err != nil { t.Errorf("%s:failed marshalling response: %v", cc.name, err) } got := string(b) if cc.want != got { t.Errorf("incorrect response:want %s , got %s", cc.want, got) } } } func newService(c context.Context, t *testing.T, pm ProfileManager, cm CarManager) *Service { mc, err := mongotesting.NewClient(c) if err != nil { t.Fatalf("cannot create mongo client: %v", err) } logger, err := zap.NewDevelopment() if err != nil { t.Fatalf("cannot create logger : %v", err) } db := mc.Database("coolcar") mongotesting.SetupIndex(c, db) return &Service{ ProfileManager: pm, CarManager: cm, POIManager: &poi.Manager{}, Mongo: dao.NewMongo(db), Logger: logger, } }
|