-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfield_access_test.go
More file actions
139 lines (129 loc) · 3.31 KB
/
Copy pathfield_access_test.go
File metadata and controls
139 lines (129 loc) · 3.31 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package protopath
import (
"context"
"reflect"
"testing"
upiV1 "github.com/caraml-dev/universal-prediction-interface/gen/go/grpc/caraml/upi/v1"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"
)
func TestFieldAccessOperation_Lookup(t *testing.T) {
obj := &upiV1.PredictValuesRequest{
TargetName: "target_name",
Metadata: &upiV1.RequestMetadata{
PredictionId: "predictionID",
},
PredictionRows: []*upiV1.PredictionRow{
{
RowId: "id-1",
ModelInputs: []*upiV1.NamedValue{
{
Name: "rating",
Type: upiV1.NamedValue_TYPE_DOUBLE,
DoubleValue: float64(4.2),
},
{
Name: "acceptance_rate",
Type: upiV1.NamedValue_TYPE_DOUBLE,
DoubleValue: float64(0.8),
},
},
},
},
}
tests := []struct {
name string
obj any
rootObj any
fieldAccessOp *FieldAccessOperation
want any
expectedErr error
}{
{
name: "simple field access",
obj: obj,
rootObj: obj,
fieldAccessOp: &FieldAccessOperation{
fieldName: "target_name",
fieldGetter: &ProtoFieldGetter{},
fromRoot: true,
},
want: "target_name",
},
{
name: "simple field access returning object",
obj: obj,
rootObj: obj,
fieldAccessOp: &FieldAccessOperation{
fieldName: "metadata",
fieldGetter: &ProtoFieldGetter{},
fromRoot: true,
},
want: &upiV1.RequestMetadata{
PredictionId: "predictionID",
},
},
{
name: "simple field access returning array of object",
obj: obj,
rootObj: obj,
fieldAccessOp: &FieldAccessOperation{
fieldName: "prediction_rows",
fieldGetter: &ProtoFieldGetter{},
fromRoot: true,
},
want: []*upiV1.PredictionRow{
{
RowId: "id-1",
ModelInputs: []*upiV1.NamedValue{
{
Name: "rating",
Type: upiV1.NamedValue_TYPE_DOUBLE,
DoubleValue: float64(4.2),
},
{
Name: "acceptance_rate",
Type: upiV1.NamedValue_TYPE_DOUBLE,
DoubleValue: float64(0.8),
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.fieldAccessOp.Lookup(context.Background(), tt.obj, tt.rootObj)
assert.Equal(t, tt.expectedErr, err)
if tt.expectedErr != nil {
return
}
assertValueEquality(t, tt.want, got)
})
}
}
func assertValueEquality(t *testing.T, expected, actual any) {
wantReflectVal := reflect.ValueOf(expected)
switch wantReflectVal.Kind() {
case reflect.Slice:
gotReflectVal := reflect.ValueOf(actual)
if gotReflectVal.Len() != wantReflectVal.Len() {
t.Errorf("FieldAccessOperation.Lookup() having different length between actual: %d and expected: %d", gotReflectVal.Len(), wantReflectVal.Len())
return
}
for i := 0; i < wantReflectVal.Len(); i++ {
wVal := wantReflectVal.Index(i).Interface()
assertValueEquality(t, wVal, gotReflectVal.Index(i).Interface())
}
default:
if expectedVal, isProto := expected.(proto.Message); isProto {
if !proto.Equal(expectedVal, actual.(proto.Message)) {
t.Errorf("FieldAccessOperation.Lookup() = %v, want %v", actual, expected)
}
return
}
if !reflect.DeepEqual(expected, actual) {
t.Errorf("FieldAccessOperation.Lookup() = %v, want %v", actual, expected)
}
}
}