mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-07-23 09:29:14 -04:00
Raise exception when overriding a custom property
If a subclass of `JSONModel` defines a property then we shouldn’t try to override it with the value from the underlying dictionary. Rather than silently fail we should raise an exception because it will help keep our list of `ALLOWED_PROPERTIES` nice and tidy.
This commit is contained in:
@@ -13,7 +13,7 @@ class JSONModel(SerialisedModel):
|
||||
# in the case of a bad request _dict may be `None`
|
||||
self._dict = _dict or {}
|
||||
for property in self.ALLOWED_PROPERTIES:
|
||||
if property in self._dict and not hasattr(self, property):
|
||||
if property in self._dict:
|
||||
setattr(self, property, self._dict[property])
|
||||
|
||||
def __bool__(self):
|
||||
|
||||
@@ -28,7 +28,6 @@ class Job(JSONModel):
|
||||
'template_version',
|
||||
'original_file_name',
|
||||
'created_at',
|
||||
'processing_started',
|
||||
'notification_count',
|
||||
'created_by',
|
||||
'template_type',
|
||||
|
||||
@@ -11,7 +11,7 @@ def test_looks_up_from_dict():
|
||||
assert Custom({'foo': 'bar'}).foo == 'bar'
|
||||
|
||||
|
||||
def test_prefers_property_to_dict():
|
||||
def test_raises_when_overriding_custom_properties():
|
||||
|
||||
class Custom(JSONModel):
|
||||
|
||||
@@ -19,9 +19,12 @@ def test_prefers_property_to_dict():
|
||||
|
||||
@property
|
||||
def foo(self):
|
||||
return 'bar'
|
||||
pass
|
||||
|
||||
assert Custom({'foo': 'NOPE'}).foo == 'bar'
|
||||
with pytest.raises(AttributeError) as e:
|
||||
Custom({'foo': 'NOPE'})
|
||||
|
||||
assert str(e.value) == "can't set attribute"
|
||||
|
||||
|
||||
@pytest.mark.parametrize('json_response', (
|
||||
|
||||
Reference in New Issue
Block a user