Exemple #1
0
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',
        }],
    }
Exemple #2
0
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'],
    }
Exemple #3
0
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()
Exemple #4
0
def test_JsonSchema_from_List():
    assert cast(JsonValue, JsonSchema(List[str])) == {
        'type': 'array',
        'items': {
            'type': 'string',
        }
    }
Exemple #5
0
def test_JsonSchema_from_Dict():
    assert cast(JsonValue, JsonSchema(Dict[str, int])) == {
        'type': 'object',
        'additionalProperties': {
            'type': 'integer'
        },
    }
Exemple #6
0
def test_JsonSchema_from_Set():
    assert cast(JsonValue, JsonSchema(Set[str])) == {
        'type': 'array',
        'uniqueItems': True,
        'items': {
            'type': 'string',
        }
    }
Exemple #7
0
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,
    }
Exemple #8
0
def test_JsonSchema_from_complex():
    assert cast(JsonValue, JsonSchema(complex)) == {
        'type': 'array',
        'items': [{
            'type': 'number',
        }, {
            'type': 'number',
        }],
    }
Exemple #9
0
def test_JsonSchema_from_IntFlag():
    class Perm(enum.IntFlag):
        R = 4
        W = 2
        X = 1

    assert cast(dict, JsonSchema(Perm)) == {
        'type': 'integer',
    }
Exemple #10
0
def test_JsonSchema_from_IntEnum():
    class Shape(enum.IntEnum):
        CIRCLE = 1
        SQUARE = 2

    assert cast(dict, JsonSchema(Shape)) == {
        'type': 'integer',
        'enum': [1, 2],
    }
Exemple #11
0
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'],
    }
Exemple #12
0
def test_JsonSchema_from_None():
    assert cast(dict, JsonSchema(type(None))) == {'type': 'null'}
Exemple #13
0
def test_JsonSchema_from_JsonValue():
    assert cast(JsonValue, JsonSchema(JsonValue)) == {}
Exemple #14
0
def test_JsonSchema_from_object():
    assert cast(dict, JsonSchema(object)) == {}
Exemple #15
0
def test_JsonSchema_from_set():
    assert cast(dict, JsonSchema(set)) == {
        'type': 'array',
        'uniqueItems': True
    }
Exemple #16
0
def test_JsonSchema_from_str():
    assert cast(dict, JsonSchema(str)) == {'type': 'string'}
Exemple #17
0
def test_JsonSchema_from_bytes():
    assert cast(dict, JsonSchema(bytes)) == {'type': 'string'}
Exemple #18
0
def test_JsonSchema_from_tuple():
    assert cast(dict, JsonSchema(tuple)) == {'type': 'array'}
Exemple #19
0
def test_JsonSchema_from_list():
    assert cast(dict, JsonSchema(list)) == {'type': 'array'}
Exemple #20
0
def test_JsonSchema_from_dict():
    assert cast(dict, JsonSchema(dict)) == {'type': 'object'}
Exemple #21
0
def test_JsonSchema_from_time():
    assert cast(dict, JsonSchema(datetime.time)) == {
        'type': 'string',
        'format': 'time'
    }
Exemple #22
0
def test_JsonSchema_instance():
    assert cast(dict, JsonSchema()) == {}
    assert cast(dict, JsonSchema({})) == {}
Exemple #23
0
def test_JsonSchema_from_Literal():
    assert cast(JsonValue, JsonSchema(Literal[0, '1', 2.3])) == {
        'enum': [0, '1', 2.3],
    }
Exemple #24
0
def test_JsonSchema_from_bool():
    assert cast(dict, JsonSchema(bool)) == {'type': 'boolean'}
Exemple #25
0
def test_JsonSchema_from_Optional():
    assert cast(JsonValue, JsonSchema(Optional[int])) == {
        'type': ['integer', 'null'],
    }
Exemple #26
0
def test_JsonSchema_from_Any():
    assert cast(dict, JsonSchema(Any)) == {}
Exemple #27
0
def test_JsonSchema_from_timedelta():
    assert cast(dict, JsonSchema(datetime.timedelta)) == {
        'type': 'string',
        'format': 'duration'
    }
Exemple #28
0
def test_JsonSchema_from_float():
    assert cast(dict, JsonSchema(float)) == {'type': 'number'}
Exemple #29
0
def test_JsonSchema_from_int():
    assert cast(dict, JsonSchema(int)) == {'type': 'integer'}