Add custom JSON encoder to handle attachment objects
This commit is contained in:
parent
f553c9a9ea
commit
e51a9012fd
9
atst/utils/json.py
Normal file
9
atst/utils/json.py
Normal file
@ -0,0 +1,9 @@
|
||||
from flask.json import JSONEncoder
|
||||
from atst.models.attachment import Attachment
|
||||
|
||||
|
||||
class CustomJSONEncoder(JSONEncoder):
|
||||
def default(self, obj):
|
||||
if isinstance(obj, Attachment):
|
||||
return obj.filename
|
||||
return JSONEncoder.default(self, obj)
|
15
tests/utils/test_json.py
Normal file
15
tests/utils/test_json.py
Normal file
@ -0,0 +1,15 @@
|
||||
import json
|
||||
from atst.utils.json import CustomJSONEncoder
|
||||
|
||||
from tests.factories import AttachmentFactory
|
||||
|
||||
|
||||
encoder = CustomJSONEncoder()
|
||||
|
||||
|
||||
def test_custom_encoder_serializes_attachments():
|
||||
filename = "jar_jar_is_secretly_a_sith_lord.pdf"
|
||||
attachment = AttachmentFactory.create(filename=filename)
|
||||
encoded = encoder.encode({"file": attachment})
|
||||
expected = json.dumps({"file": filename})
|
||||
assert encoded == expected
|
Loading…
x
Reference in New Issue
Block a user