async def test_validate_attr_INT_float_as_str(): with pytest.raises(InvalidAttrException): await validate_attr( attr_name='test_validate_attr_INT', attr_type=ATTR.INT(), attr_val='1.1', 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_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_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_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_INT_None_allow_none(): attr_val = await validate_attr( attr_name='test_validate_attr_INT', attr_type=ATTR.INT(), attr_val=None, mode='update', ) assert attr_val == None
async def test_validate_attr_INT_int_as_str(): attr_val = await validate_attr( attr_name='test_validate_attr_INT', attr_type=ATTR.INT(), attr_val='1', mode='create', ) assert attr_val == 1
async def test_validate_attr_INT_range_int_invalid(): with pytest.raises(InvalidAttrException): await validate_attr( attr_name='test_validate_attr_INT', attr_type=ATTR.INT(ranges=[[0, 10]]), attr_val=10, mode='create', )
async def test_validate_attr_INT_range_int_as_str(): attr_val = await validate_attr( attr_name='test_validate_attr_INT', attr_type=ATTR.INT(ranges=[[0, 10]]), attr_val='0', mode='create', ) assert attr_val == 0
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_INT_None(): with pytest.raises(InvalidAttrException): await validate_attr( attr_name='test_validate_attr_INT', attr_type=ATTR.INT(), attr_val=None, mode='create', )
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_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_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_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_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}
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_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_INT_default_int_allow_none(): attr_type = ATTR.INT() attr_type._default = 'test_validate_attr_INT' attr_val = await validate_attr( attr_name='test_validate_attr_INT', attr_type=attr_type, attr_val='str', mode='update', ) assert attr_val == None
async def test_validate_attr_INT_default_str(): attr_type = ATTR.INT() attr_type._default = 'test_validate_attr_INT' attr_val = await validate_attr( attr_name='test_validate_attr_INT', attr_type=attr_type, attr_val='str', mode='create', ) assert attr_val == 'test_validate_attr_INT'
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}}
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_UNION_default_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=None, mode='create', ) assert attr_val == 'test_validate_attr_UNION'
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_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_attr_DICT_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()), attr_val={ 'key': 'value', 'key2': 2, }, mode='create', )
async def test_validate_attr_DICT_nested_list_dict(): attr_val = 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': ['4'], 'key2': [1, '2', 3]}, mode='create', ) assert attr_val == { 'key1': [4], 'key2': [1, 2, 3], }
async def test_validate_attr_DICT_simple_dict(): dict_attr_val = { 'key1': 'value', 'key2': 2, } attr_val = await validate_attr( attr_name='test_validate_attr_DICT', attr_type=ATTR.TYPED_DICT(dict={'key1': ATTR.STR(), 'key2': ATTR.INT()}), attr_val=dict_attr_val, mode='create', ) assert attr_val == dict_attr_val
async def test_validate_attr_LIST_nested_dict_invalid(): with pytest.raises(InvalidAttrException): await validate_attr( attr_name='test_validate_attr_LIST', attr_type=ATTR.LIST( list=[ATTR.KV_DICT(key=ATTR.STR(), val=ATTR.INT())]), attr_val=[{ 'key': 1 }, { 'key': 'val' }], mode='create', )
async def test_validate_attr_LIST_nested_dict(): attr_val = await validate_attr( attr_name='test_validate_attr_LIST', attr_type=ATTR.LIST( list=[ATTR.KV_DICT(key=ATTR.STR(), val=ATTR.INT())]), attr_val=[{ 'key': 1 }, { 'key': '2' }], mode='create', ) assert attr_val == [{'key': 1}, {'key': 2}]