Skip to content

Commit

Permalink
Merge pull request #3124 from FriedrichFroebel/issue3110
Browse files Browse the repository at this point in the history
  • Loading branch information
justinmayer committed Jun 4, 2023
2 parents 8b6b7da + 7adcfc7 commit 043ab61
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
31 changes: 31 additions & 0 deletions pelican/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,3 +860,34 @@ def test_pass_deep_subpaths(self):
utils.posixize_path(
os.path.abspath(os.path.join("/foo/bar", "test")))
)


class TestMemoized(unittest.TestCase):
def test_memoized(self):
class Container:
def _get(self, key):
pass

@utils.memoized
def get(self, key):
return self._get(key)

container = Container()

with unittest.mock.patch.object(
container, "_get", side_effect=lambda x: x
) as get_mock:
self.assertEqual("foo", container.get("foo"))
get_mock.assert_called_once_with("foo")

get_mock.reset_mock()
self.assertEqual("foo", container.get("foo"))
get_mock.assert_not_called()

self.assertEqual("bar", container.get("bar"))
get_mock.assert_called_once_with("bar")

get_mock.reset_mock()
container.get.cache.clear()
self.assertEqual("bar", container.get("bar"))
get_mock.assert_called_once_with("bar")
4 changes: 3 additions & 1 deletion pelican/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ def __repr__(self):

def __get__(self, obj, objtype):
'''Support instance methods.'''
return partial(self.__call__, obj)
fn = partial(self.__call__, obj)
fn.cache = self.cache
return fn


def deprecated_attribute(old, new, since=None, remove=None, doc=None):
Expand Down

0 comments on commit 043ab61

Please sign in to comment.