-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added default CacheAdapter
lifecycle hook
#766
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import inspect | ||
import pathlib | ||
import shelve | ||
|
||
import pytest | ||
|
||
from hamilton import graph_types, node | ||
from hamilton.lifecycle.default import CacheAdapter | ||
|
||
|
||
def _callable_to_node(callable) -> node.Node: | ||
return node.Node( | ||
name=callable.__name__, | ||
typ=inspect.signature(callable).return_annotation, | ||
callabl=callable, | ||
) | ||
|
||
|
||
@pytest.fixture() | ||
def hook(tmp_path: pathlib.Path): | ||
return CacheAdapter(cache_path=str(tmp_path.resolve())) | ||
|
||
|
||
@pytest.fixture() | ||
def node_a(): | ||
"""Default function implementation""" | ||
|
||
def A(external_input: int) -> int: | ||
return external_input % 7 | ||
|
||
return _callable_to_node(A) | ||
|
||
|
||
@pytest.fixture() | ||
def node_a_body(): | ||
"""The function A() has modulo 5 instead of 7""" | ||
|
||
def A(external_input: int) -> int: | ||
return external_input % 5 | ||
|
||
return _callable_to_node(A) | ||
|
||
|
||
@pytest.fixture() | ||
def node_a_docstring(): | ||
"""The function A() has a docstring""" | ||
|
||
def A(external_input: int) -> int: | ||
"""This one has a docstring""" | ||
return external_input % 7 | ||
|
||
return _callable_to_node(A) | ||
|
||
|
||
def test_set_result(hook: CacheAdapter, node_a: node.Node): | ||
"""Hook sets value and assert value in cache""" | ||
node_hash = graph_types.hash_source_code(node_a.callable, strip=True) | ||
node_kwargs = dict(external_input=7) | ||
cache_key = CacheAdapter.create_key(node_hash, node_kwargs) | ||
result = 2 | ||
|
||
hook.cache_vars = [node_a.name] | ||
# used_nodes_hash would be set by run_to_execute() hook | ||
hook.used_nodes_hash[node_a.name] = node_hash | ||
hook.run_after_node_execution( | ||
node_name=node_a.name, | ||
node_kwargs=node_kwargs, | ||
result=result, | ||
) | ||
|
||
# run_to_execute_node() hook would get cache | ||
assert hook.cache.get(key=cache_key) == result | ||
|
||
|
||
def test_get_result(hook: CacheAdapter, node_a: node.Node): | ||
"""Hooks get value and assert cache hit""" | ||
node_hash = graph_types.hash_source_code(node_a.callable, strip=True) | ||
node_kwargs = dict(external_input=7) | ||
result = 2 | ||
cache_key = CacheAdapter.create_key(node_hash, node_kwargs) | ||
|
||
hook.cache_vars = [node_a.name] | ||
# run_after_node_execution() would set cache | ||
hook.cache[cache_key] = result | ||
retrieved = hook.run_to_execute_node( | ||
node_name=node_a.name, | ||
node_kwargs=node_kwargs, | ||
node_callable=node_a.callable, | ||
) | ||
|
||
assert retrieved == result | ||
|
||
|
||
def test_append_nodes_history( | ||
hook: CacheAdapter, | ||
node_a: node.Node, | ||
node_a_body: node.Node, | ||
): | ||
"""Assert the CacheHook.nodes_history is growing; | ||
doesn't check for commit to cache | ||
""" | ||
node_name = "A" | ||
node_kwargs = dict(external_input=7) | ||
hook.cache_vars = [node_a.name] | ||
|
||
# node version 1 | ||
hook.used_nodes_hash[node_name] = graph_types.hash_source_code(node_a.callable, strip=True) | ||
hook.run_to_execute_node( | ||
node_name=node_name, | ||
node_kwargs=node_kwargs, | ||
node_callable=node_a.callable, | ||
) | ||
|
||
# check history | ||
assert len(hook.nodes_history.get(node_name, [])) == 1 | ||
|
||
# node version 2 | ||
hook.used_nodes_hash[node_name] = graph_types.hash_source_code(node_a_body.callable, strip=True) | ||
hook.run_to_execute_node( | ||
node_name=node_name, | ||
node_kwargs=node_kwargs, | ||
node_callable=node_a_body.callable, | ||
) | ||
|
||
assert len(hook.nodes_history.get(node_name, [])) == 2 | ||
|
||
|
||
def test_commit_nodes_history(hook: CacheAdapter): | ||
"""Commit node history to cache""" | ||
hook.nodes_history = dict(A=["hash_1", "hash_2"]) | ||
|
||
hook.run_after_graph_execution() | ||
|
||
# need to reopen the hook cache | ||
with shelve.open(hook.cache_path) as cache: | ||
assert cache.get(CacheAdapter.nodes_history_key) == hook.nodes_history |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For afterwards -- we may want to make this a set? Odds are high this is not a slow operation, but it could get
O(n**2)
which we don't want.