def test_field_custom_source_names_in_container(): f1 = Field(name='f1', source_names=['f1_source']) assert not hasattr(f1, 'source_name') assert f1.source_names == ['f1_source'] c1 = {'f1': 'f1_value'} c2 = {'f1_source': 'f1_value'} assert not f1.has_value_in(c1) assert f1.get_value_in(c1) is None assert f1.has_value_in(c2) assert f1.get_value_in(c2) == 'f1_value'
def test_field_in_container(): f1 = Field(name='f1') c1 = {'f1': 'f1_value'} c2 = {'f2': 'f2_value'} assert f1.has_value_in(c1) assert not f1.has_value_in(c2) assert f1.get_value_in(c1) == 'f1_value' assert f1.get_value_in(c2) is None assert f1.get_value_in(c2, 'default') == 'default' f1.set_value_in(c2, 'new_value') assert c2['f1'] == 'new_value'
def test_nested_field(): person_schema = Schema( Field('first_name', default=None), Field('last_name', default=None), ) director = Field(name='director', mapping=person_schema, nullable=True) assert director.has_value_in({'director': {}}) assert director.get_value_in({'director': {}}) == {} assert director.get_value_in({'director': {'first_name': 'John', 'last_name': 'Smith'}}) == { 'first_name': 'John', 'last_name': 'Smith', } assert Schema(director).load({'director': {}}) == {'director': {'first_name': None, 'last_name': None}} assert Schema(director).load({'director': None}) == {'director': None}