def test_generate_attr_UNION(): attr_val1 = utils.generate_attr(attr_type=ATTR.UNION( union=[ATTR.STR(), ATTR.INT()])) attr_val2 = utils.generate_attr(attr_type=ATTR.UNION(union=[ ATTR.LIST(list=[ATTR.STR()], min=1), ATTR.TYPED_DICT(dict={'foo': ATTR.FLOAT()}) ])) assert type(attr_val1) in [str, int] assert (type(attr_val2) == list and type(attr_val2[0]) == str) or ( type(attr_val2) == dict and type(attr_val2['foo']) == float)
async def test_validate_attr_DICT_nested_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.KV_DICT(key=ATTR.STR(), val=ATTR.INT()) ), attr_val={ 'key1': 'value', 'key2': 2, }, mode='create', )
async def test_validate_attr_DICT_nested_dict(): dict_attr_val = { 'key1': {'child_key': 1}, 'key2': {'child_key': 2}, } attr_val = await validate_attr( attr_name='test_validate_attr_DICT', attr_type=ATTR.KV_DICT( key=ATTR.STR(), val=ATTR.KV_DICT(key=ATTR.STR(), val=ATTR.INT()) ), attr_val=dict_attr_val, mode='create', ) assert attr_val == dict_attr_val
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_STR_str(): attr_val = await validate_attr( attr_name='test_validate_attr_STR', attr_type=ATTR.STR(), attr_val='test_validate_attr_STR', mode='create', ) assert attr_val == 'test_validate_attr_STR'
async def test_validate_attr_STR_None(): with pytest.raises(InvalidAttrException): await validate_attr( attr_name='test_validate_attr_STR', attr_type=ATTR.STR(), attr_val=None, 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_STR_pattern_str(): attr_val = await validate_attr( attr_name='test_validate_attr_STR', attr_type=ATTR.STR(pattern=r'[a-zA-Z_]+'), attr_val='test_validate_attr_STR', mode='create', ) assert attr_val == 'test_validate_attr_STR'
async def test_validate_attr_STR_None_allow_none(): attr_val = await validate_attr( attr_name='test_validate_attr_STR', attr_type=ATTR.STR(), attr_val=None, mode='update', ) assert attr_val == None
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_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_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(): 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_LIST_int(): with pytest.raises(InvalidAttrException): await validate_attr( attr_name='test_validate_attr_LIST', attr_type=ATTR.LIST(list=[ATTR.STR()]), attr_val=1, 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_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_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_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_STR_pattern_str_invalid(): with pytest.raises(InvalidAttrException): await validate_attr( attr_name='test_validate_attr_STR', attr_type=ATTR.STR(pattern=r'[a-z_]+'), attr_val='test_validate_attr_STR', mode='create', )
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_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_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_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_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_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
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'
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'
async def test_validate_attr_BOOL_default_int_allow_none(): attr_type = ATTR.STR() attr_type._default = 'test_validate_attr_BOOL' attr_val = await validate_attr( attr_name='test_validate_attr_BOOL', attr_type=attr_type, attr_val=1, mode='update', ) assert attr_val == None
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', )