-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The Model class now has a deserialize classmethod
and serialize method supporting field names mapping. refs #16
- Loading branch information
1 parent
8673049
commit 0be86c5
Showing
2 changed files
with
133 additions
and
0 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
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 |
---|---|---|
|
@@ -209,6 +209,82 @@ def setup(self): | |
self.user = User(name='Jack', email='[email protected]') | ||
|
||
|
||
class TestDeserializeModel(object): | ||
def test_should_return_dict_with_model_fields(self): | ||
result = User.deserialize({ | ||
'name': 'Jack', 'email': '[email protected]'}) | ||
|
||
expect(result).to.have.keys(name='Jack', email='[email protected]') | ||
|
||
def test_should_return_dict_with_model_mapped_fields(self): | ||
result = UserWithMappedFields.deserialize({ | ||
'username': 'Jack', 'emailAddress': '[email protected]'}) | ||
|
||
expect(result).to.have.keys(name='Jack', email='[email protected]') | ||
|
||
def test_should_deserialize_embedded_model_if_field_is_embedded_field(self): | ||
result = UserWithTokenAndMappedFields.deserialize({ | ||
'name': 'Jack', 'email': '[email protected]', | ||
'token': { | ||
'accessToken':'foo', 'secretToken': 'bar' | ||
} | ||
}) | ||
|
||
expect(result['token']).to.have.keys(key='foo', secret='bar') | ||
|
||
|
||
class TestSerializeModel(object): | ||
def test_should_return_dict_with_model_fields(self): | ||
user = User(name='Jack', email='[email protected]') | ||
|
||
result = user.serialize() | ||
|
||
expect(result).to.have.keys(name='Jack', email='[email protected]') | ||
|
||
def test_should_return_dict_with_model_mapped_fields(self): | ||
user = UserWithMappedFields(name='Jack', email='[email protected]') | ||
|
||
result = user.serialize() | ||
|
||
expect(result).to.have.keys(username='Jack', | ||
emailAddress='[email protected]') | ||
|
||
def test_should_serialize_embedded_model(self): | ||
user = UserWithTokenAndMappedFields( | ||
name='Jack', email='[email protected]', | ||
token={'key':'foo', 'secret': 'bar'}) | ||
|
||
result = user.serialize() | ||
|
||
expect(result['token']).to.have.keys(accessToken='foo', | ||
secretToken='bar') | ||
|
||
def test_should_serialize_list_of_models(self): | ||
user = UserWithList( | ||
name='Jack', email='[email protected]', | ||
tokens=[self.token1, self.token2]) | ||
|
||
result = user.serialize() | ||
|
||
expect(result['tokens']).to.equal([self.token1.serialize(), | ||
self.token2.serialize()]) | ||
|
||
def test_should_serialize_list_of_models_and_values(self): | ||
user = UserWithList( | ||
name='Jack', email='[email protected]', | ||
tokens=[self.token1, 'foo', self.token2]) | ||
|
||
result = user.serialize() | ||
|
||
expect(result['tokens']).to.equal([self.token1.serialize(), 'foo', | ||
self.token2.serialize()]) | ||
|
||
def setup(self): | ||
self.token1 = TokenWithMappedFields(key='foo', secret='bar') | ||
self.token2 = TokenWithMappedFields(key='fuu', secret='baz') | ||
|
||
|
||
|
||
class User(models.Model): | ||
name = fields.String() | ||
email = fields.String() | ||
|
@@ -242,3 +318,17 @@ class UserWithList(User): | |
class Token(models.Model): | ||
key = fields.String() | ||
secret = fields.String() | ||
|
||
|
||
class UserWithMappedFields(models.Model): | ||
name = fields.String(name='username') | ||
email = fields.String(name='emailAddress') | ||
|
||
|
||
class TokenWithMappedFields(models.Model): | ||
key = fields.String(name='accessToken') | ||
secret = fields.String(name='secretToken') | ||
|
||
|
||
class UserWithTokenAndMappedFields(User): | ||
token = fields.Embedded(TokenWithMappedFields) |