From dbceb28a419e250e4e7a472235f88b5e7a053e8a Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Tue, 6 Apr 2021 13:56:14 -0700 Subject: [PATCH 1/2] test: add test that mem-cached obj doesn't change --- powersimdata/utility/tests/test_helpers.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/powersimdata/utility/tests/test_helpers.py b/powersimdata/utility/tests/test_helpers.py index 7b75438c1..d6cea1552 100644 --- a/powersimdata/utility/tests/test_helpers.py +++ b/powersimdata/utility/tests/test_helpers.py @@ -68,6 +68,17 @@ def test_mem_cache_get_returns_copy(): assert id(cache.get(key)) != id(obj) +def test_mem_cache_put_version_never_changes(): + cache = MemoryCache() + key = cache_key("foo", 4) + obj = {"key1": "value1"} + cache.put(key, obj) + obj["key2"] = "value2" + assert "key1" in cache.get(key) + assert "key2" not in cache.get(key) + assert "key2" in obj + + def test_copy_command(): expected = r"\cp -p source dest" command = CommandBuilder.copy("source", "dest") From d7af92beddcf04a1af21de33bfc7b1da679ab6d2 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Tue, 6 Apr 2021 13:56:40 -0700 Subject: [PATCH 2/2] fix: add deepcopy on the way into MemoryCache --- powersimdata/utility/helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powersimdata/utility/helpers.py b/powersimdata/utility/helpers.py index 9aaec9228..a56534cb9 100644 --- a/powersimdata/utility/helpers.py +++ b/powersimdata/utility/helpers.py @@ -63,7 +63,7 @@ def put(self, key, obj): :param tuple key: a tuple used to lookup the cached value :param Any obj: the object to cache """ - self._cache[key] = obj + self._cache[key] = copy.deepcopy(obj) def get(self, key): """Retrieve the value associated with key if it exists.