Repository navigation
Expand file tree
/
Copy pathtest_feature_service.py
More file actions
234 lines (194 loc) · 7.47 KB
/
Copy pathtest_feature_service.py
File metadata and controls
234 lines (194 loc) · 7.47 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from feast.errors import FeastObjectNotFoundException
from feast.feature_service import FeatureService
from feast.feature_view import FeatureView
from feast.feature_view_projection import FeatureViewProjection
from feast.field import Field
from feast.infra.offline_stores.file_source import FileSource
from feast.protos.feast.core.Feature_pb2 import FeatureSpecV2
from feast.protos.feast.core.FeatureViewProjection_pb2 import (
FeatureViewProjection as FeatureViewProjectionProto,
)
from feast.types import Float32, String
from tests.utils.test_wrappers import no_warnings
class _MockRegistry:
def __init__(self, feature_view: FeatureView):
self._feature_view = feature_view
def get_any_feature_view(self, name, project, allow_cache=False):
if name != self._feature_view.name:
raise FeastObjectNotFoundException(name)
return self._feature_view
def _build_feature_view():
file_source = FileSource(name="my-file-source", path="test.parquet")
return FeatureView(
name="my-feature-view",
entities=[],
schema=[
Field(name="feature1", dtype=Float32),
Field(name="feature2", dtype=String),
],
source=file_source,
)
def test_prepare_for_apply_whole_view():
feature_view = _build_feature_view()
feature_service = FeatureService.from_proto(
FeatureService(
name="my-feature-service",
features=[],
).to_proto()
)
feature_service.feature_view_projections = [
FeatureViewProjection(
name=feature_view.name,
name_alias=None,
desired_features=[],
features=[],
)
]
feature_service.prepare_for_apply(_MockRegistry(feature_view), "test_project")
assert len(feature_service.feature_view_projections) == 1
resolved = feature_service.feature_view_projections[0]
assert [feature.name for feature in resolved.features] == [
"feature1",
"feature2",
]
assert resolved.features[0].dtype == Float32
assert resolved.features[1].dtype == String
def test_prepare_for_apply_feature_subset():
feature_view = _build_feature_view()
feature_service = FeatureService(
name="my-feature-service",
features=[],
)
feature_service.feature_view_projections = [
FeatureViewProjection(
name=feature_view.name,
name_alias=None,
desired_features=["feature2"],
features=[],
)
]
feature_service.prepare_for_apply(_MockRegistry(feature_view), "test_project")
resolved = feature_service.feature_view_projections[0]
assert len(resolved.features) == 1
assert resolved.features[0].name == "feature2"
assert resolved.features[0].dtype == String
def test_prepare_for_apply_name_only_columns():
feature_view = _build_feature_view()
projection_proto = FeatureViewProjectionProto(
feature_view_name=feature_view.name,
)
projection_proto.feature_columns.append(FeatureSpecV2(name="feature2"))
feature_service = FeatureService.from_proto(
FeatureService(name="my-feature-service", features=[]).to_proto()
)
feature_service.feature_view_projections = [
FeatureViewProjection.from_proto(projection_proto)
]
feature_service.prepare_for_apply(_MockRegistry(feature_view), "test_project")
resolved = feature_service.feature_view_projections[0]
assert len(resolved.features) == 1
assert resolved.features[0].name == "feature2"
assert resolved.features[0].dtype == String
def test_prepare_for_apply_keeps_complete_projection():
feature_view = _build_feature_view()
feature_service = FeatureService(
name="my-feature-service",
features=[feature_view[["feature1"]]],
)
original_projection = feature_service.feature_view_projections[0]
feature_service.prepare_for_apply(_MockRegistry(feature_view), "test_project")
assert feature_service.feature_view_projections[0] == original_projection
def test_feature_view_projections_have_independent_join_key_maps():
first_projection = FeatureViewProjection(
name="first",
name_alias=None,
desired_features=[],
features=[],
)
second_projection = FeatureViewProjection(
name="second",
name_alias=None,
desired_features=[],
features=[],
)
first_projection.join_key_map["entity_id"] = "aliased_entity_id"
assert second_projection.join_key_map == {}
def test_build_apply_request():
request = FeatureService.build_apply_request(
name="my-feature-service",
project="test_project",
feature_view_refs=[
("feature_view_a", None),
("feature_view_b", ["feature1"]),
],
description="test service",
tags={"team": "ml"},
owner="owner@example.com",
)
spec = request.feature_service.spec
assert spec.name == "my-feature-service"
assert spec.description == "test service"
assert spec.owner == "owner@example.com"
assert dict(spec.tags) == {"team": "ml"}
assert request.project == "test_project"
assert len(spec.features) == 2
assert spec.features[0].feature_view_name == "feature_view_a"
assert len(spec.features[0].feature_columns) == 0
assert spec.features[1].feature_view_name == "feature_view_b"
assert [column.name for column in spec.features[1].feature_columns] == ["feature1"]
def test_feature_service_with_description():
feature_service = FeatureService(
name="my-feature-service", features=[], description="a clear description"
)
assert feature_service.to_proto().spec.description == "a clear description"
def test_feature_service_without_description():
feature_service = FeatureService(name="my-feature-service", features=[])
assert feature_service.to_proto().spec.description == ""
def test_hash():
file_source = FileSource(name="my-file-source", path="test.parquet")
feature_view = FeatureView(
name="my-feature-view",
entities=[],
schema=[
Field(name="feature1", dtype=Float32),
Field(name="feature2", dtype=Float32),
],
source=file_source,
)
feature_service_1 = FeatureService(
name="my-feature-service", features=[feature_view[["feature1", "feature2"]]]
)
feature_service_2 = FeatureService(
name="my-feature-service", features=[feature_view[["feature1", "feature2"]]]
)
feature_service_3 = FeatureService(
name="my-feature-service", features=[feature_view[["feature1"]]]
)
feature_service_4 = FeatureService(
name="my-feature-service",
features=[feature_view[["feature1"]]],
description="test",
)
s1 = {feature_service_1, feature_service_2}
assert len(s1) == 1
s2 = {feature_service_1, feature_service_3}
assert len(s2) == 2
s3 = {feature_service_3, feature_service_4}
assert len(s3) == 2
s4 = {feature_service_1, feature_service_2, feature_service_3, feature_service_4}
assert len(s4) == 3
@no_warnings
def test_feature_view_kw_args_normal():
file_source = FileSource(name="my-file-source", path="test.parquet")
feature_view = FeatureView(
name="my-feature-view",
entities=[],
schema=[
Field(name="feature1", dtype=Float32),
Field(name="feature2", dtype=Float32),
],
source=file_source,
)
_ = FeatureService(
name="my-feature-service", features=[feature_view[["feature1", "feature2"]]]
)