-
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.
Now any object implementing the dict interface can be used
as a dict value for the Embedded field. refs #18
- Loading branch information
1 parent
7ae5184
commit 38bd6d0
Showing
2 changed files
with
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from __future__ import unicode_literals | ||
import collections | ||
|
||
from expects import expect | ||
from ._helpers import Spy, stub_validator | ||
|
@@ -97,6 +98,13 @@ def callback(): | |
|
||
expect(callback).to.raise_error(errors.FieldError, 'foo') | ||
|
||
def test_when_set_field_value_with_mutable_mapping_then_value_is_model_instance_with_dict_values(self): | ||
self.group.admin = MyDict(name='foo', email='[email protected]') | ||
|
||
expect(self.group.admin).to.be.an(User) | ||
expect(self.group.admin).to.have.properties( | ||
name='foo', email='[email protected]') | ||
|
||
def test_when_set_field_value_with_not_dict_object_then_value_is_given_object(self): | ||
user = User(name='foo', email='[email protected]') | ||
self.group.admin = user | ||
|
@@ -193,3 +201,23 @@ def test_when_embedded_model_validates_then_does_not_raise(self): | |
|
||
def setup(self): | ||
self.field = fields.Embedded(User) | ||
|
||
|
||
class MyDict(collections.MutableMapping): | ||
def __init__(self, **kwargs): | ||
self._store = kwargs | ||
|
||
def __getitem__(self, key): | ||
return self._store[key] | ||
|
||
def __setitem__(self, key, value): | ||
pass | ||
|
||
def __delitem__(self, key): | ||
pass | ||
|
||
def __len__(self): | ||
pass | ||
|
||
def __iter__(self): | ||
return iter(self._store) |