租车服务 - 创建行程测试
func TestCreateTrip(t *testing.T) { \n\t// 创建带有账户ID的上下文 \n\tc := auth.ContextWithAccountID(context.Background(), id.AccountID("account1")) \n\t// 创建MongoDB客户端 \n\tmc, err := mongotesting.NewClient(c) \n\tif err != nil { \n\t t.Fatalf("cannot create mongo client: %v", err) \n\t} \n\t// 创建日志记录器 \n\tlogger, err := zap.NewDevelopment() \n\tif err != nil { \n\t t.Fatalf("cannot create mongo client : %v", err) \n\t} \n\t// 创建Profile Manager \n\tpm := &profileManager{} \n\t// 创建Car Manager \n\tcm := &carManager{} \n\t// 创建Service \n\ts := &Service{ \n\t ProfileManager: pm, \n\t CarManager: cm, \n\t POIManager: &poi.Manager{}, \n\t // 创建Mongo数据库 \n\t Mongo: dao.NewMongo(mc.Database("rentCar")), \n\t Logger: logger, \n\t} \n\t// 创建行程请求 \n\treq := &rentalpb.CreateTripRequest{ \n\t CarId: "car1", \n\t Start: &rentalpb.Location{ \n\t Latitude: 32.123, \n\t Longitude: 114.2525, \n\t }, \n\t} \n\t// 设置身份ID \n\tpm.iID = "identity1" \n\t// 预期结果 \n\tgolden := {"account_id":"account1","car_id":"car1","start":{"Location":{"latitude":32.123,"longitude":114.2525},"poi_name":"天安门"},"current":{"Location":{"latitude":32.123,"longitude":114.2525},"poi_name":"天安门"},"status":1,"identity_id":"identity1"} \n\t// 测试用例 \n\tcases := []struct { \n\t name string \n\t tripID string \n\t profileErr error \n\t carVerifyErr error \n\t carUnlockErr error \n\t want string \n\t wantErr bool \n\t}{ \n\t { \n\t name: "normal_create", \n\t tripID: "648badb1b4600ccb093e5b29", \n\t want: golden, \n\t }, \n\t { \n\t name: "profile_err", \n\t tripID: "648badb1b4600ccb093e5b21", \n\t profileErr: fmt.Errorf("profile"), \n\t wantErr: true, \n\t }, \n\t { \n\t name: "car_verify_err", \n\t tripID: "648badb1b4600ccb093e5b22", \n\t profileErr: fmt.Errorf("verify"), \n\t wantErr: true, \n\t }, \n\t { \n\t name: "car_unlock_err", \n\t tripID: "648badb1b4600ccb093e5b28", \n\t want: golden, \n\t }, \n\t} \n\t// 循环执行测试用例 \n\tfor _, cc := range cases { \n\t t.Run(cc.name, func(t *testing.T) { \n\t // 使用指定的TripID创建一个新的ObjID \n\t mgutil.NewObjIDWithValue(id.TripID(cc.tripID)) \n\t // 设置Profile Manager的错误 \n\t pm.err = cc.profileErr \n\t // 设置Car Manager的解锁错误 \n\t cm.unlockErr = cc.carUnlockErr \n\t // 设置Car Manager的验证错误 \n\t cm.verifyErr = cc.carVerifyErr \n\t // 创建Trip \n\t res, err := s.CreateTrip(c, req) \n\t // 检查是否出现预期错误 \n\t if cc.wantErr { \n\t if err == nil { \n\t t.Errorf("want err ,got none") \n\t } else { \n\t return \n\t } \n\t } \n\t // 检查创建Trip是否出现错误 \n\t if err != nil { \n\t t.Errorf("error create trip: %v", err) \n\t return \n\t } \n\t // 检查Trip ID是否正确 \n\t if res.Id != cc.tripID { \n\t t.Errorf("incorrect id; want:%q,got:%q", cc.tripID, res.Id) \n\t } \n\t // 将响应转换为JSON字符串 \n\t b, err := json.Marshal(res.Trip) \n\t if err != nil { \n\t t.Errorf("cannot marshal response: %v", err) \n\t } \n\t // 检查响应是否正确 \n\t got := string(b) \n\t if cc.want != got { \n\t t.Errorf("incorrect response want:%s got:%s", cc.want, got) \n\t } \n\t}) \n\t} \n}
原文地址: https://www.cveoy.top/t/topic/pvpO 著作权归作者所有。请勿转载和采集!