-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
let the user choose a custom json serializer through the RESTPLUS_JSO…
…N_SERIALIZER config option
- Loading branch information
Showing
5 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ pytest-mock==1.6.3 | |
pytest-profiling==1.2.11 | ||
pytest-sugar==0.9.0 | ||
tzlocal | ||
ujson |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import flask_restplus.representations as rep | ||
|
||
from json import dumps, loads | ||
from ujson import dumps as udumps, loads as uloads | ||
|
||
payload = { | ||
'id': 1, | ||
'name': 'toto', | ||
'address': 'test', | ||
} | ||
|
||
|
||
def test_representations_serialization_output_correct(app): | ||
r = rep.output_json(payload, 200) | ||
assert loads(r.get_data(True)) == loads(dumps(payload)) | ||
|
||
|
||
def test_config_custom_serializer_is_module(app): | ||
# now reset serializer | ||
rep.serializer = None | ||
# then enforce a custom serializer | ||
app.config['RESTPLUS_JSON_SERIALIZER'] = 'ujson' | ||
r2 = rep.output_json(payload, 200) | ||
assert uloads(r2.get_data(True)) == uloads(udumps(payload)) | ||
assert rep.serializer == udumps | ||
|
||
|
||
def test_config_custom_serializer_is_function(app): | ||
# test other config syntax | ||
rep.serializer = None | ||
app.config['RESTPLUS_JSON_SERIALIZER'] = 'ujson.dumps' | ||
rep.output_json(payload, 200) | ||
assert rep.serializer == udumps | ||
|
||
|
||
def test_config_custom_serializer_fallback(app): | ||
# test fallback | ||
rep.serializer = None | ||
app.config['RESTPLUS_JSON_SERIALIZER'] = 'ujson.lol.dumps' | ||
rep.output_json(payload, 200) | ||
assert rep.serializer == dumps |