From f8b523b97b347f59abb5701d4c98a4abf4b50bba Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 25 Nov 2020 11:44:44 -0500 Subject: [PATCH] Extend assertInstanceEqual to accept a list of data fields to exclude from comparison with the instance --- netbox/utilities/testing/api.py | 11 ++++++++++- netbox/utilities/testing/views.py | 19 ++++++++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/netbox/utilities/testing/api.py b/netbox/utilities/testing/api.py index 2f3e2aa19..856af8ea1 100644 --- a/netbox/utilities/testing/api.py +++ b/netbox/utilities/testing/api.py @@ -174,6 +174,7 @@ class APIViewTestCases: class CreateObjectViewTestCase(APITestCase): create_data = [] + validation_excluded_fields = [] def test_create_object_without_permission(self): """ @@ -205,6 +206,7 @@ class APIViewTestCases: self.assertInstanceEqual( self._get_queryset().get(pk=response.data['id']), self.create_data[0], + exclude=self.validation_excluded_fields, api=True ) @@ -229,11 +231,13 @@ class APIViewTestCases: self.assertInstanceEqual( self._get_queryset().get(pk=obj['id']), self.create_data[i], + exclude=self.validation_excluded_fields, api=True ) class UpdateObjectViewTestCase(APITestCase): update_data = {} + validation_excluded_fields = [] def test_update_object_without_permission(self): """ @@ -266,7 +270,12 @@ class APIViewTestCases: response = self.client.patch(url, update_data, format='json', **self.header) self.assertHttpStatus(response, status.HTTP_200_OK) instance.refresh_from_db() - self.assertInstanceEqual(instance, update_data, api=True) + self.assertInstanceEqual( + instance, + update_data, + exclude=self.validation_excluded_fields, + api=True + ) class DeleteObjectViewTestCase(APITestCase): diff --git a/netbox/utilities/testing/views.py b/netbox/utilities/testing/views.py index afbcbc057..d19848985 100644 --- a/netbox/utilities/testing/views.py +++ b/netbox/utilities/testing/views.py @@ -126,20 +126,25 @@ class TestCase(_TestCase): err_message = f"Expected HTTP status {expected_status}; received {response.status_code}: {err}" self.assertEqual(response.status_code, expected_status, err_message) - def assertInstanceEqual(self, instance, data, api=False): + def assertInstanceEqual(self, instance, data, exclude=None, api=False): """ Compare a model instance to a dictionary, checking that its attribute values match those specified in the dictionary. - :instance: Python object instance - :data: Dictionary of test data used to define the instance - :api: Set to True is the data is a JSON representation of the instance + :param instance: Python object instance + :param data: Dictionary of test data used to define the instance + :param exclude: List of fields to exclude from comparison (e.g. passwords, which get hashed) + :param api: Set to True is the data is a JSON representation of the instance """ - model_dict = self.model_to_dict(instance, fields=data.keys(), api=api) + if exclude is None: + exclude = [] - # Omit any dictionary keys which are not instance attributes + fields = [k for k in data.keys() if k not in exclude] + model_dict = self.model_to_dict(instance, fields=fields, api=api) + + # Omit any dictionary keys which are not instance attributes or have been excluded relevant_data = { - k: v for k, v in data.items() if hasattr(instance, k) + k: v for k, v in data.items() if hasattr(instance, k) and k not in exclude } self.assertDictEqual(model_dict, relevant_data)