<s> from __future__ import unicode_literals <EOL> from expects import * <EOL> from . . _helpers import MyDict <EOL> from booby import fields , errors , models <EOL> class TestEmbeddedFieldDescriptor ( object ) : <EOL> def test_when_set_field_value_with_dict_then_value_is_embedded_object_with_dict_values ( self ) : <EOL> self . group . admin = { '<STR_LIT:name>' : '<STR_LIT:foo>' , '<STR_LIT:email>' : '<STR_LIT>' } <EOL> expect ( self . group . admin ) . to ( be_an ( User ) ) <EOL> expect ( self . group . admin ) . to ( have_properties ( <EOL> name = '<STR_LIT:foo>' , email = '<STR_LIT>' ) ) <EOL> def test_when_set_field_value_with_dict_with_invalid_field_then_raises_field_error ( self ) : <EOL> def callback ( ) : <EOL> self . group . admin = { '<STR_LIT:name>' : '<STR_LIT:foo>' , '<STR_LIT:foo>' : '<STR_LIT:bar>' } <EOL> expect ( callback ) . to ( raise_error ( errors . FieldError , '<STR_LIT:foo>' ) ) <EOL> def test_when_set_field_value_with_mutable_mapping_then_value_is_model_instance_with_dict_values ( self ) : <EOL> self . group . admin = MyDict ( name = '<STR_LIT:foo>' , email = '<STR_LIT>' ) <EOL> expect ( self . group . admin ) . to ( be_an ( User ) ) <EOL> expect ( self . group . admin ) . to ( have_properties ( <EOL> name = '<STR_LIT:foo>' , email = '<STR_LIT>' ) ) <EOL> def test_when_set_field_value_with_not_dict_object_then_value_is_given_object ( self ) : <EOL> user = User ( name = '<STR_LIT:foo>' , email = '<STR_LIT>' ) <EOL> self . group . admin = user <EOL> expect ( self . group . admin ) . to ( be ( user ) ) <EOL> def test_when_set_field_with_not_model_instance_then_value_is_given_object ( self ) : <EOL> user = object ( ) <EOL> self . group . admin = user <EOL> expect ( self . group . admin ) . to ( be ( user ) ) <EOL> def setup ( self ) : <EOL> self . group = Group ( ) <EOL> class TestEmbeddedFieldBuiltinValidators ( object ) : <EOL> def test_when_value_is_not_instance_of_model_then_raises_validation_error ( self ) : <EOL> expect ( lambda : self . field . validate ( object ( ) ) ) . to ( raise_error ( <EOL> errors . ValidationError , match ( '<STR_LIT>' ) ) ) <EOL> def test_when_embedded_model_field_has_invalid_value_then_raises_validation_error ( self ) : <EOL> expect ( lambda : self . field . validate ( User ( name = <NUM_LIT:1> ) ) ) . to ( raise_error ( <EOL> errors . ValidationError , match ( '<STR_LIT>' ) ) ) <EOL> def test_when_embedded_model_validates_then_does_not_raise ( self ) : <EOL> self . field . validate ( User ( ) ) <EOL> def setup ( self ) : <EOL> self . field = fields . Embedded ( User ) <EOL> class User ( models . Model ) : <EOL> name = fields . String ( default = '<STR_LIT>' ) <EOL> email = fields . String ( ) <EOL> class Group ( models . Model ) : <EOL> name = fields . String ( ) <EOL> admin = fields . Embedded ( User ) </s>
