Skip to content

Commit

Permalink
Merge pull request #365 from digitalocean/config-conftext-diff
Browse files Browse the repository at this point in the history
Deepcopy in _parse_values
  • Loading branch information
Zach Moody committed Apr 8, 2021
2 parents d62e5e0 + c0a81de commit 35adedd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
7 changes: 2 additions & 5 deletions pynetbox/core/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/test_extras.py
Original file line number Diff line number Diff line change
@@ -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"})

0 comments on commit 35adedd

Please sign in to comment.