Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Ensure docstring tests always teardown #1817

Merged
merged 2 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions sdk/python/feast/feature_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def apply(

>>> from feast import FeatureStore, Entity, FeatureView, Feature, ValueType, FileSource, RepoConfig
>>> from datetime import timedelta
>>> fs = FeatureStore(config=RepoConfig(registry="feature_repo/data/registry.db", project="feature_repo", provider="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",
Expand Down Expand Up @@ -413,7 +413,7 @@ def get_historical_features(

>>> from feast import FeatureStore, RepoConfig
>>> import pandas as pd
>>> fs = FeatureStore(config=RepoConfig(registry="feature_repo/data/registry.db", project="feature_repo", provider="local"))
>>> fs = FeatureStore(repo_path="feature_repo")
>>> entity_df = pd.DataFrame.from_dict(
... {
... "driver_id": [1001, 1002],
Expand Down Expand Up @@ -498,7 +498,7 @@ def materialize_incremental(

>>> from feast import FeatureStore, RepoConfig
>>> from datetime import datetime, timedelta
>>> fs = FeatureStore(config=RepoConfig(registry="feature_repo/data/registry.db", project="feature_repo", provider="local"))
>>> fs = FeatureStore(repo_path="feature_repo")
>>> fs.materialize_incremental(end_date=datetime.utcnow() - timedelta(minutes=5))
Materializing...
<BLANKLINE>
Expand Down Expand Up @@ -583,7 +583,7 @@ def materialize(

>>> from feast import FeatureStore, RepoConfig
>>> from datetime import datetime, timedelta
>>> fs = FeatureStore(config=RepoConfig(registry="feature_repo/data/registry.db", project="feature_repo", provider="local"))
>>> fs = FeatureStore(repo_path="feature_repo")
>>> fs.materialize(
... start_date=datetime.utcnow() - timedelta(hours=3), end_date=datetime.utcnow() - timedelta(minutes=10)
... )
Expand Down Expand Up @@ -675,7 +675,7 @@ def get_online_features(
from 3 hours ago to 10 minutes ago, and then retrieve these online features.

>>> from feast import FeatureStore, RepoConfig
>>> fs = FeatureStore(config=RepoConfig(registry="feature_repo/data/registry.db", project="feature_repo", provider="local"))
>>> fs = FeatureStore(repo_path="feature_repo")
>>> online_response = fs.get_online_features(
... features=[
... "driver_hourly_stats:conv_rate",
Expand Down
61 changes: 27 additions & 34 deletions sdk/python/tests/doctest/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,15 @@
import feast


def setup_feature_store(docstring_tests):
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,
RepoConfig,
ValueType,
)
from feast import Entity, Feature, FeatureStore, FeatureView, FileSource, ValueType
from feast.repo_operations import init_repo

init_repo("feature_repo", "local")
fs = FeatureStore(
config=RepoConfig(
registry="feature_repo/data/registry.db",
project="feature_repo",
provider="local",
)
)
fs = FeatureStore(repo_path="feature_repo")
driver = Entity(
name="driver_id", value_type=ValueType.INT64, description="driver id",
)
Expand All @@ -56,12 +42,11 @@ def setup_feature_store(docstring_tests):
)


def teardown_feature_store(docstring_tests):
def teardown_feature_store():
"""Cleans up the local environment after a FeatureStore docstring test."""
import shutil

shutil.rmtree("feature_repo", ignore_errors=True)
shutil.rmtree("data", ignore_errors=True)


def test_docstrings():
Expand All @@ -84,28 +69,36 @@ def test_docstrings():

try:
temp_module = importlib.import_module(full_name)
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)
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,
setUp=setup_function,
tearDown=teardown_function,
optionflags=doctest.ELLIPSIS,
temp_module, optionflags=doctest.ELLIPSIS,
)
if test_suite.countTestCases() > 0:
result = unittest.TextTestRunner(sys.stdout).run(test_suite)
if not result.wasSuccessful():
successful = False

if is_pkg:
next_packages.append(temp_module)
except ModuleNotFoundError:
pass
except Exception:
successful = False
finally:
if teardown_function:
teardown_function()

current_packages = next_packages

Expand Down