Skip to content

Commit

Permalink
Merge pull request #266 from markkuleinio/fix-init-local-context-data
Browse files Browse the repository at this point in the history
Fixes 242: Copy local_context_data as-is in Record._add_cache()
  • Loading branch information
Zach Moody authored Jul 8, 2020
2 parents 064b72e + 2b34432 commit bb93c8e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
25 changes: 9 additions & 16 deletions pynetbox/core/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
See the License for the specific language governing permissions and
limitations under the License.
"""
import copy

import pynetbox.core.app
from six.moves.urllib.parse import urlsplit
from pynetbox.core.query import Request
Expand Down Expand Up @@ -235,7 +237,10 @@ def __eq__(self, other):

def _add_cache(self, item):
key, value = item
self._init_cache.append((key, get_return(value)))
if key == "local_context_data":
self._init_cache.append((key, copy.deepcopy(value)))
else:
self._init_cache.append((key, get_return(value)))

def _parse_values(self, values):
""" Parses values init arg.
Expand All @@ -252,7 +257,9 @@ def list_parser(list_item):
for k, v in values.items():
if isinstance(v, dict):
lookup = getattr(self.__class__, k, None)
if k == "custom_fields" or hasattr(lookup, "_json_field"):
if k in ["custom_fields", "local_context_data"] or hasattr(
lookup, "_json_field"
):
self._add_cache((k, v.copy()))
setattr(self, k, v)
continue
Expand All @@ -275,20 +282,6 @@ def _endpoint_from_url(self, url):
app, name = urlsplit(url).path.split("/")[2:4]
return getattr(pynetbox.core.app.App(self.api, app), name)

def _compare(self):
"""Compares current attributes to values at instantiation.
In order to be idempotent we run this method in `save()`.
Returns:
Boolean value, True indicates current instance has the same
attributes as the ones passed to `values`.
"""

if self.serialize(init=True) == self.serialize():
return True
return False

def full_details(self):
"""Queries the hyperlinked endpoint if 'url' is defined.
Expand Down
8 changes: 4 additions & 4 deletions tests/test_dcim.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def test_delete(self):
headers=HEADERS,
)

def test_compare(self):
def test_diff(self):
with patch(
"pynetbox.core.query.requests.sessions.Session.get",
return_value=Response(
Expand All @@ -120,7 +120,7 @@ def test_compare(self):
):
ret = getattr(nb, self.name).get(1)
self.assertTrue(ret)
self.assertTrue(ret._compare())
self.assertEqual(ret._diff(), set())

def test_serialize(self):
with patch(
Expand Down Expand Up @@ -186,7 +186,7 @@ def test_modify(self, *_):
ret.serial = "123123123123"
ret_serialized = ret.serialize()
self.assertTrue(ret_serialized)
self.assertFalse(ret._compare())
self.assertEqual(ret._diff(), {"serial"})
self.assertEqual(ret_serialized["serial"], "123123123123")

@patch(
Expand Down Expand Up @@ -264,7 +264,7 @@ def test_modify_custom(self, *_):
"""
ret = getattr(nb, self.name).get(1)
ret.custom_fields["test_custom"] = "Testing"
self.assertFalse(ret._compare())
self.assertEqual(ret._diff(), {"custom_fields"})
self.assertTrue(ret.serialize())
self.assertEqual(ret.custom_fields["test_custom"], "Testing")

Expand Down
4 changes: 2 additions & 2 deletions tests/test_ipam.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def test_modify(self, *_):
ret.prefix = "10.1.2.0/24"
ret_serialized = ret.serialize()
self.assertTrue(ret_serialized)
self.assertFalse(ret._compare())
self.assertEqual(ret._diff(), {"prefix"})
self.assertEqual(ret_serialized["prefix"], "10.1.2.0/24")

@patch(
Expand Down Expand Up @@ -216,7 +216,7 @@ def test_modify(self, *_):
ret.description = "testing"
ret_serialized = ret.serialize()
self.assertTrue(ret_serialized)
self.assertFalse(ret._compare())
self.assertEqual(ret._diff(), {"description"})
self.assertEqual(ret_serialized["address"], "10.0.255.1/32")
self.assertEqual(ret_serialized["description"], "testing")

Expand Down
6 changes: 5 additions & 1 deletion tests/unit/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,16 @@ def test_diff(self):
"nested_dict": {"id": 222, "name": "bar"},
"tags": ["foo", "bar"],
"int_list": [123, 321, 231],
"local_context_data": {"data": ["one"]},
}
test = Record(test_values, None, None)
test.tags.append("baz")
test.nested_dict = 1
test.string_field = "foobaz"
self.assertEqual(test._diff(), {"tags", "nested_dict", "string_field"})
test.local_context_data["data"].append("two")
self.assertEqual(
test._diff(), {"tags", "nested_dict", "string_field", "local_context_data"}
)

def test_diff_append_records_list(self):
test_values = {
Expand Down

0 comments on commit bb93c8e

Please sign in to comment.