mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-07-17 21:20:32 -04:00
Remove custom getattr implementation from JSONModel
We wrote custom `__getattr__` and `__getitem__` implementation to make
it easier to find code that was accessing fields in the dictionary that
we weren’t aware of. See this commit for details:
b48305c50d
Now that no view-layer code accesses the service dictionary directly we
don’t need to support this behaviour any more. By removing it we can
make the model code simpler, and closer to the `SerialisedModel` it
inherits from.
It still needs some custom implementation because:
- a lot of our test fixtures are lazy and don’t set up all the expected
fields, so we need to account for fields sometimes being present in
the underlying dictionary and sometimes not
- we often implement a property that has the same name as one of the
fields in the JSON, so we have to be careful not to try to override
this property with the value from the underlying JSON
This commit is contained in:
@@ -39,10 +39,7 @@ def test_model_raises_for_unknown_attributes(json_response):
|
||||
with pytest.raises(AttributeError) as e:
|
||||
model.foo
|
||||
|
||||
assert str(e.value) == (
|
||||
"'Custom' object has no attribute 'foo' and 'foo' is not a "
|
||||
"field in the underlying JSON"
|
||||
)
|
||||
assert str(e.value) == "'Custom' object has no attribute 'foo'"
|
||||
|
||||
|
||||
def test_model_raises_keyerror_if_item_missing_from_dict():
|
||||
@@ -50,10 +47,10 @@ def test_model_raises_keyerror_if_item_missing_from_dict():
|
||||
class Custom(JSONModel):
|
||||
ALLOWED_PROPERTIES = {'foo'}
|
||||
|
||||
with pytest.raises(KeyError) as e:
|
||||
with pytest.raises(AttributeError) as e:
|
||||
Custom({}).foo
|
||||
|
||||
assert str(e.value) == "'foo'"
|
||||
assert str(e.value) == "'Custom' object has no attribute 'foo'"
|
||||
|
||||
|
||||
@pytest.mark.parametrize('json_response', (
|
||||
@@ -79,4 +76,6 @@ def test_dynamic_properties_are_introspectable():
|
||||
class Custom(JSONModel):
|
||||
ALLOWED_PROPERTIES = {'foo', 'bar', 'baz'}
|
||||
|
||||
assert dir(Custom({}))[-3:] == ['bar', 'baz', 'foo']
|
||||
model = Custom({'foo': None, 'bar': None, 'baz': None})
|
||||
|
||||
assert dir(model)[-3:] == ['bar', 'baz', 'foo']
|
||||
|
||||
Reference in New Issue
Block a user