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
| func TestProfilePhotoLifecycle(t *testing.T) { c := auth.ContestWithAccontId(context.Background(), id.AccountID("account1")) s := newService(c, t) s.BlobClient = &blobClient{ idForCreate: "blob1", } cases := []struct { name string op func() (string, error) wantRUL string wantErrCode codes.Code }{ { name: "create_blob", op: func() (string, error) { r, err := s.CreateProfilePhoto(c, &rentalpb.CreateProfilePhotoRequest{}) if err != nil { return "", err } return r.UploadUrl, nil }, wantRUL: "upload_url for blob1", }, { name: "complete_photo_upload", op: func() (string, error) { _, err := s.CompleteProfilePhoto(c, &rentalpb.CompleteProfilePhotoRequest{}) return "", err }, }, { name: "get_photo_url", op: func() (string, error) { r, err := s.GetProfilePhoto(c, &rentalpb.GetProfilePhotoRequest{}) if err != nil { return "", err } return r.Url, nil }, wantRUL: "get_url for blob1", }, { name: "clear_photo", op: func() (string, error) { _, err := s.ClearProfilePhoto(c, &rentalpb.ClearProfilePhotoRequest{})
return "", err }, }, { name: "get_photo_after_clear", op: func() (string, error) { r, err := s.GetProfilePhoto(c, &rentalpb.GetProfilePhotoRequest{}) if err != nil { return "", err } return r.Url, nil }, wantErrCode: codes.NotFound, }, }
for _, cc := range cases { got, err := cc.op() code := codes.OK if err != nil { if s, ok := status.FromError(err); ok { code = s.Code() } else { t.Errorf("operation failed:%v", err) } } if code != cc.wantErrCode { t.Errorf("%s: want error %d, got %d", cc.name, cc.wantErrCode, code) } if got != cc.wantRUL { t.Errorf("%s: want %q, got %q", cc.name, cc.wantRUL, got) } } }
type blobClient struct { idForCreate string }
func (b *blobClient) CreateBlob(c context.Context, req *blobpb.CreateBlobRequest, opts ...grpc.CallOption) (*blobpb.CreateBlobResponse, error) { return &blobpb.CreateBlobResponse{ Id: b.idForCreate, UploadUrl: "upload_url for " + b.idForCreate, }, nil } func (b *blobClient) GetBlob(c context.Context, req *blobpb.GetBlobRequest, opts ...grpc.CallOption) (*blobpb.GetBlobResponse, error) { return &blobpb.GetBlobResponse{}, nil } func (b *blobClient) GetBlobURL(c context.Context, req *blobpb.GetBlobURlRequest, opts ...grpc.CallOption) (*blobpb.GetBlobURLResponse, error) { return &blobpb.GetBlobURLResponse{ Url: "get_url for " + req.Id, }, nil }
|