|
| 1 | +package feast |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/feast-dev/feast/sdk/go/mocks" |
| 8 | + "github.com/feast-dev/feast/sdk/go/protos/feast/serving" |
| 9 | + "github.com/feast-dev/feast/sdk/go/protos/feast/types" |
| 10 | + "github.com/golang/mock/gomock" |
| 11 | + "github.com/google/go-cmp/cmp" |
| 12 | + "github.com/opentracing/opentracing-go" |
| 13 | +) |
| 14 | + |
| 15 | +func TestGetOnlineFeatures(t *testing.T) { |
| 16 | + tt := []struct { |
| 17 | + name string |
| 18 | + req OnlineFeaturesRequest |
| 19 | + recieve OnlineFeaturesResponse |
| 20 | + want OnlineFeaturesResponse |
| 21 | + wantErr bool |
| 22 | + err error |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "Valid client Get Online Features call", |
| 26 | + req: OnlineFeaturesRequest{ |
| 27 | + Features: []string{ |
| 28 | + "driver:rating", |
| 29 | + "rating", |
| 30 | + }, |
| 31 | + Entities: []Row{ |
| 32 | + {"driver_id": Int64Val(1)}, |
| 33 | + }, |
| 34 | + Project: "driver_project", |
| 35 | + }, |
| 36 | + // check GetOnlineFeatures() should strip projects returned from serving |
| 37 | + recieve: OnlineFeaturesResponse{ |
| 38 | + RawResponse: &serving.GetOnlineFeaturesResponse{ |
| 39 | + FieldValues: []*serving.GetOnlineFeaturesResponse_FieldValues{ |
| 40 | + { |
| 41 | + Fields: map[string]*types.Value{ |
| 42 | + "driver_project/driver:rating": Int64Val(1), |
| 43 | + "driver_project/rating": Int64Val(1), |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + want: OnlineFeaturesResponse{ |
| 50 | + RawResponse: &serving.GetOnlineFeaturesResponse{ |
| 51 | + FieldValues: []*serving.GetOnlineFeaturesResponse_FieldValues{ |
| 52 | + { |
| 53 | + Fields: map[string]*types.Value{ |
| 54 | + "driver:rating": Int64Val(1), |
| 55 | + "rating": Int64Val(1), |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + for _, tc := range tt { |
| 65 | + t.Run(tc.name, func(t *testing.T) { |
| 66 | + // mock feast grpc client get online feature requestss |
| 67 | + ctrl := gomock.NewController(t) |
| 68 | + defer ctrl.Finish() |
| 69 | + cli := mock_serving.NewMockServingServiceClient(ctrl) |
| 70 | + ctx := context.Background() |
| 71 | + _, traceCtx := opentracing.StartSpanFromContext(ctx, "get_online_features") |
| 72 | + rawRequest, _ := tc.req.buildRequest() |
| 73 | + resp := tc.recieve.RawResponse |
| 74 | + cli.EXPECT().GetOnlineFeatures(traceCtx, rawRequest).Return(resp, nil).Times(1) |
| 75 | + |
| 76 | + client := &GrpcClient{ |
| 77 | + cli: cli, |
| 78 | + } |
| 79 | + got, err := client.GetOnlineFeatures(ctx, &tc.req) |
| 80 | + |
| 81 | + if err != nil && !tc.wantErr { |
| 82 | + t.Errorf("error = %v, wantErr %v", err, tc.wantErr) |
| 83 | + return |
| 84 | + } |
| 85 | + if tc.wantErr && err.Error() != tc.err.Error() { |
| 86 | + t.Errorf("error = %v, expected err = %v", err, tc.err) |
| 87 | + return |
| 88 | + } |
| 89 | + // TODO: compare directly once OnlineFeaturesResponse no longer embeds a rawResponse. |
| 90 | + if !cmp.Equal(got.RawResponse.String(), tc.want.RawResponse.String()) { |
| 91 | + t.Errorf("got: \n%v\nwant:\n%v", got.RawResponse.String(), tc.want.RawResponse.String()) |
| 92 | + } |
| 93 | + }) |
| 94 | + } |
| 95 | +} |
0 commit comments