forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_grpc_server_write_protos.py
More file actions
159 lines (134 loc) · 5.14 KB
/
Copy pathtest_grpc_server_write_protos.py
File metadata and controls
159 lines (134 loc) · 5.14 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import pytest
from feast.infra.contrib.grpc_server import parse_typed
from feast.protos.feast.serving.GrpcServer_pb2 import (
PushRequest,
WriteToOnlineStoreRequest,
)
from feast.protos.feast.types.Value_pb2 import (
Int64List,
Map,
Null,
StringSet,
Value,
)
def test_push_request_string_features():
request = PushRequest(
features={"driver_id": "1001", "conv_rate": "0.5"},
stream_feature_view="driver_stats",
to="online",
)
assert request.features["driver_id"] == "1001"
assert request.features["conv_rate"] == "0.5"
assert len(request.typed_features) == 0
def test_push_request_typed_features():
request = PushRequest(
typed_features={
"driver_id": Value(int64_val=1001),
"conv_rate": Value(float_val=0.5),
"active": Value(bool_val=True),
"label": Value(string_val="fast"),
},
stream_feature_view="driver_stats",
to="online",
)
assert request.typed_features["driver_id"].int64_val == 1001
assert request.typed_features["conv_rate"].float_val == pytest.approx(0.5)
assert request.typed_features["active"].bool_val is True
assert request.typed_features["label"].string_val == "fast"
assert len(request.features) == 0
def test_push_request_typed_features_val_case():
"""WhichOneof('val') returns the correct field name for each value type."""
cases = [
(Value(int32_val=1), "int32_val"),
(Value(int64_val=2), "int64_val"),
(Value(float_val=1.0), "float_val"),
(Value(double_val=2.0), "double_val"),
(Value(bool_val=True), "bool_val"),
(Value(string_val="x"), "string_val"),
]
for value, expected_case in cases:
assert value.WhichOneof("val") == expected_case
def test_write_to_online_store_request_string_features():
request = WriteToOnlineStoreRequest(
features={"driver_id": "1001", "avg_daily_trips": "10"},
feature_view_name="driver_hourly_stats",
)
assert request.features["driver_id"] == "1001"
assert request.features["avg_daily_trips"] == "10"
assert len(request.typed_features) == 0
def test_write_to_online_store_request_typed_features():
request = WriteToOnlineStoreRequest(
typed_features={
"driver_id": Value(int64_val=1001),
"avg_daily_trips": Value(int32_val=10),
"conv_rate": Value(float_val=0.42),
},
feature_view_name="driver_hourly_stats",
)
assert request.typed_features["driver_id"].int64_val == 1001
assert request.typed_features["avg_daily_trips"].int32_val == 10
assert request.typed_features["conv_rate"].float_val == pytest.approx(0.42)
assert len(request.features) == 0
def test_push_request_string_and_typed_features_are_independent():
"""Setting features does not affect typed_features and vice versa."""
r1 = PushRequest(
features={"driver_id": "1001"}, stream_feature_view="s", to="online"
)
r2 = PushRequest(
typed_features={"driver_id": Value(int64_val=1001)},
stream_feature_view="s",
to="online",
)
assert len(r1.typed_features) == 0
assert len(r2.features) == 0
def test_write_to_online_store_string_and_typed_features_are_independent():
r1 = WriteToOnlineStoreRequest(
features={"driver_id": "1001"}, feature_view_name="fv"
)
r2 = WriteToOnlineStoreRequest(
typed_features={"driver_id": Value(int64_val=1001)}, feature_view_name="fv"
)
assert len(r1.typed_features) == 0
assert len(r2.features) == 0
def test_parse_typed_null_val_becomes_none():
"""Value(null_val=NULL) must produce None in the DataFrame, not the integer 0."""
df = parse_typed(
{
"present": Value(int64_val=42),
"missing": Value(null_val=Null.NULL),
}
)
assert df["present"].iloc[0] == 42
assert df["missing"].iloc[0] is None
def test_parse_typed_unset_val_becomes_none():
"""A Value with no oneof field set (WhichOneof returns None) must also produce None."""
df = parse_typed({"empty": Value()})
assert df["empty"].iloc[0] is None
def test_parse_typed_list_val_unwrapped_to_python_list():
"""Compound list values are unwrapped from their protobuf wrapper to a plain list."""
df = parse_typed(
{
"ids": Value(int64_list_val=Int64List(val=[1, 2, 3])),
}
)
assert df["ids"].iloc[0] == [1, 2, 3]
def test_parse_typed_set_val_unwrapped_to_python_list():
"""Compound set values are unwrapped from their protobuf wrapper to a plain list."""
df = parse_typed(
{
"tags": Value(string_set_val=StringSet(val=["a", "b"])),
}
)
assert sorted(df["tags"].iloc[0]) == ["a", "b"]
def test_parse_typed_map_val_unwrapped_to_python_dict():
"""Map values are unwrapped from their protobuf Map wrapper to a plain dict."""
df = parse_typed(
{
"scores": Value(
map_val=Map(val={"x": Value(float_val=1.0), "y": Value(float_val=2.0)})
),
}
)
result = df["scores"].iloc[0]
assert isinstance(result, dict)
assert set(result.keys()) == {"x", "y"}