예제 #1
0
    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])
예제 #2
0
    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"])
예제 #3
0
 def test_function_nd(
     count_struct: {
         "test": SchemaOr({"different_test": int}, type(None))
     }):
     pass
예제 #4
0
 def test_function_hel(count_struct: [SchemaOr(str), SchemaOr(int)]):
     pass
예제 #5
0
 def test_function_hol(count_struct: [SchemaOr(str, int)]):
     pass
예제 #6
0
 def test_function_d(count_struct: SchemaOr({
     "a": int,
     "b": str
 }, {"c": str})) -> int:
     return 5
예제 #7
0
 def test_function_hel(count_struct: SchemaOr([str, int, str],
                                              [int, str])) -> int:
     return 5
예제 #8
0
@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