def test_schemaor(self): """Test that a top-level schemaor accepts either of its arguments""" test_schema = SchemaOr({"count": int}, float) @schema def test_function(count_struct: test_schema) -> test_schema: return {"count": 5} test_function({"count": 5}) test_function(5.) self.assertRaises(SchemaError, test_function, {}) self.assertRaises(SchemaError, test_function, ["hello", 2])
def test_schemaor_nested_dict(self): """Test that SchemaOr objects work from inside dictionaries and lists.""" test_schema = {"count": SchemaOr(int, type(None))} @schema def test_function(count_struct: test_schema) -> test_schema: return {"count": 5} test_function({"count": 5}) test_function({"count": None}) test_function({}) self.assertRaises(SchemaError, test_function, "hello") self.assertRaises(SchemaError, test_function, {"count": "hello"}) self.assertRaises(SchemaError, test_function, {"count": {}}) self.assertRaises(SchemaError, test_function, {"hello": 5}) self.assertRaises(SchemaError, test_function, ["hello"])
def test_function_nd( count_struct: { "test": SchemaOr({"different_test": int}, type(None)) }): pass
def test_function_hel(count_struct: [SchemaOr(str), SchemaOr(int)]): pass
def test_function_hol(count_struct: [SchemaOr(str, int)]): pass
def test_function_d(count_struct: SchemaOr({ "a": int, "b": str }, {"c": str})) -> int: return 5
def test_function_hel(count_struct: SchemaOr([str, int, str], [int, str])) -> int: return 5
@typecheck def no_return_type() -> None: pass # schema.py NoneType = type(None) test_schema = { "hello": int, "world": { "people": [str], "version": int }, "optional": SchemaOr(int, NoneType) } @schema def schema_checked(a: test_schema) -> test_schema: b = deepcopy(a) b["hello"] += 5 if b["world"]["people"]: b["world"]["people"][0] = "Bob" b["world"]["version"] += 1 return b #---------------------- # Tests