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_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_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_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_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_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'