コード例 #1
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_Tuple():
    assert cast(JsonValue, JsonSchema(Tuple[str, ...])) == {
        'type': 'array',
        'items': {
            'type': 'string',
        }
    }

    assert cast(JsonValue, JsonSchema(Tuple[()])) == {
        'type': 'array',
        'items': [],
    }

    assert cast(JsonValue, JsonSchema(Tuple[(str, int)])) == {
        'type': 'array',
        'items': [{
            'type': 'string',
        }, {
            'type': 'integer',
        }],
    }

    assert cast(JsonValue, JsonSchema(Tuple[(float, float)])) == {
        'type': 'array',
        'items': [{
            'type': 'number',
        }, {
            'type': 'number',
        }],
    }
コード例 #2
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_Union():
    assert cast(JsonValue, JsonSchema(Union[str, int, bool])) == {
        'type': ['string', 'integer', 'boolean'],
    }

    assert cast(JsonValue, JsonSchema(Union[str, int, bool, Any])) == {}

    assert cast(JsonValue, JsonSchema(Union[str, List[str]])) == {
        'anyOf': [{
            'type': 'string',
        }, {
            'type': 'array',
            'items': {
                'type': 'string',
            }
        }]
    }

    class Number:
        pass

    @JsonSchema.register(Number)
    def _(self, cls):
        self.type = ['integer', 'number']

    assert cast(JsonValue, JsonSchema(Union[str, int, Number])) == {
        'type': ['string', 'integer', 'number'],
    }
コード例 #3
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonValue():
    data = {'a': ["0", True, 2, 3.14], 'b': range(4)}
    expected = {'a': ['0', True, 2, 3.14], 'b': [0, 1, 2, 3]}
    assert cast(JsonValue, data) == expected

    assert cast(JsonValue, []) == []
    assert cast(JsonValue, {}) == {}
    assert cast(JsonValue, ()) == ()

    # JsonValue is abstract
    with pytest.raises(TypeError):
        JsonValue()
コード例 #4
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_List():
    assert cast(JsonValue, JsonSchema(List[str])) == {
        'type': 'array',
        'items': {
            'type': 'string',
        }
    }
コード例 #5
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_Dict():
    assert cast(JsonValue, JsonSchema(Dict[str, int])) == {
        'type': 'object',
        'additionalProperties': {
            'type': 'integer'
        },
    }
コード例 #6
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_Set():
    assert cast(JsonValue, JsonSchema(Set[str])) == {
        'type': 'array',
        'uniqueItems': True,
        'items': {
            'type': 'string',
        }
    }
コード例 #7
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_Object():
    class X(Object):
        a: int
        b: str = field(required=True)

    assert cast(JsonValue, JsonSchema(X)) == {
        'type': 'object',
        'properties': {
            'a': {
                'type': 'integer',
            },
            'b': {
                'type': 'string',
            }
        },
        'required': ['b'],
        'additionalProperties': False,
    }

    uri = 'https://raw.githubusercontent.com/open-rpc/meta-schema/master/schema.json'

    class OpenRpcDoc(Object, jsonschema=uri):
        pass

    assert cast(JsonValue, JsonSchema(OpenRpcDoc)) == {'$ref': uri}

    class Empty(Object):
        pass

    assert cast(JsonValue, JsonSchema(Empty)) == {
        'type': 'object',
        'additionalProperties': False,
    }

    class AllOptional(Object):
        a: int

    assert cast(JsonValue, JsonSchema(AllOptional)) == {
        'type': 'object',
        'properties': {
            'a': {
                'type': 'integer',
            },
        },
        'additionalProperties': False,
    }
コード例 #8
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_complex():
    assert cast(JsonValue, JsonSchema(complex)) == {
        'type': 'array',
        'items': [{
            'type': 'number',
        }, {
            'type': 'number',
        }],
    }
コード例 #9
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_IntFlag():
    class Perm(enum.IntFlag):
        R = 4
        W = 2
        X = 1

    assert cast(dict, JsonSchema(Perm)) == {
        'type': 'integer',
    }
コード例 #10
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_IntEnum():
    class Shape(enum.IntEnum):
        CIRCLE = 1
        SQUARE = 2

    assert cast(dict, JsonSchema(Shape)) == {
        'type': 'integer',
        'enum': [1, 2],
    }
コード例 #11
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_Enum():
    class Color(enum.Enum):
        RED = None
        GREEN = 1
        BLUE = 'blue'

    assert cast(dict, JsonSchema(Color)) == {
        'type': 'string',
        'enum': ['RED', 'GREEN', 'BLUE'],
    }
コード例 #12
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_None():
    assert cast(dict, JsonSchema(type(None))) == {'type': 'null'}
コード例 #13
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_JsonValue():
    assert cast(JsonValue, JsonSchema(JsonValue)) == {}
コード例 #14
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_object():
    assert cast(dict, JsonSchema(object)) == {}
コード例 #15
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_set():
    assert cast(dict, JsonSchema(set)) == {
        'type': 'array',
        'uniqueItems': True
    }
コード例 #16
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_str():
    assert cast(dict, JsonSchema(str)) == {'type': 'string'}
コード例 #17
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_bytes():
    assert cast(dict, JsonSchema(bytes)) == {'type': 'string'}
コード例 #18
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_tuple():
    assert cast(dict, JsonSchema(tuple)) == {'type': 'array'}
コード例 #19
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_list():
    assert cast(dict, JsonSchema(list)) == {'type': 'array'}
コード例 #20
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_dict():
    assert cast(dict, JsonSchema(dict)) == {'type': 'object'}
コード例 #21
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_time():
    assert cast(dict, JsonSchema(datetime.time)) == {
        'type': 'string',
        'format': 'time'
    }
コード例 #22
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_instance():
    assert cast(dict, JsonSchema()) == {}
    assert cast(dict, JsonSchema({})) == {}
コード例 #23
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_Literal():
    assert cast(JsonValue, JsonSchema(Literal[0, '1', 2.3])) == {
        'enum': [0, '1', 2.3],
    }
コード例 #24
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_bool():
    assert cast(dict, JsonSchema(bool)) == {'type': 'boolean'}
コード例 #25
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_Optional():
    assert cast(JsonValue, JsonSchema(Optional[int])) == {
        'type': ['integer', 'null'],
    }
コード例 #26
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_Any():
    assert cast(dict, JsonSchema(Any)) == {}
コード例 #27
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_timedelta():
    assert cast(dict, JsonSchema(datetime.timedelta)) == {
        'type': 'string',
        'format': 'duration'
    }
コード例 #28
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_float():
    assert cast(dict, JsonSchema(float)) == {'type': 'number'}
コード例 #29
0
ファイル: test_json.py プロジェクト: flowdas/typeable
def test_JsonSchema_from_int():
    assert cast(dict, JsonSchema(int)) == {'type': 'integer'}