Skip to content

Commit

Permalink
Fixed incorrect namespace used for caching function returns
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholas-plutoflume authored and john-kurkowski committed Apr 11, 2022
1 parent 86c82c3 commit ce33d03
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,23 @@ def test_get_cache_dir(monkeypatch):
monkeypatch.setenv("TLDEXTRACT_CACHE", "/alt-tld-cache")

assert get_cache_dir() == "/alt-tld-cache"


def test_cache_and_run(tmpdir):
cache = DiskCache(tmpdir)

def return_value(value):
"""Test function that returns whatever is passed to it."""
return value

# Call the cache with a function that returns any value
first_call = cache.run_and_cache(return_value, "testing", {"value": 1}, {"value"})

def raise_error(*args, **kwags): # pylint: disable=unused-argument
"""Test function that raises an error."""
raise RuntimeError()

# Calling it again with the same namespace and hash should give the same
# value
second_call = cache.run_and_cache(raise_error, "testing", {"value": 1}, {"value"})
assert first_call == second_call
2 changes: 1 addition & 1 deletion tldextract/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def run_and_cache(
result: T = self.get(namespace=namespace, key=key_args)
except KeyError:
result = func(**kwargs)
self.set(namespace="urls", key=key_args, value=result)
self.set(namespace=namespace, key=key_args, value=result)

return result

Expand Down

0 comments on commit ce33d03

Please sign in to comment.