def test_validator_parameters_exist(params): config = { "penalty": Union(Enum("l1", "l2", "elasticnet", "none")), "dual": TypeOf(bool), "tol": Interval(float, 0.0, None, lower_inclusive=False), "solver": Enum("newton-cg", "lbfgs", "liblinear", "sag", "saga") } # does not raise validate_parameters(config, params)
def test_validator_parameters_validate_error(params): config = { "penalty": Union(Enum("l1", "l2", "elasticnet", "none")), "dual": TypeOf(bool), "tol": Interval(float, 0.0, None, lower_inclusive=False), "solver": Enum("newton-cg", "lbfgs", "liblinear", "sag", "saga"), "warm_start": TypeOf(bool), "n_jobs": Union(Interval(int, 1, None), Const(-1), Const(None)) } with pytest.raises(Exception): validate_parameters(config, params)
def test_validator_parameters_not_exist(params, error_key): config = { "penalty": Union(Enum("l1", "l2", "elasticnet", "none")), "dual": TypeOf(bool), "C": Interval(float, 0.0, None), "fit_intercept": True } msg = "{} is not a valid parameter".format(error_key) with pytest.raises(KeyError, match=msg): validate_parameters(config, params)
def test_validator_conditions_exists(params): config = { "kernel": Enum("linear", "poly", "rbf", "sigmoid", "percomputed"), "degree": Interval(int, 1, None), "gamma": Union(Interval(float, 0, None), Const("auto")), "coef0": Interval(float, 0.0, None), "tol": Interval(float, 0.0, None, lower_inclusive=False), "_conditions": { "kernel": { "poly": ["degree", "gamma", "coef0"], "rbf": ["gamma"], "sigmoid": ["gamma", "coef0"] } } } validate_parameters(config, params)
def test_validator_conditions_error(params, msg): config = { "kernel": Enum("linear", "poly", "rbf", "sigmoid", "percomputed"), "degree": Interval(int, 1, None), "gamma": Union(Interval(float, 0, None), Const("auto")), "coef0": Interval(float, 0.0, None), "tol": Interval(float, 0.0, None, lower_inclusive=False), "_conditions": { "tol": {}, "kernel": { "poly": ["degree", "gamma", "coef0"], "rbf": ["gamma"], "sigmoid": ["gamma", "coef0"] } } } with pytest.raises(ValueError, match=msg): validate_parameters(config, params)
def test_enum_invalid_type_error(): enum, value, msg = Enum('hello', 'f'), 1, r'1 is not in \[hello, f\]' with pytest.raises(ValueError, match=msg): enum.validate(value, 'tol')
def test_enum_invalid_members_init(members, msg): with pytest.raises(ValueError, match=msg): Enum(*members)
def test_union_removes_tags(): union = Union(TypeOf(int, tags=['control']), Enum('a', 'b', tags=['not good']), tags=['deprecated']) for params in union.params: assert not params.tags
def test_constant_invalid_values(constant, value): msg = 'tol: {} != {}'.format(value, constant.value) with pytest.raises(ValueError, match=msg): constant.validate(value, "tol") @pytest.mark.parametrize('members, msg', [([], 'members must have at least one item'), ((), 'members must have at least one item')]) def test_enum_invalid_members_init(members, msg): with pytest.raises(ValueError, match=msg): Enum(*members) @pytest.mark.parametrize('enum, value', [(Enum('a', 'b'), 'a'), (Enum('a', 'b'), 'b'), (Enum('a', 'c', 'b'), 'c'), (Enum('a', 1, None, 1.0, True), 'a'), (Enum('a', 1, None, 1.0, True), 1), (Enum('a', 1, None, 1.0, True), None), (Enum('a', 1, None, 1.0, True), 1.0), (Enum('a', 1, None, 1.0, True), True)]) def test_enum_values(enum, value): # does not raise enum.validate(value, "tol") @pytest.mark.parametrize( 'enum, value, msg', [(Enum('a', '5'), '3', r'3 is not in \[a, 5\]'), (Enum('a', '3', '9'), '5', r'5 is not in \[a, 3, 9\]'),