async def test_validate_doc_valid(): attrs = { 'attr_str': ATTR.STR(), 'attr_int': ATTR.INT(), } doc = {'attr_str': 'str', 'attr_int': '42'} await utils.validate_doc(mode='create', doc=doc, attrs=attrs) assert doc == {'attr_str': 'str', 'attr_int': 42}
async def test_validate_doc_allow_update_valid_none(): attrs = { 'attr_str': ATTR.STR(), 'attr_int': ATTR.INT(), } doc = {'attr_str': 'str', 'attr_int': None} await utils.validate_doc(doc=doc, attrs=attrs, mode='update') assert doc == {'attr_str': 'str', 'attr_int': None}
async def test_validate_attr_DICT_None_allow_none(): attr_val = await validate_attr( attr_name='test_validate_attr_DICT', attr_type=ATTR.KV_DICT(key=ATTR.STR(), val=ATTR.INT()), attr_val=None, mode='update', ) assert attr_val == None
async def test_validate_doc_invalid_none(): attrs = { 'attr_str': ATTR.STR(), 'attr_int': ATTR.INT(), } doc = {'attr_str': 'str', 'attr_int': None} with pytest.raises(MissingAttrException): await utils.validate_doc(mode='create', doc=doc, attrs=attrs)
async def test_validate_attr_DICT_None(): with pytest.raises(InvalidAttrException): await validate_attr( attr_name='test_validate_attr_DICT', attr_type=ATTR.KV_DICT(key=ATTR.STR(), val=ATTR.ANY()), attr_val=None, mode='create', )
async def test_validate_attr_UNION_None(): with pytest.raises(InvalidAttrException): await validate_attr( attr_name='test_validate_attr_UNION', attr_type=ATTR.UNION(union=[ATTR.STR(), ATTR.INT()]), attr_val=None, mode='create', )
async def test_validate_attr_DICT_None_allow_none(): attr_val = await validate_attr( attr_name='test_validate_attr_DICT', attr_type=ATTR.TYPED_DICT(dict={'key': ATTR.STR()}), attr_val=None, mode='update', ) assert attr_val == None
async def test_validate_attr_DICT_min_req_max_dict(): attr_val = await validate_attr( attr_name='test_validate_attr_DICT', attr_type=ATTR.KV_DICT(key=ATTR.STR(), val=ATTR.INT(), min=3, max=4, req=['key3']), attr_val={'key1': '4', 'key2': 3, 'key3': 0, 'key4': 5}, mode='create', ) assert attr_val == {'key1': 4, 'key2': 3, 'key3': 0, 'key4': 5}
async def test_validate_attr_LIST_None(): with pytest.raises(InvalidAttrException): await validate_attr( attr_name='test_validate_attr_LIST', attr_type=ATTR.LIST(list=[ATTR.STR()]), attr_val=None, mode='create', )
async def test_validate_attr_UNION_int(): attr_val = await validate_attr( attr_name='test_validate_attr_UNION', attr_type=ATTR.UNION(union=[ATTR.STR(), ATTR.INT()]), attr_val=1, mode='create', ) assert attr_val == 1
async def test_validate_attr_LIST_nested_list_invalid(): with pytest.raises(InvalidAttrException): await validate_attr( attr_name='test_validate_attr_LIST', attr_type=ATTR.LIST(list=[ATTR.LIST(list=[ATTR.STR()])]), attr_val=['str', 'str', ['str']], mode='create', )
async def test_validate_attr_UNION_None_allow_none(): attr_val = await validate_attr( attr_name='test_validate_attr_UNION', attr_type=ATTR.UNION(union=[ATTR.STR(), ATTR.INT()]), attr_val=None, mode='update', ) assert attr_val == None
async def test_validate_attr_DICT_None(): with pytest.raises(InvalidAttrException): await validate_attr( attr_name='test_validate_attr_DICT', attr_type=ATTR.TYPED_DICT(dict={'key': ATTR.STR()}), attr_val=None, mode='create', )
async def test_validate_attr_DICT_req_dict(): attr_val = await validate_attr( attr_name='test_validate_attr_DICT', attr_type=ATTR.KV_DICT(key=ATTR.STR(), val=ATTR.INT(), req=['key3']), attr_val={'key1': '4', 'key2': 1, 'key3': 0}, mode='create', ) assert attr_val == {'key1': 4, 'key2': 1, 'key3': 0}
async def test_validate_attr_DICT_min_req_max_dict_invalid(): with pytest.raises(InvalidAttrException): await validate_attr( attr_name='test_validate_attr_DICT', attr_type=ATTR.KV_DICT(key=ATTR.STR(), val=ATTR.INT(), min=3, max=4, req=['key3']), attr_val={'key1': '4', 'key2': 3, 'key3': 0, 'key4': 5, 'key5': 2}, mode='create', )
async def test_validate_attr_LIST_None_allow_none(): attr_val = await validate_attr( attr_name='test_validate_attr_LIST', attr_type=ATTR.LIST(list=[ATTR.STR()]), attr_val=None, mode='update', ) assert attr_val == None
async def test_validate_attr_LIST_muti_list_invalid(): with pytest.raises(InvalidAttrException): await validate_attr( attr_name='test_validate_attr_LIST', attr_type=ATTR.LIST( list=[ATTR.EMAIL(), ATTR.URI_WEB()]), attr_val=['*****@*****.**', 'http://sub.example.com', '1'], mode='create', )
def test_generate_attr_TYPED_DICT(): attr_val = utils.generate_attr(attr_type=ATTR.TYPED_DICT(dict={ 'foo': ATTR.INT(), 'bar': ATTR.STR(), })) assert len(attr_val.keys()) == 2 assert set(attr_val.keys()) == {'foo', 'bar'} assert type(attr_val['foo']) == int assert type(attr_val['bar']) == str
async def test_validate_attr_LIST_nested_list(): list_attr_val = [['str'], ['str', 'str'], ['str']] attr_val = await validate_attr( attr_name='test_validate_attr_LIST', attr_type=ATTR.LIST(list=[ATTR.LIST(list=[ATTR.STR()])]), attr_val=list_attr_val, mode='create', ) assert attr_val == list_attr_val
def test_update_attr_values_default_dict(): attr = ATTR.TYPED_DICT(dict={'key': ATTR.STR()}) _update_attr_values( attr=ATTR.TYPED_DICT(dict={'attr': attr}), value='default', value_path='attr.key', value_val='test_update_attr_values', ) assert attr._args['dict']['key']._default == 'test_update_attr_values'
def test_update_attr_values_default_list(): attr = ATTR.LIST(list=[ATTR.STR()]) _update_attr_values( attr=ATTR.TYPED_DICT(dict={'attr': attr}), value='default', value_path='attr:0', value_val='test_update_attr_values', ) assert attr._args['list'][0]._default == 'test_update_attr_values'
async def test_validate_attr_DICT_nested_list_dict_invalid(): with pytest.raises(InvalidAttrException): await validate_attr( attr_name='test_validate_attr_DICT', attr_type=ATTR.KV_DICT(key=ATTR.STR(), val=ATTR.LIST(list=[ATTR.INT()])), attr_val={ 'key1': ['a'], }, mode='create', )
async def test_validate_attr_UNION_default_float_allow_none(): attr_type = ATTR.UNION(union=[ATTR.STR(), ATTR.INT()]) attr_type._default = 'test_validate_attr_UNION' attr_val = await validate_attr( attr_name='test_validate_attr_UNION', attr_type=attr_type, attr_val=1.1, mode='update', ) assert attr_val == None
async def test_validate_attr_DICT_default_int_allow_none(): attr_type = ATTR.KV_DICT(key=ATTR.STR(), val=ATTR.INT()) attr_type._default = 'test_validate_attr_DICT' attr_val = await validate_attr( attr_name='test_validate_attr_DICT', attr_type=attr_type, attr_val=1, mode='update', ) assert attr_val == None
async def test_validate_doc_allow_update_list_int_str(preserve_state): with preserve_state(config, 'Config'): config.Config.locales = ['ar_AE', 'en_AE'] config.Config.locale = 'ar_AE' attrs = { 'attr_list_int': ATTR.LIST(list=[ATTR.INT()]), } doc = {'attr_list_int': {'$append': '1'}} await utils.validate_doc(doc=doc, attrs=attrs, mode='update') assert doc == {'attr_list_int': {'$append': 1, '$unique': False}}
async def test_validate_doc_allow_update_kv_dict_typed_dict_time_dict_dot_notated(): attrs = { 'shift': ATTR.KV_DICT( key=ATTR.STR(pattern=r'[0-9]{2}'), val=ATTR.TYPED_DICT(dict={'start': ATTR.TIME(), 'end': ATTR.TIME()}), ) } doc = {'shift.01.start': '09:00'} await utils.validate_doc(doc=doc, attrs=attrs, mode='update') assert doc == {'shift.01.start': '09:00'}
async def test_validate_attr_LIST_default_int(): attr_type = ATTR.LIST(list=[ATTR.STR()]) attr_type._default = 'test_validate_attr_LIST' attr_val = await validate_attr( attr_name='test_validate_attr_LIST', attr_type=attr_type, attr_val=[1], mode='create', ) assert attr_val == 'test_validate_attr_LIST'
async def test_validate_attr_LIST_nested_default_int(): attr_type = ATTR.LIST(list=[ATTR.STR()]) attr_type._args['list'][0]._default = 'test_validate_attr_LIST' with pytest.raises(InvalidAttrException): await validate_attr( attr_name='test_validate_attr_LIST', attr_type=attr_type, attr_val=[1], mode='create', )
async def test_validate_attr_LIST_nested_default_int_allow_none(): attr_type = ATTR.LIST(list=[ATTR.STR()]) attr_type._args['list'][0]._default = 'test_validate_attr_LIST' attr_val = await validate_attr( attr_name='test_validate_attr_LIST', attr_type=attr_type, attr_val=[1], mode='update', ) assert attr_val == None
async def test_validate_attr_DICT_default_int_allow_none(): attr_type = ATTR.TYPED_DICT(dict={'key': ATTR.STR()}) attr_type._default = 'test_validate_attr_DICT' attr_val = await validate_attr( attr_name='test_validate_attr_DICT', attr_type=attr_type, attr_val=1, mode='update', ) assert attr_val == None