forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_validation.py
More file actions
385 lines (326 loc) · 13.2 KB
/
test_validation.py
File metadata and controls
385 lines (326 loc) · 13.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
import datetime
import shutil
import pandas as pd
import pyarrow as pa
import pytest
from great_expectations.core import ExpectationSuite
from great_expectations.dataset import PandasDataset
from feast import FeatureService
from feast.dqm.errors import ValidationFailed
from feast.dqm.profilers.ge_profiler import ge_profiler
from feast.feature_logging import (
LOG_TIMESTAMP_FIELD,
FeatureServiceLoggingSource,
LoggingConfig,
)
from feast.protos.feast.serving.ServingService_pb2 import FieldStatus
from feast.utils import make_tzaware
from feast.wait import wait_retry_backoff
from tests.integration.feature_repos.repo_configuration import (
construct_universal_feature_views,
)
from tests.integration.feature_repos.universal.entities import (
customer,
driver,
location,
)
from tests.utils.cli_repo_creator import CliRunner
from tests.utils.test_log_creator import prepare_logs
_features = [
"customer_profile:current_balance",
"customer_profile:avg_passenger_count",
"customer_profile:lifetime_trip_count",
"order:order_is_success",
"global_stats:num_rides",
"global_stats:avg_ride_length",
]
@pytest.mark.integration
@pytest.mark.universal_offline_stores
def test_historical_retrieval_with_validation(environment, universal_data_sources):
store = environment.feature_store
(entities, datasets, data_sources) = universal_data_sources
feature_views = construct_universal_feature_views(data_sources)
store.apply([driver(), customer(), location(), *feature_views.values()])
# Create two identical retrieval jobs
entity_df = datasets.entity_df.drop(
columns=["order_id", "origin_id", "destination_id"]
)
reference_job = store.get_historical_features(
entity_df=entity_df,
features=_features,
)
job = store.get_historical_features(
entity_df=entity_df,
features=_features,
)
# Save dataset using reference job and retrieve it
store.create_saved_dataset(
from_=reference_job,
name="my_training_dataset",
storage=environment.data_source_creator.create_saved_dataset_destination(),
allow_overwrite=True,
)
saved_dataset = store.get_saved_dataset("my_training_dataset")
# If validation pass there will be no exceptions on this point
reference = saved_dataset.as_reference(name="ref", profiler=configurable_profiler)
job.to_df(validation_reference=reference)
@pytest.mark.integration
def test_historical_retrieval_fails_on_validation(environment, universal_data_sources):
store = environment.feature_store
(entities, datasets, data_sources) = universal_data_sources
feature_views = construct_universal_feature_views(data_sources)
store.apply([driver(), customer(), location(), *feature_views.values()])
entity_df = datasets.entity_df.drop(
columns=["order_id", "origin_id", "destination_id"]
)
reference_job = store.get_historical_features(
entity_df=entity_df,
features=_features,
)
store.create_saved_dataset(
from_=reference_job,
name="my_other_dataset",
storage=environment.data_source_creator.create_saved_dataset_destination(),
allow_overwrite=True,
)
job = store.get_historical_features(
entity_df=entity_df,
features=_features,
)
ds = store.get_saved_dataset("my_other_dataset")
profiler_expectation_suite = ds.get_profile(
profiler=profiler_with_unrealistic_expectations
)
assert len(profiler_expectation_suite.expectation_suite["expectations"]) == 3
with pytest.raises(ValidationFailed) as exc_info:
job.to_df(
validation_reference=store.get_saved_dataset(
"my_other_dataset"
).as_reference(name="ref", profiler=profiler_with_unrealistic_expectations)
)
failed_expectations = exc_info.value.report.errors
assert len(failed_expectations) == 2
assert failed_expectations[0].check_name == "expect_column_max_to_be_between"
assert failed_expectations[0].column_name == "current_balance"
assert failed_expectations[1].check_name == "expect_column_values_to_be_in_set"
assert failed_expectations[1].column_name == "avg_passenger_count"
@pytest.mark.integration
@pytest.mark.universal_offline_stores
def test_logged_features_validation(environment, universal_data_sources):
store = environment.feature_store
(_, datasets, data_sources) = universal_data_sources
feature_views = construct_universal_feature_views(data_sources)
feature_service = FeatureService(
name="test_service",
features=[
feature_views.customer[
["current_balance", "avg_passenger_count", "lifetime_trip_count"]
],
feature_views.order[["order_is_success"]],
feature_views.global_fv[["num_rides", "avg_ride_length"]],
],
logging_config=LoggingConfig(
destination=environment.data_source_creator.create_logged_features_destination()
),
)
store.apply(
[driver(), customer(), location(), feature_service, *feature_views.values()]
)
entity_df = datasets.entity_df.drop(
columns=["order_id", "origin_id", "destination_id"]
)
# add some non-existing entities to check NotFound feature handling
for i in range(5):
entity_df = pd.concat(
[
entity_df,
pd.DataFrame.from_records(
[
{
"customer_id": 2000 + i,
"driver_id": 6000 + i,
"event_timestamp": datetime.datetime.now(),
}
]
),
]
)
store_fs = store.get_feature_service(feature_service.name)
reference_dataset = store.create_saved_dataset(
from_=store.get_historical_features(
entity_df=entity_df, features=store_fs, full_feature_names=True
),
name="reference_for_validating_logged_features",
storage=environment.data_source_creator.create_saved_dataset_destination(),
allow_overwrite=True,
)
log_source_df = store.get_historical_features(
entity_df=entity_df, features=store_fs, full_feature_names=False
).to_df()
logs_df = prepare_logs(log_source_df, feature_service, store)
schema = FeatureServiceLoggingSource(
feature_service=feature_service, project=store.project
).get_schema(store._registry)
store.write_logged_features(
pa.Table.from_pandas(logs_df, schema=schema), source=feature_service
)
def validate():
"""
Return Tuple[succeed, completed]
Succeed will be True if no ValidateFailed exception was raised
"""
try:
store.validate_logged_features(
feature_service,
start=logs_df[LOG_TIMESTAMP_FIELD].min(),
end=logs_df[LOG_TIMESTAMP_FIELD].max() + datetime.timedelta(seconds=1),
reference=reference_dataset.as_reference(
name="ref", profiler=profiler_with_feature_metadata
),
)
except ValidationFailed:
return False, True
except Exception:
# log table is still being created
return False, False
return True, True
success = wait_retry_backoff(validate, timeout_secs=30)
assert success, "Validation failed (unexpectedly)"
@pytest.mark.integration
def test_e2e_validation_via_cli(environment, universal_data_sources):
runner = CliRunner()
store = environment.feature_store
(_, datasets, data_sources) = universal_data_sources
feature_views = construct_universal_feature_views(data_sources)
feature_service = FeatureService(
name="test_service",
features=[
feature_views.customer[
["current_balance", "avg_passenger_count", "lifetime_trip_count"]
],
],
logging_config=LoggingConfig(
destination=environment.data_source_creator.create_logged_features_destination()
),
)
store.apply([customer(), feature_service, feature_views.customer])
entity_df = datasets.entity_df.drop(
columns=["order_id", "origin_id", "destination_id", "driver_id"]
)
retrieval_job = store.get_historical_features(
entity_df=entity_df,
features=store.get_feature_service(feature_service.name),
full_feature_names=True,
)
logs_df = prepare_logs(retrieval_job.to_df(), feature_service, store)
saved_dataset = store.create_saved_dataset(
from_=retrieval_job,
name="reference_for_validating_logged_features",
storage=environment.data_source_creator.create_saved_dataset_destination(),
allow_overwrite=True,
)
reference = saved_dataset.as_reference(
name="test_reference", profiler=configurable_profiler
)
schema = FeatureServiceLoggingSource(
feature_service=feature_service, project=store.project
).get_schema(store._registry)
store.write_logged_features(
pa.Table.from_pandas(logs_df, schema=schema), source=feature_service
)
with runner.local_repo(example_repo_py="", offline_store="file") as local_repo:
local_repo.apply(
[customer(), feature_views.customer, feature_service, reference]
)
local_repo._registry.apply_saved_dataset(saved_dataset, local_repo.project)
validate_args = [
"validate",
"--feature-service",
feature_service.name,
"--reference",
reference.name,
(datetime.datetime.now() - datetime.timedelta(days=7)).isoformat(),
datetime.datetime.now().isoformat(),
]
p = runner.run(validate_args, cwd=local_repo.repo_path)
assert p.returncode == 0, p.stderr.decode()
assert "Validation successful" in p.stdout.decode(), p.stderr.decode()
# make sure second validation will use cached profile
shutil.rmtree(saved_dataset.storage.file_options.uri)
# Add some invalid data that would lead to failed validation
invalid_data = pd.DataFrame(
data={
"customer_id": [0],
"current_balance": [0],
"avg_passenger_count": [0],
"lifetime_trip_count": [0],
"event_timestamp": [
make_tzaware(datetime.datetime.utcnow())
- datetime.timedelta(hours=1)
],
}
)
invalid_logs = prepare_logs(invalid_data, feature_service, store)
store.write_logged_features(
pa.Table.from_pandas(invalid_logs, schema=schema), source=feature_service
)
p = runner.run(validate_args, cwd=local_repo.repo_path)
assert p.returncode == 1, p.stdout.decode()
assert "Validation failed" in p.stdout.decode(), p.stderr.decode()
# Great expectations profilers created for testing
@ge_profiler
def configurable_profiler(dataset: PandasDataset) -> ExpectationSuite:
from great_expectations.profile.user_configurable_profiler import (
UserConfigurableProfiler,
)
return UserConfigurableProfiler(
profile_dataset=dataset,
ignored_columns=["event_timestamp"],
excluded_expectations=[
"expect_table_columns_to_match_ordered_list",
"expect_table_row_count_to_be_between",
],
value_set_threshold="few",
).build_suite()
@ge_profiler(with_feature_metadata=True)
def profiler_with_feature_metadata(dataset: PandasDataset) -> ExpectationSuite:
from great_expectations.profile.user_configurable_profiler import (
UserConfigurableProfiler,
)
# always present
dataset.expect_column_values_to_be_in_set(
"global_stats__avg_ride_length__status", {FieldStatus.PRESENT}
)
# present at least in 70% of rows
dataset.expect_column_values_to_be_in_set(
"customer_profile__current_balance__status", {FieldStatus.PRESENT}, mostly=0.7
)
return UserConfigurableProfiler(
profile_dataset=dataset,
ignored_columns=["event_timestamp"]
+ [
c
for c in dataset.columns
if c.endswith("__timestamp") or c.endswith("__status")
],
excluded_expectations=[
"expect_table_columns_to_match_ordered_list",
"expect_table_row_count_to_be_between",
],
value_set_threshold="few",
).build_suite()
@ge_profiler
def profiler_with_unrealistic_expectations(dataset: PandasDataset) -> ExpectationSuite:
# note: there are 4 expectations here and only 3 are returned from the profiler
# need to create dataframe with corrupted data first
df = pd.DataFrame()
df["current_balance"] = [-100]
df["avg_passenger_count"] = [0]
other_ds = PandasDataset(df)
other_ds.expect_column_max_to_be_between("current_balance", -1000, -100)
other_ds.expect_column_values_to_be_in_set("avg_passenger_count", value_set={0})
# this should pass
other_ds.expect_column_min_to_be_between("avg_passenger_count", 0, 1000)
# this should fail
other_ds.expect_column_to_exist("missing random column")
return other_ds.get_expectation_suite()