forked from jfinkels/flask-restless
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jfinkels#679 - write tests to verify presence of enum-field custom se…
…rialization issue#679 - add dependencies for enum, marshmallow_enum serialization - create marshmallow-based custom de/serialization to demonstrate the issue - add models, schema, serializers and a test case for updating enum fields. the test case fails as expected - todo: apply a fix to the issue
- Loading branch information
Kiptoo Magutt
committed
Feb 2, 2018
1 parent
fbff01f
commit 074a911
Showing
3 changed files
with
131 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
-r install.txt | ||
enum34 | ||
marshmallow_enum | ||
unittest2 |
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,52 @@ | ||
# serializers.py - custom serializers for unit tests | ||
# using the marshmallow de/serialization library | ||
# | ||
# .._Marshmallow: http://marshmallow.readthedocs.io/en/latest/index.html | ||
# | ||
# Copyright 2018 Kiptoo Magutt <[email protected]>. | ||
# Copyright 2012, 2013, 2014, 2015, 2016 Jeffrey Finkelstein | ||
# <[email protected]> and contributors. | ||
# | ||
# This file is part of Flask-Restless. | ||
# | ||
# Flask-Restless is distributed under both the GNU Affero General Public | ||
# License version 3 and under the 3-clause BSD license. For more | ||
# information, see LICENSE.AGPL and LICENSE.BSD. | ||
"""Helper functions for unit tests.""" | ||
|
||
from flask_restless import DefaultSerializer | ||
from flask_restless import DefaultDeserializer | ||
|
||
|
||
class MarshmallowSerializer(DefaultSerializer): | ||
""" | ||
Base class for models that need custom serializers | ||
using the marshmallow library | ||
See | ||
:class:`TestUpdating.TestSupport.AddressSchema` | ||
for example usage | ||
""" | ||
schema_class = None | ||
|
||
def serialize(self, instance, only=None): | ||
schema = self.schema_class(only=only) | ||
return schema.dump(instance).data | ||
|
||
def serialize_many(self, instances, only=None): | ||
schema = self.schema_class(many=True, only=only) | ||
return schema.dump(instances).data | ||
|
||
|
||
class MarshmallowDeserializer(DefaultDeserializer): | ||
|
||
schema_class = None | ||
|
||
def deserialize(self, document): | ||
schema = self.schema_class() | ||
return schema.load(document).data | ||
|
||
def deserialize_many(self, document): | ||
schema = self.schema_class(many=True) | ||
return schema.load(document).data |
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