def test_normalize_availability_on_func_1(): func1 = availability(C1)(level_param_step_no_default) func1 = normalize("param", ["a", "b"])(func1) assert func1(level=1000, param="a", step="24") == ("1000", "a", "24") with pytest.raises(ValueError, match="invalid .*"): func1(level=850, param="a", step="24") with pytest.raises(ValueError): func1(level="1032100", param="a", step="24")
def test_enum_inconsistent_availablity_normalizers(): g = name_no_default g = normalize("name", type=int)(g) g = availability(C3)(g) g(name="50") with pytest.raises( ValueError, match= "Inconsistent types for availability and normalize for argument 'a'.", ): g(name="50")
def test_normalize_duplicates_normalize_1(): func7 = func_param func7 = normalize("param", ["b"])(func7) func7 = normalize("param", ["a", "b"])(func7) assert func7(param="B") == "b" with pytest.raises(ValueError): func7(param="a") func7a = availability(C1)(func7) assert func7a(param="B") == "b" with pytest.raises(ValueError): func7a(param="a")
def test_normalize_availability_on_func_2(): func2 = availability(C2)(level_param_step_no_default) func2 = normalize("param", ["a", "b"])(func2) # func2 = normalize("level", type=int)(func2) # func2 = normalize("step", type=int)(func2) # in C2: {"level": 1000, "param": "a", "step": 24}, assert func2(level=1000, param="a", step=24) == (1000, "a", 24) assert func2(level="1000", param="a", step=24) == (1000, "a", 24) with pytest.raises(ValueError, match="invalid .*"): func2(level="850", param="a", step="24") with pytest.raises(ValueError): func2(level="1032100", param="a", step="24")
def test_order_availability_normalize_int(): decorators = [ availability(C2), normalize("step", type=int, multiple=True), normalize("param", type=str, multiple=True), normalize("level", type=int, multiple=False), ] g = level_param_step_no_default for order in itertools.permutations(decorators): print(order) for decorator in order: g = decorator(g) print("---", g("1000", "a", "24")) assert g("1000", "a", "24") == (1000, ["a"], [24])
def test_normalize_availability_on_method(): norm_decorator = normalize("param", ["a", "b"]) availability_decorator_1 = availability(C1) class A: @norm_decorator @availability_decorator_1 def method1(self, level, param, step): return level, param, step assert A().method1(level="1000", param="a", step="24") == ("1000", "a", "24") with pytest.raises(ValueError): A().method1(level="1032100", param="a", step="24")
def test_availability_decorator_from_json_2(): g = availability("availability.json")(level_param_step_no_default) g("1000", "Z", "24")
def test_availability_decorator_from_text(): g = availability(C0)(level_param_step_no_default) g(level="1000", param="Z", step="24") with pytest.raises(ValueError): g(level="1032100", param="Z", step="24")