Browse Source

Standardize serialization around db.Model and adjust raised errors

merge-requests/1/head
Drew Short 7 years ago
parent
commit
cfa367a9d4
  1. 6
      server/atheneum/service/transformation_service.py
  2. 4
      server/tests/service/test_transformation_service.py

6
server/atheneum/service/transformation_service.py

@ -77,7 +77,7 @@ def register_transformer(
if model_name not in _model_transformers: if model_name not in _model_transformers:
_model_transformers[model_name] = model_serializer _model_transformers[model_name] = model_serializer
else: else:
raise ValueError(
raise KeyError(
' '.join([ ' '.join([
'A transformer for type "{}" already exists with class "{}".', 'A transformer for type "{}" already exists with class "{}".',
'Cannot register a new transformer with class "{}"' 'Cannot register a new transformer with class "{}"'
@ -100,12 +100,12 @@ def serialize_model(model_obj: db.Model,
def deserialize_model( def deserialize_model(
model_type: str,
model_type: Type[db.Model],
json_model_object: dict, json_model_object: dict,
options: Optional[List[str]] = None) -> db.Model: options: Optional[List[str]] = None) -> db.Model:
"""Lookup a Model and hand it off to the deserializer.""" """Lookup a Model and hand it off to the deserializer."""
try: try:
transformer = _model_transformers[model_type]
transformer = _model_transformers[model_type.__name__]
return transformer( return transformer(
transformer.type()).deserialize(json_model_object, options) transformer.type()).deserialize(json_model_object, options)
except KeyError: except KeyError:

4
server/tests/service/test_transformation_service.py

@ -24,7 +24,7 @@ def test_deserialize_model():
user_token_json = { user_token_json = {
'token': 'test' 'token': 'test'
} }
result = deserialize_model('UserToken', user_token_json)
result = deserialize_model(UserToken, user_token_json)
assert result is not None assert result is not None
assert result.token == 'test' assert result.token == 'test'
@ -40,7 +40,7 @@ def test_registering_two_transformers_of_the_same_type():
pass pass
error = None error = None
with pytest.raises(ValueError) as e_info:
with pytest.raises(KeyError) as e_info:
error = e_info error = e_info
register_transformer(BadTransformer) register_transformer(BadTransformer)

Loading…
Cancel
Save