From 7c4d26a37f1d1266b4c9be55b8ee1850f5e3d4ad Mon Sep 17 00:00:00 2001 From: Zach Moody Date: Thu, 8 Apr 2021 16:13:50 -0500 Subject: [PATCH 1/2] Deepcopy in _parse_values Go ahead and deepcopy if field is either of the built-in json fields (custom_fields or local_context_data) or if field is a JsonField object (ConfigContexts.data). Fixes #349 --- pynetbox/core/response.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/pynetbox/core/response.py b/pynetbox/core/response.py index 181999a4..081b377d 100644 --- a/pynetbox/core/response.py +++ b/pynetbox/core/response.py @@ -268,10 +268,7 @@ def __eq__(self, other): def _add_cache(self, item): key, value = item - if key == "local_context_data": - self._init_cache.append((key, copy.deepcopy(value))) - else: - self._init_cache.append((key, get_return(value))) + self._init_cache.append((key, get_return(value))) def _parse_values(self, values): """ Parses values init arg. @@ -291,7 +288,7 @@ def list_parser(list_item): if k in ["custom_fields", "local_context_data"] or hasattr( lookup, "_json_field" ): - self._add_cache((k, v.copy())) + self._add_cache((k, copy.deepcopy(v))) setattr(self, k, v) continue if lookup: From c0a81de8f6f6a25cd522d790a71c946d8aacaa2a Mon Sep 17 00:00:00 2001 From: Zach Moody Date: Thu, 8 Apr 2021 16:17:10 -0500 Subject: [PATCH 2/2] Add unit tests for Extras model --- tests/unit/test_extras.py | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/unit/test_extras.py diff --git a/tests/unit/test_extras.py b/tests/unit/test_extras.py new file mode 100644 index 00000000..922a5149 --- /dev/null +++ b/tests/unit/test_extras.py @@ -0,0 +1,40 @@ +import unittest + +import six + +from pynetbox.models.extras import ConfigContexts + + +class ExtrasTestCase(unittest.TestCase): + def test_config_contexts(self): + test_values = { + "data": {"test_int": 123, "test_str": "testing", "test_list": [1, 2, 3],} + } + test = ConfigContexts(test_values, None, None) + self.assertTrue(test) + + def test_config_contexts_diff_str(self): + test_values = { + "data": { + "test_int": 123, + "test_str": "testing", + "test_list": [1, 2, 3], + "test_dict": {"foo": "bar"}, + } + } + test = ConfigContexts(test_values, None, None) + test.data["test_str"] = "bar" + self.assertEqual(test._diff(), {"data"}) + + def test_config_contexts_diff_dict(self): + test_values = { + "data": { + "test_int": 123, + "test_str": "testing", + "test_list": [1, 2, 3], + "test_dict": {"foo": "bar"}, + } + } + test = ConfigContexts(test_values, None, None) + test.data["test_dict"].update({"bar": "foo"}) + self.assertEqual(test._diff(), {"data"})