forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.py
More file actions
486 lines (427 loc) · 19.2 KB
/
test_client.py
File metadata and controls
486 lines (427 loc) · 19.2 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# Copyright 2018 The Feast Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from datetime import datetime
import grpc
import numpy as np
import pandas as pd
import pytest
from google.protobuf.timestamp_pb2 import Timestamp
from pandas.util.testing import assert_frame_equal
import feast.core.CoreService_pb2_grpc as core
import feast.core.DatasetService_pb2_grpc as training
import feast.core.JobService_pb2_grpc as jobs
import feast.serving.Serving_pb2 as serving_pb
from feast.core.CoreService_pb2 import CoreServiceTypes
from feast.core.DatasetService_pb2 import DatasetInfo as DatasetInfo_pb
from feast.core.DatasetService_pb2 import DatasetServiceTypes
from feast.core.JobService_pb2 import JobServiceTypes
from feast.sdk.client import Client, _parse_date, _timestamp_from_datetime
from feast.sdk.importer import Importer
from feast.sdk.resources.entity import Entity
from feast.sdk.resources.feature import Feature
from feast.sdk.resources.feature_group import FeatureGroup
from feast.sdk.resources.feature_set import FeatureSet, DatasetInfo, FileType
from feast.sdk.resources.storage import Storage
from feast.sdk.utils.bq_util import TableDownloader
from feast.serving.Serving_pb2 import QueryFeaturesRequest, \
QueryFeaturesResponse, FeatureValue
from feast.specs.FeatureSpec_pb2 import FeatureSpec
from feast.specs.ImportSpec_pb2 import ImportSpec
from feast.specs.StorageSpec_pb2 import StorageSpec
from feast.types.Value_pb2 import Value
class TestClient(object):
@pytest.fixture
def client(self, mocker):
cli = Client(core_url="some.uri", serving_url="some.serving.uri")
mocker.patch.object(cli, '_connect_core')
mocker.patch.object(cli, '_connect_serving')
return cli
def test_apply_single_feature(self, client, mocker):
my_feature = Feature(name="test", entity="test")
grpc_stub = core.CoreServiceStub(grpc.insecure_channel(""))
with mocker.patch.object(
grpc_stub,
'ApplyFeature',
return_value=CoreServiceTypes.ApplyFeatureResponse(
featureId="test.test")):
client._core_service_stub = grpc_stub
id = client.apply(my_feature)
assert id == "test.test"
def test_apply_single_entity(self, client, mocker):
my_entity = Entity(name="test")
grpc_stub = core.CoreServiceStub(grpc.insecure_channel(""))
with mocker.patch.object(
grpc_stub,
'ApplyEntity',
return_value=CoreServiceTypes.ApplyEntityResponse(
entityName="test")):
client._core_service_stub = grpc_stub
name = client.apply(my_entity)
assert name == "test"
def test_apply_single_feature_group(self, client, mocker):
my_feature_group = FeatureGroup(id="test")
grpc_stub = core.CoreServiceStub(grpc.insecure_channel(""))
with mocker.patch.object(
grpc_stub,
'ApplyFeatureGroup',
return_value=CoreServiceTypes.ApplyFeatureGroupResponse(
featureGroupId="test")):
client._core_service_stub = grpc_stub
name = client.apply(my_feature_group)
assert name == "test"
def test_apply_unsupported_object(self, client):
with pytest.raises(TypeError) as e_info:
client.apply(None)
assert e_info.__str__() == "Apply can only be passed one of the" \
+ "following types: [Feature, Entity, FeatureGroup, Storage, Importer]"
def test_apply_multiple(self, client, mocker):
my_feature_group = FeatureGroup(id="test")
my_entity = Entity(name="test")
grpc_stub = core.CoreServiceStub(grpc.insecure_channel(""))
mocker.patch.object(
grpc_stub,
'ApplyFeatureGroup',
return_value=CoreServiceTypes.ApplyFeatureGroupResponse(
featureGroupId="test"))
mocker.patch.object(
grpc_stub,
'ApplyEntity',
return_value=CoreServiceTypes.ApplyEntityResponse(
entityName="test"))
client._core_service_stub = grpc_stub
ids = client.apply([my_entity, my_feature_group])
assert ids == ["test", "test"]
def test_run_job_no_staging(self, client, mocker):
grpc_stub = jobs.JobServiceStub(grpc.insecure_channel(""))
mocker.patch.object(
grpc_stub,
'SubmitJob',
return_value=JobServiceTypes.SubmitImportJobResponse(
jobId="myjob12312"))
client._job_service_stub = grpc_stub
importer = Importer({"import": ImportSpec()}, None,
{"require_staging": False})
job_id = client.run(importer)
assert job_id == "myjob12312"
def test_create_dataset_invalid_args(self, client):
feature_set = FeatureSet("entity", ["entity.feature1"])
# empty feature set
with pytest.raises(ValueError, match="feature set is empty"):
inv_feature_set = FeatureSet("entity", [])
client.create_dataset(inv_feature_set, "2018-12-01", "2018-12-02")
# invalid start date
with pytest.raises(
ValueError,
match="Incorrect date format, should be YYYY-MM-DD"):
client.create_dataset(feature_set, "20181201", "2018-12-02")
# invalid end date
with pytest.raises(
ValueError,
match="Incorrect date format, should be YYYY-MM-DD"):
client.create_dataset(feature_set, "2018-12-01", "20181202")
# start date > end date
with pytest.raises(ValueError, match="end_date is before start_date"):
client.create_dataset(feature_set, "2018-12-02", "2018-12-01")
# invalid limit
with pytest.raises(
ValueError, match="limit is not a positive integer"):
client.create_dataset(feature_set, "2018-12-01", "2018-12-02", -1)
with pytest.raises(ValueError, match="filters is not dictionary"):
client.create_dataset(feature_set, "2018-12-01", "2018-12-02",
10, filters="filter")
def test_create_dataset(self, client, mocker):
entity_name = "myentity"
feature_ids = ["myentity.feature1", "myentity.feature2"]
fs = FeatureSet(entity_name, feature_ids)
start_date = "2018-01-02"
end_date = "2018-12-31"
ds_pb = DatasetInfo_pb(
name="dataset_name", tableUrl="project.dataset.table")
mock_trn_stub = training.DatasetServiceStub(grpc.insecure_channel(""))
mocker.patch.object(
mock_trn_stub,
"CreateDataset",
return_value=DatasetServiceTypes.CreateDatasetResponse(
datasetInfo=ds_pb))
client._dataset_service_stub = mock_trn_stub
ds = client.create_dataset(fs, start_date, end_date)
assert "dataset_name" == ds.name
assert "project.dataset.table" == ds.full_table_id
mock_trn_stub.CreateDataset.assert_called_once_with(
DatasetServiceTypes.CreateDatasetRequest(
featureSet=fs.proto,
startDate=_timestamp_from_datetime(_parse_date(start_date)),
endDate=_timestamp_from_datetime(_parse_date(end_date)),
limit=None,
namePrefix=None))
def test_create_dataset_with_filters(self, client, mocker):
entity_name = "myentity"
feature_ids = ["myentity.feature1", "myentity.feature2"]
fs = FeatureSet(entity_name, feature_ids)
start_date = "2018-01-02"
end_date = "2018-12-31"
ds_pb = DatasetInfo_pb(
name="dataset_name", tableUrl="project.dataset.table")
mock_trn_stub = training.DatasetServiceStub(grpc.insecure_channel(""))
mocker.patch.object(
mock_trn_stub,
"CreateDataset",
return_value=DatasetServiceTypes.CreateDatasetResponse(
datasetInfo=ds_pb))
client._dataset_service_stub = mock_trn_stub
job_filter = {"job_id": 12345}
ds = client.create_dataset(fs, start_date, end_date,
filters=job_filter)
assert "dataset_name" == ds.name
assert "project.dataset.table" == ds.full_table_id
mock_trn_stub.CreateDataset.assert_called_once_with(
DatasetServiceTypes.CreateDatasetRequest(
featureSet=fs.proto,
startDate=_timestamp_from_datetime(_parse_date(start_date)),
endDate=_timestamp_from_datetime(_parse_date(end_date)),
limit=None,
namePrefix=None,
filters={"job_id": "12345"}))
def test_create_dataset_with_limit(self, client, mocker):
entity_name = "myentity"
feature_ids = ["myentity.feature1", "myentity.feature2"]
fs = FeatureSet(entity_name, feature_ids)
start_date = "2018-01-02"
end_date = "2018-12-31"
limit = 100
ds_pb = DatasetInfo_pb(
name="dataset_name", tableUrl="project.dataset.table")
mock_trn_stub = training.DatasetServiceStub(grpc.insecure_channel(""))
mocker.patch.object(
mock_trn_stub,
"CreateDataset",
return_value=DatasetServiceTypes.CreateDatasetResponse(
datasetInfo=ds_pb))
client._dataset_service_stub = mock_trn_stub
ds = client.create_dataset(fs, start_date, end_date, limit=limit)
assert "dataset_name" == ds.name
assert "project.dataset.table" == ds.full_table_id
mock_trn_stub.CreateDataset.assert_called_once_with(
DatasetServiceTypes.CreateDatasetRequest(
featureSet=fs.proto,
startDate=_timestamp_from_datetime(_parse_date(start_date)),
endDate=_timestamp_from_datetime(_parse_date(end_date)),
limit=limit,
namePrefix=None))
def test_create_dataset_with_name_prefix(self, client, mocker):
entity_name = "myentity"
feature_ids = ["myentity.feature1", "myentity.feature2"]
fs = FeatureSet(entity_name, feature_ids)
start_date = "2018-01-02"
end_date = "2018-12-31"
limit = 100
name_prefix = "feast"
ds_pb = DatasetInfo_pb(
name="dataset_name", tableUrl="project.dataset.table")
mock_dssvc_stub = training.DatasetServiceStub(
grpc.insecure_channel(""))
mocker.patch.object(
mock_dssvc_stub,
"CreateDataset",
return_value=DatasetServiceTypes.CreateDatasetResponse(
datasetInfo=ds_pb))
client._dataset_service_stub = mock_dssvc_stub
ds = client.create_dataset(
fs, start_date, end_date, limit=limit, name_prefix=name_prefix)
assert "dataset_name" == ds.name
assert "project.dataset.table" == ds.full_table_id
mock_dssvc_stub.CreateDataset.assert_called_once_with(
DatasetServiceTypes.CreateDatasetRequest(
featureSet=fs.proto,
startDate=_timestamp_from_datetime(_parse_date(start_date)),
endDate=_timestamp_from_datetime(_parse_date(end_date)),
limit=limit,
namePrefix=name_prefix))
def test_build_serving_request(self, client):
feature_set = FeatureSet("entity", ["entity.feat1", "entity.feat2"])
req = client._build_serving_request(feature_set, ["1", "2", "3"])
expected = QueryFeaturesRequest(
entityName="entity",
entityId=["1", "2", "3"],
featureId=feature_set.features)
assert req == expected
def test_serving_response_to_df(self, client):
response = self._create_query_features_response(
entity_name="entity",
entities={
"1": {
"entity.feat1": (1, Timestamp(seconds=1)),
"entity.feat2": (2, Timestamp(seconds=2))
},
"2": {
"entity.feat1": (3, Timestamp(seconds=3)),
"entity.feat2": (4, Timestamp(seconds=4))
}
})
expected_df = pd.DataFrame({'entity': ["1", "2"],
'entity.feat1': [1, 3],
'entity.feat2': [2, 4]}) \
.reset_index(drop=True)
df = client._response_to_df(FeatureSet("entity", ["entity.feat1",
"entity.feat2"]),
response) \
.sort_values(['entity']) \
.reset_index(drop=True)[expected_df.columns]
assert_frame_equal(
df,
expected_df,
check_dtype=False,
check_column_type=False,
check_index_type=False)
def test_serving_response_to_df_with_missing_value(self, client):
response = self._create_query_features_response(
entity_name="entity",
entities={
"1": {
"entity.feat1": (1, Timestamp(seconds=1))
},
"2": {
"entity.feat1": (3, Timestamp(seconds=3)),
"entity.feat2": (4, Timestamp(seconds=4))
}
})
expected_df = pd.DataFrame({'entity': ["1", "2"],
'entity.feat1': [1, 3],
'entity.feat2': [np.nan, 4]}) \
.reset_index(drop=True)
df = client._response_to_df(FeatureSet("entity", ["entity.feat1",
"entity.feat2"]),
response) \
.sort_values(['entity']) \
.reset_index(drop=True)[expected_df.columns]
assert_frame_equal(
df,
expected_df,
check_dtype=False,
check_column_type=False,
check_index_type=False)
def test_serving_response_to_df_with_missing_feature(self, client):
response = self._create_query_features_response(
entity_name="entity",
entities={
"1": {
"entity.feat1": (1, Timestamp(seconds=1))
},
"2": {
"entity.feat1": (3, Timestamp(seconds=3))
}
})
expected_df = pd.DataFrame({'entity': ["1", "2"],
'entity.feat1': [1, 3],
'entity.feat2': [np.NaN, np.NaN]}) \
.reset_index(drop=True)
df = client._response_to_df(FeatureSet("entity", ["entity.feat1",
"entity.feat2"]),
response) \
.sort_values(['entity']) \
.reset_index(drop=True)[expected_df.columns]
assert_frame_equal(
df,
expected_df,
check_dtype=False,
check_column_type=False,
check_index_type=False)
def test_serving_response_to_df_no_data(self, client):
response = QueryFeaturesResponse(entityName="entity")
expected_df = pd.DataFrame(
columns=['entity', 'entity.feat1', 'entity.feat2'])
df = client._response_to_df(
FeatureSet("entity", ["entity.feat1", "entity.feat2"]), response)
assert_frame_equal(
df,
expected_df,
check_dtype=False,
check_column_type=False,
check_index_type=False)
def test_serving_response_to_df_with_time_filter(self, client):
response = self._create_query_features_response(
entity_name="entity",
entities={
"1": {
"entity.feat1": (1, Timestamp(seconds=1))
},
"2": {
"entity.feat1": (3, Timestamp(seconds=3))
}
})
expected_df = pd.DataFrame({'entity': ["1", "2"],
'entity.feat1': [np.NaN, 3],
'entity.feat2': [np.NaN, np.NaN]}) \
.reset_index(drop=True)
start = datetime.utcfromtimestamp(2)
end = datetime.utcfromtimestamp(5)
df = client._response_to_df(FeatureSet("entity", ["entity.feat1",
"entity.feat2"]),
response, start, end) \
.sort_values(['entity']) \
.reset_index(drop=True)[expected_df.columns]
assert_frame_equal(
df,
expected_df,
check_dtype=False,
check_column_type=False,
check_index_type=False)
def test_serving_invalid_type(self, client):
start = "2018-01-01T01:01:01"
end = "2018-01-01T01:01:01"
ts_range = [start, end]
with pytest.raises(
TypeError, match="start and end must be datetime "
"type"):
client.get_serving_data(
FeatureSet("entity", ["entity.feat1", "entity.feat2"]),
["1234", "5678"], ts_range)
def test_download_dataset_as_file(self, client, mocker):
destination = "/tmp/dest_file"
table_dlder = TableDownloader()
mocker.patch.object(
table_dlder, "download_table_as_file", return_value=destination)
client._table_downloader = table_dlder
full_table_id = "project.dataset.table"
staging_location = "gs://gcs_bucket/"
dataset = DatasetInfo("mydataset", full_table_id)
result = client.download_dataset(
dataset,
destination,
staging_location=staging_location,
file_type=FileType.CSV)
assert result == destination
table_dlder.download_table_as_file.assert_called_once_with(
full_table_id, destination, FileType.CSV, staging_location)
def _create_query_features_response(self, entity_name, entities):
response = QueryFeaturesResponse(entityName=entity_name)
for entity_id, feature_map in entities.items():
feature = {}
for feature_id, feature_value in feature_map.items():
feature[feature_id] = FeatureValue(
value=Value(int32Val=feature_value[0]),
timestamp=feature_value[1])
entity_pb = serving_pb.Entity(features=feature)
response.entities[entity_id].CopyFrom(entity_pb)
return response
def _create_feature_spec(self, feature_id, wh_id):
return FeatureSpec(id=feature_id)
def _create_bq_spec(self, id, project, dataset):
return StorageSpec(
id=id,
type="bigquery",
options={
"project": project,
"dataset": dataset
})