From c7132d0befa62da521f8b45333c3563861086d55 Mon Sep 17 00:00:00 2001 From: Zach Moody Date: Fri, 10 Jul 2020 17:51:34 -0500 Subject: [PATCH 1/2] Preserve order of LIST_AS_SET attrs Values in attrs that should be handled as sets were being reordered sometimes when converted to sets. --- pynetbox/core/response.py | 3 ++- tests/unit/test_response.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pynetbox/core/response.py b/pynetbox/core/response.py index 29e124d9..49d9dc57 100644 --- a/pynetbox/core/response.py +++ b/pynetbox/core/response.py @@ -14,6 +14,7 @@ limitations under the License. """ import copy +from collections import OrderedDict import pynetbox.core.app from six.moves.urllib.parse import urlsplit @@ -341,7 +342,7 @@ def serialize(self, nested=False, init=False): v.id if isinstance(v, Record) else v for v in current_val ] if i in LIST_AS_SET: - current_val = list(set(current_val)) + current_val = list(OrderedDict.fromkeys(current_val)) ret[i] = current_val return ret diff --git a/tests/unit/test_response.py b/tests/unit/test_response.py index f37d4302..32cace43 100644 --- a/tests/unit/test_response.py +++ b/tests/unit/test_response.py @@ -209,3 +209,19 @@ def test_endpoint_from_url(self): ) ret = test._endpoint_from_url(test.url) self.assertEqual(ret.name, "test-endpoint") + + def test_serialize_tag_list_order(self): + test_tags = [ + "one", + "two", + "three", + "four", + "five", + "six", + "seven", + "eight", + "nine", + "ten", + ] + test = Record({"id": 123, "tags": test_tags}, None, None).serialize() + self.assertEqual(test["tags"], test_tags) From 7e8365a1224c3c3a1934a7ceb06a15c4765eaeb4 Mon Sep 17 00:00:00 2001 From: Zach Moody Date: Wed, 15 Jul 2020 14:36:28 -0500 Subject: [PATCH 2/2] Add test description --- tests/unit/test_response.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/unit/test_response.py b/tests/unit/test_response.py index 32cace43..dddd32e2 100644 --- a/tests/unit/test_response.py +++ b/tests/unit/test_response.py @@ -211,6 +211,12 @@ def test_endpoint_from_url(self): self.assertEqual(ret.name, "test-endpoint") def test_serialize_tag_list_order(self): + """Add tests to ensure we're preserving tag order + + This test could still give false-negatives, but making the tag list + longer helps mitigate that. + """ + test_tags = [ "one", "two",