def test_from_dict_bounded(self): # bounded datatypes should only be float or int with pytest.raises(ValueError): UDSDataType.from_dict({ 'datatype': 'str', 'categories': ['yes', 'maybe', 'no'], 'ordered': True, 'lower_bound': 'no', 'upper_bound': 'yes' }) # the the datatype is categorical, the lower bound should # match the category lower bound with pytest.raises(ValueError): UDSDataType.from_dict({ 'datatype': 'int', 'categories': [1, 2, 3, 4, 5], 'ordered': True, 'lower_bound': 2, 'upper_bound': 5 }) # these are good for c, _ in self.cases: UDSDataType.from_dict(c)
def test_from_dict_categorical(self): # the name for the categories key is "categories" with pytest.raises(KeyError): UDSDataType.from_dict({ 'datatype': 'int', 'category': [1, 2, 3, 4, 5], 'ordered': True }) # floats cannot be categorical with pytest.raises(ValueError): UDSDataType.from_dict({ 'datatype': 'float', 'categories': [1, 2, 3, 4, 5], 'ordered': True }) # bounds can only be specified if ordered is not specified or # is True with pytest.raises(ValueError): UDSDataType.from_dict({ 'datatype': 'str', 'categories': ["no", "maybe", "yes"], 'ordered': False, 'lower_bound': "no", 'upper_bound': "yes" }) # these are good for t, c in self.catdict.items(): for o in [True, False]: dt = UDSDataType.from_dict({ 'datatype': t, 'categories': c, 'ordered': o }) assert dt.is_categorical assert dt.is_ordered_categorical == o if o: assert dt.categories == c else: assert dt.categories == set(c)
def test_from_dict_simple(self): UDSDataType.from_dict({'datatype': 'str'}) UDSDataType.from_dict({'datatype': 'int'}) UDSDataType.from_dict({'datatype': 'bool'}) UDSDataType.from_dict({'datatype': 'float'})
def test_init_categorical(self): for t, c in self.catdict.items(): for o in [True, False]: t = int if t == 'int' else str UDSDataType(datatype=t, categories=c, ordered=o)
def test_init_simple(self): UDSDataType(datatype=str) UDSDataType(datatype=int) UDSDataType(datatype=bool) UDSDataType(datatype=float)
def test_eq(self): for c_in, c_out in self.cases: loaded1 = UDSDataType.from_dict(c_in) loaded2 = UDSDataType.from_dict(c_out) assert loaded1 == loaded2
def test_to_dict(self): for c_in, c_out in self.cases: loaded = UDSDataType.from_dict(c_in) assert loaded.to_dict() == c_out