def test_process_makefile__shared_subtrees_with_defaults__defaults_disabled(): subtree = {"A": IsInt(default=1234), "B": IsInt(default=5678)} specs = {"A": subtree, "B": WithoutDefaults(subtree)} current = {"A": {"B": 17}, "B": {"A": 71}} expected = {"A": {"A": 1234, "B": 17}, "B": {"A": 71}} result = process_makefile(current, specs) assert result == expected
def test_process_makefile__sets_missing_keys(): current = {"A": 1} specs = { "A": IsInt(default=0), "B": IsInt(default=-1), "C": IsInt(default=-2) } expected = {"A": 1, "B": -1, "C": -2} result = process_makefile(current, specs) assert result == expected
def test_process_makefile__sets_missing_recursive__with_missing_substructure(): current = {"A": 1} specs = { "A": IsInt(default=0), "B": { "C": IsInt(default=-1), "D": IsInt(default=-2) }, } expected = {"A": 1, "B": {"C": -1, "D": -2}} result = process_makefile(current, specs) assert result == expected
def test_deprecated_option__no_log_on_failure(caplog): expected = _DEPRECATED_TMPL % (_DUMMY_PATH_STR, ) spec = DeprecatedOption(IsInt(default=17)) with pytest.raises(MakefileError): spec(_DUMMY_PATH, "10") assert expected not in caplog.text
def __call__(self, path, value): if isinstance(value, str): return int(value), IsInt return value, IsInt(default=self._default)
def test_is_int__custom_description(): custom_desc = "any old integer" spec = IsInt(description=custom_desc) assert spec.description == custom_desc
def test_is_list_of__defaults_not_set_in_specs(): with pytest.raises(ValueError): IsListOf(IsInt(default=10))
def test_or__defaults_not_set_in_specs(): with pytest.raises(ValueError): Or(IsInt(default=10), ValueIn((10, )))
def test_deprecated_option__meets_spec(): spec = DeprecatedOption(IsInt(default=17)) assert spec.meets_spec(10) assert not spec.meets_spec("10")
def test_is_int__default_set__must_meet_spec(): with pytest.raises(ValueError): IsInt(default="abc")
def test_process_makefile__fails_when_required_value_not_set(): current = {"A": 1} specs = {"A": IsInt, "B": {"C": IsInt(default=REQUIRED_VALUE)}} with pytest.raises(MakefileError): process_makefile(current, specs)
def test_process_makefile__accept_when_required_value_is_set(): current = {"A": 1, "B": {"C": 3}} expected = {"A": 1, "B": {"C": 3}} specs = {"A": IsInt, "B": {"C": IsInt(default=REQUIRED_VALUE)}} result = process_makefile(current, specs) assert result == expected
def test_is_int__accepts_integers(): spec = IsInt() spec(_DUMMY_PATH, 1234) spec(_DUMMY_PATH, 0) spec(_DUMMY_PATH, -1234)
def test_is_dict_of__defaults_not_set_in_value_specs(): with pytest.raises(ValueError): IsDictOf(IsInt, IsInt(default=10))
def test_is_int__default_not_set(): spec = IsInt() assert spec.default is DEFAULT_NOT_SET
def test_process_makefile__fails_required_value_not_set_in_dynamic_subtree(): current = {"A": 1, "B": {}} specs = {"A": IsInt, IsStr: {"C": IsInt(default=REQUIRED_VALUE)}} with pytest.raises(MakefileError): process_makefile(current, specs)
def test_is_int__default_set__valid_value(): spec = IsInt(default=7913) assert spec.default == 7913
def test_is_int__rejects_not_int(value): spec = IsInt() with pytest.raises(MakefileError, match="Expected value: an integer"): spec(_DUMMY_PATH, value)
def test_deprecated_option__properties(): spec = DeprecatedOption(IsInt(default=17)) assert spec.default == 17 assert spec.description == IsInt(default=17).description
def test_process_makefile__accept_missing_value_if_in_implicit_subtree(): current = {"A": 1} expected = {"A": 1} specs = {"A": IsInt, IsStr: {"C": IsInt(default=REQUIRED_VALUE)}} result = process_makefile(current, specs) assert result == expected
def test_deprecated_option__logs_on_success(caplog): expected = _DEPRECATED_TMPL % (_DUMMY_PATH_STR, ) spec = DeprecatedOption(IsInt(default=17)) spec(_DUMMY_PATH, 10) assert expected in caplog.text
def test_process_makefile__implicit_subdict_is_allowed(): current = {"A": 1, "B": None} expected = {"A": 1, "B": {"C": 3}} specs = {"A": IsInt, "B": {"C": IsInt(default=3)}} result = process_makefile(current, specs) assert result == expected
def test_and__defaults_not_set_in_specs(): with pytest.raises(ValueError): And(IsInt(default=10), ValueIn((list(range(100)))))
def test_is_int__default_description(): spec = IsInt() assert spec.description == "an integer"
def test_not__defaults_not_set_in_specs(): with pytest.raises(ValueError): Not(IsInt(default=10))
def test_is_dict_of__defaults_not_set_in_key_specs(): with pytest.raises(ValueError): IsDictOf(IsInt(default=10), IsInt) def test_is_dict_of__defaults_not_set_in_value_specs(): with pytest.raises(ValueError): IsDictOf(IsInt, IsInt(default=10)) ############################################################################### ############################################################################### # Path is displayed in exception _PATH_IN_EXCEPTION_VALUES = ( (IsInt(), "foo"), (IsUnsignedInt(), -1), (IsFloat(), "abc"), (IsBoolean(), 1), (IsStr(), 1), (IsNone(), 1), (ValueIn([1]), 2), (ValuesIntersect([1]), [2]), (ValuesSubsetOf([1]), [2]), (ValueMissing(), True), (And(IsStr), 1), (Or(IsStr), 1), (Not(IsInt), 1), (StringIn("abc"), 1), (StringStartsWith("FOO"), 1), (StringEndsWith("FOO"), 1),