-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathtest_all.py
More file actions
109 lines (90 loc) · 3.95 KB
/
Copy pathtest_all.py
File metadata and controls
109 lines (90 loc) · 3.95 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
import doctest
import importlib
import pkgutil
import sys
import unittest
import feast
def setup_feature_store():
"""Prepares the local environment for a FeatureStore docstring test."""
from datetime import datetime, timedelta
from feast import Entity, Feature, FeatureStore, FeatureView, FileSource, ValueType
from feast.repo_operations import init_repo
init_repo("feature_repo", "local")
fs = FeatureStore(repo_path="feature_repo")
driver = Entity(
name="driver_id", value_type=ValueType.INT64, description="driver id",
)
driver_hourly_stats = FileSource(
path="feature_repo/data/driver_stats.parquet",
event_timestamp_column="event_timestamp",
created_timestamp_column="created",
)
driver_hourly_stats_view = FeatureView(
name="driver_hourly_stats",
entities=["driver_id"],
ttl=timedelta(seconds=86400 * 1),
features=[
Feature(name="conv_rate", dtype=ValueType.FLOAT),
Feature(name="acc_rate", dtype=ValueType.FLOAT),
Feature(name="avg_daily_trips", dtype=ValueType.INT64),
],
batch_source=driver_hourly_stats,
)
fs.apply([driver_hourly_stats_view, driver])
fs.materialize(
start_date=datetime.utcnow() - timedelta(hours=3),
end_date=datetime.utcnow() - timedelta(minutes=10),
)
def teardown_feature_store():
"""Cleans up the local environment after a FeatureStore docstring test."""
import shutil
shutil.rmtree("feature_repo", ignore_errors=True)
def test_docstrings():
"""Runs all docstring tests.
Imports all submodules of the feast package. Checks the submodules for docstring
tests and runs them. Setup functions for a submodule named "feast.x.y.z" should be
defined in this module as a function named "setup_x_y_z". Teardown functions can be
defined similarly. Setup and teardown functions are per-submodule.
"""
successful = True
current_packages = [feast]
failed_cases = []
while current_packages:
next_packages = []
for package in current_packages:
for _, name, is_pkg in pkgutil.walk_packages(package.__path__):
full_name = package.__name__ + "." + name
try:
temp_module = importlib.import_module(full_name)
if is_pkg:
next_packages.append(temp_module)
except ModuleNotFoundError:
pass
# Retrieve the setup and teardown functions defined in this file.
relative_path_from_feast = full_name.split(".", 1)[1]
function_suffix = relative_path_from_feast.replace(".", "_")
setup_function_name = "setup_" + function_suffix
teardown_function_name = "teardown_" + function_suffix
setup_function = globals().get(setup_function_name)
teardown_function = globals().get(teardown_function_name)
# Execute the test with setup and teardown functions.
try:
if setup_function:
setup_function()
test_suite = doctest.DocTestSuite(
temp_module, optionflags=doctest.ELLIPSIS,
)
if test_suite.countTestCases() > 0:
result = unittest.TextTestRunner(sys.stdout).run(test_suite)
if not result.wasSuccessful():
successful = False
failed_cases.append(result.failures)
except Exception as e:
successful = False
failed_cases.append((full_name, e))
finally:
if teardown_function:
teardown_function()
current_packages = next_packages
if not successful:
raise Exception(f"Docstring tests failed. Failed results: {failed_cases}")