Go Rental Service Trip Creation Test: Success and Failure Scenarios
{"title":"func TestCreateTrip(t *testing.T) {\n\t\tc := auth.ContextWithAccountID(context.Background(), id.AccountID("account1"))\n\t\tmc, err := mongotesting.NewClient(c)\n\t\tif err != nil {\n\t\t\tt.Fatalf("cannot create mongo client: %v", err)\n\t\t}\n\n\t\tlogger, err := zap.NewDevelopment()\n\t\tif err != nil {\n\t\t\tt.Fatalf("cannot create mongo client : %v", err)\n\t\t}\n\n\t\tpm := &profileManager{}\n\t\tcm := &carManager{}\n\t\ts := &Service{\n\t\t\tProfileManager: pm,\n\t\t\tCarManager: cm,\n\t\t\tPOIManager: &poi.Manager{},\n\t\t\tMongo: dao.NewMongo(mc.Database("rentCar")),\n\t\t\tLogger: logger,\n\t\t}\n\n\t\treq := &rentalpb.CreateTripRequest{\n\t\t\tCarId: "car1",\n\t\t\tStart: &rentalpb.Location{\n\t\t\t\tLatitude: 32.123,\n\t\t\t\tLongitude: 114.2525,\n\t\t\t\t},\n\t\t}\n\t\tpm.iID = "identity1"\n\n\t\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\n\t\tcases := []struct {\n\t\t\tname string\n\t\t\ttripID string\n\t\t\tprofileErr error\n\t\t\tcarVerifyErr error\n\t\t\tcarUnlockErr error\n\t\t\twant string\n\t\t\twantErr bool\n\t\t}{\n\t\t\t{\n\t\t\t\tname: "normal_create",\n\t\t\t\ttripID: "648badb1b4600ccb093e5b29",\n\t\t\t\twant: golden,\n\t\t\t},\n\t\t\t{\n\t\t\t\tname: "profile_err",\n\t\t\t\ttripID: "648badb1b4600ccb093e5b21",\n\t\t\t\tprofileErr: fmt.Errorf("profile"),\n\t\t\t\twantErr: true,\n\t\t\t},\n\t\t\t{\n\t\t\t\tname: "car_verify_err",\n\t\t\t\ttripID: "648badb1b4600ccb093e5b22",\n\t\t\t\tprofileErr: fmt.Errorf("verify"),\n\t\t\t\twantErr: true,\n\t\t\t},\n\t\t\t{\n\t\t\t\tname: "car_unlock_err",\n\t\t\t\ttripID: "648badb1b4600ccb093e5b28",\n\t\t\t\tprofileErr: fmt.Errorf("unlock"),\n\t\t\t\twant: golden,\n\t\t\t},\n\t\t}\n\n\t\tfor _, cc := range cases {\n\t\t\tt.Run(cc.name, func(t *testing.T) {\n\t\t\t\tmgutil.NewObjIDWithValue(id.TripID(cc.tripID))\n\t\t\t\tpm.err = cc.profileErr\n\t\t\t\tcm.unlockErr = cc.carUnlockErr\n\t\t\t\tcm.verifyErr = cc.carVerifyErr\n\t\t\t\tres, err := s.CreateTrip(c, req)\n\t\t\t\tif cc.wantErr {\n\t\t\t\t\tif err == nil {\n\t\t\t\t\t\tt.Errorf("want err ,got none")\n\t\t\t\t\t} else {\n\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif err != nil {\n\t\t\t\t\tt.Errorf("error create trip: %v", err)\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tif res.Id != cc.tripID {\n\t\t\t\t\tt.Errorf("incorrect id; want:%q,got:%q", cc.tripID, res.Id)\n\t\t\t\t}\n\n\t\t\t\tb, err := json.Marshal(res.Trip)\n\t\t\t\tif err != nil {\n\t\t\t\t\tt.Errorf("cannot marshal response: %v", err)\n\t\t\t\t}\n\n\t\t\t\tgot := string(b)\n\t\t\t\tif cc.want != got {\n\t\t\t\t\tt.Errorf("incorrect response want:%s got:%s", cc.want, got)\n\t\t\t\t}\n\t\t\t})\n\t\t}\n}\n\n\ntype profileManager struct {\n\t\tiID id.IdentityID\n\t\terr error\n}\n\n\tfunc (p *profileManager) Verify(context.Context, id.AccountID) (id.IdentityID, error) {\n\t\treturn p.iID, p.err\n\t}\n\n\ntype carManager struct {\n\t\tverifyErr error\n\t\tunlockErr error\n\t}\n\n\tfunc (c *carManager) Verify(context.Context, id.CarID, *rentalpb.Location) error {\n\t\treturn c.verifyErr\n\t}\n\n\tfunc (c *carManager) Unlock(context.Context, id.CarID) error {\n\t\treturn c.unlockErr\n\t}\n\n\tfunc TestMain(m *testing.M) {\n\t\tos.Exit(mongotesting.RunWithMongoInDocker(m))\n\t}
原文地址: https://www.cveoy.top/t/topic/pviF 著作权归作者所有。请勿转载和采集!