Skip to content

Commit

Permalink
Fix caching issue when calling list_methods with source
Browse files Browse the repository at this point in the history
  • Loading branch information
storax committed Nov 17, 2016
1 parent f612623 commit 2347a2c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/reddel_server/provider.py
Expand Up @@ -402,12 +402,16 @@ def _get_methods(self, src=None):
:type source: :class:`str`
:returns: dict method names and methods
"""
if self._cached_methods is None:
self._cached_methods = {}
methods = {}
if src or self._cached_methods is None:
for p in reversed(self._providers):
self._cached_methods.update(p._get_methods(src=src))
self._cached_methods.update(super(ChainedProvider, self)._get_methods(src=src))
return self._cached_methods
methods.update(p._get_methods(src=src))
methods.update(super(ChainedProvider, self)._get_methods(src=src))
if self._cached_methods is None:
self._cached_methods = methods
else:
methods = self._cached_methods
return methods


class RedBaronProvider(ProviderBase):
Expand Down
15 changes: 14 additions & 1 deletion tests/test_provider.py
Expand Up @@ -241,11 +241,24 @@ def test_ChainedProvider_get_methods(foobarprovider, fooprovider, barprovider):


def test_ChainedProvider_get_methods_cached(foobarprovider):
"""Test that all methods from all providers are present."""
"""Test that methods get cached."""
methdict = foobarprovider._get_methods()
assert methdict is foobarprovider._get_methods()


def test_ChainedProvider_get_methods_cached_src(foobarprovider):
"""Test that methods do not get cached when calling with a source."""
methdict = foobarprovider._get_methods('1+1')
assert methdict is not foobarprovider._get_methods('1+1')


def test_ChainedProvider_get_methods_cached_src_fresh(foobarprovider):
"""Test that methods do not get cached when calling with a source
when calling without source first."""
methdict = foobarprovider._get_methods()
assert methdict is not foobarprovider._get_methods('1+1')


@pytest.mark.parametrize("method", ["foo", "bar"])
def test_ChainedProvider_get_method(foobarprovider, method):
"""Test that name resolution works"""
Expand Down

0 comments on commit 2347a2c

Please sign in to comment.