Ejemplo n.º 1
0
def test_int64_long():
    spec = {'type': 'integer', 'format': 'int64'}
    if six.PY3:
        result = to_python(spec, 999)
        assert 999 == result
    else:
        result = to_python(spec, long(999))
        assert long(999) == result
Ejemplo n.º 2
0
def test_int64_long(minimal_swagger_spec):
    integer_spec = {'type': 'integer', 'format': 'int64'}
    if six.PY3:
        result = to_python(minimal_swagger_spec, integer_spec, 999)
        assert 999 == result
    else:
        result = to_python(minimal_swagger_spec, integer_spec, long(999))
        assert long(999) == result
Ejemplo n.º 3
0
def test_int64_long(minimal_swagger_spec):
    integer_spec = {'type': 'integer', 'format': 'int64'}
    if six.PY3:
        result = to_python(minimal_swagger_spec, integer_spec, 999)
        assert 999 == result
    else:
        result = to_python(minimal_swagger_spec, integer_spec, long(999))
        assert long(999) == result
Ejemplo n.º 4
0
def test_int64_long(minimal_swagger_spec):
    integer_spec = {"type": "integer", "format": "int64"}
    if six.PY3:
        result = to_python(minimal_swagger_spec, integer_spec, 999)
        assert 999 == result
    else:
        result = to_python(minimal_swagger_spec, integer_spec, long(999))
        assert long(999) == result
Ejemplo n.º 5
0
def test_date(minimal_swagger_spec):
    string_spec = {'type': 'string', 'format': 'date'}
    assert date(2015, 4, 1) == to_python(
        minimal_swagger_spec,
        string_spec,
        '2015-04-01',
    )
Ejemplo n.º 6
0
def test_override(minimal_swagger_dict):
    class Byte(object):
        def __init__(self, x):
            self.x = x

        def __str__(self):
            return str(self.x)

        def __repr__(self):
            return '%s(%r)' % (self.__class__, self.x)

    byteformat = SwaggerFormat(
        format='byte',
        to_wire=lambda x: str(x),
        to_python=lambda x: Byte(x),
        validate=lambda x: isinstance(x, str),
        description=None,
    )

    number_spec = {'type': 'string', 'format': 'byte'}

    swagger_spec = Spec.from_dict(minimal_swagger_dict,
                                  config={'formats': [byteformat]})
    result = to_python(swagger_spec, number_spec, '8bits')

    assert '8bits' == str(result)
    assert repr(Byte('8bits')) == repr(result)
    assert type(result) is Byte
def test_success(register_base64_format):
    spec = {
        'name': 'username',
        'type': 'string',
        'format': 'base64',
    }
    assert b'darwin' == to_python(spec, to_wire(spec, b'darwin'))
Ejemplo n.º 8
0
def test_int32_long():
    if six.PY3:  # test irrelevant in py3
        return
    spec = {'type': 'integer', 'format': 'int32'}
    result = to_python(spec, long(999))
    assert 999 == result
    assert isinstance(result, int)
Ejemplo n.º 9
0
def test_int32_long(minimal_swagger_spec):
    if six.PY3:  # test irrelevant in py3
        return
    integer_spec = {"type": "integer", "format": "int32"}
    result = to_python(minimal_swagger_spec, integer_spec, long(999))
    assert 999 == result
    assert isinstance(result, int)
Ejemplo n.º 10
0
def test_int32_long(minimal_swagger_spec):
    if six.PY3:  # test irrelevant in py3
        return
    integer_spec = {'type': 'integer', 'format': 'int32'}
    result = to_python(minimal_swagger_spec, integer_spec, long(999))
    assert 999 == result
    assert isinstance(result, int)
Ejemplo n.º 11
0
def test_datetime(minimal_swagger_spec):
    string_spec = {'type': 'string', 'format': 'date-time'}
    result = to_python(
        minimal_swagger_spec,
        string_spec,
        '2015-03-22T13:19:54',
    )
    assert datetime(2015, 3, 22, 13, 19, 54) == result
Ejemplo n.º 12
0
def test_int64_int(minimal_swagger_spec):
    integer_spec = {"type": "integer", "format": "int64"}
    result = to_python(minimal_swagger_spec, integer_spec, 999)
    if six.PY3:
        assert 999 == result
        assert isinstance(result, int)
    else:
        assert long(999) == result
        assert isinstance(result, long)
Ejemplo n.º 13
0
def test_int64_int(minimal_swagger_spec):
    integer_spec = {'type': 'integer', 'format': 'int64'}
    result = to_python(minimal_swagger_spec, integer_spec, 999)
    if six.PY3:
        assert 999 == result
        assert isinstance(result, int)
    else:
        assert long(999) == result
        assert isinstance(result, long)
Ejemplo n.º 14
0
def test_byte_base64(minimal_swagger_dict):
    swagger_spec = Spec.from_dict(
        minimal_swagger_dict,
        config={'use_base64_for_byte_format': True},
    )
    schema = {'type': 'string', 'format': 'byte'}
    result = to_python(swagger_spec, schema, 'YWJj/w==')
    assert b'abc\xff' == result
    assert isinstance(result, bytes)
Ejemplo n.º 15
0
def test_ref(minimal_swagger_dict):
    minimal_swagger_dict['definitions']['Int32'] = {
        'type': 'integer', 'format': 'int32'
    }
    int_ref_spec = {'$ref': '#/definitions/Int32'}
    swagger_spec = Spec.from_dict(minimal_swagger_dict)
    result = to_python(swagger_spec, int_ref_spec, 999)
    assert 999 == result
    assert isinstance(result, int)
Ejemplo n.º 16
0
def test_int64_int():
    spec = {'type': 'integer', 'format': 'int64'}
    result = to_python(spec, 999)
    if six.PY3:
        assert 999 == result
        assert isinstance(result, int)
    else:
        assert long(999) == result
        assert isinstance(result, long)
Ejemplo n.º 17
0
def test_ref(minimal_swagger_dict):
    minimal_swagger_dict['definitions']['Int32'] = {
        'type': 'integer',
        'format': 'int32'
    }
    int_ref_spec = {'$ref': '#/definitions/Int32'}
    swagger_spec = Spec.from_dict(minimal_swagger_dict)
    result = to_python(swagger_spec, int_ref_spec, 999)
    assert 999 == result
    assert isinstance(result, int)
Ejemplo n.º 18
0
def test_success():
    register_format('base64', base64.b64encode, base64.b64decode, "Base64")
    try:
        spec = {
            'name': 'username',
            'type': 'string',
            'format': 'base64',
        }
        assert 'darwin' == to_python(spec, to_wire(spec, 'darwin'))
    finally:
        # I know, icky!
        del bravado_core.formatter._formatters['base64']
Ejemplo n.º 19
0
def unmarshal_primitive(swagger_spec, primitive_spec, value):
    """Unmarshal a jsonschema primitive type into a python primitive.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :type primitive_spec: dict
    :type value: int, long, float, boolean, string, unicode, etc

    :rtype: int, long, float, boolean, string, unicode, or an object
        based on 'format'
    :raises: SwaggerMappingError
    """
    if value is None:
        return handle_null_value(swagger_spec, primitive_spec)

    value = formatter.to_python(swagger_spec, primitive_spec, value)
    return value
Ejemplo n.º 20
0
def unmarshal_primitive(swagger_spec, primitive_spec, value):
    """Unmarshal a jsonschema primitive type into a python primitive.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :type primitive_spec: dict
    :type value: int, long, float, boolean, string, unicode, etc

    :rtype: int, long, float, boolean, string, unicode, or an object
        based on 'format'
    :raises: SwaggerMappingError
    """
    if value is None:
        return handle_null_value(swagger_spec, primitive_spec)

    value = formatter.to_python(swagger_spec, primitive_spec, value)
    return value
Ejemplo n.º 21
0
def unmarshal_primitive(swagger_spec, primitive_spec, value):
    """Unmarshal a jsonschema primitive type into a python primitive.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :type primitive_spec: dict
    :type value: int, long, float, boolean, string, unicode, etc

    :rtype: int, long, float, boolean, string, unicode, or an object
        based on 'format'
    :raises: SwaggerMappingError
    """
    if value is None and schema.is_required(swagger_spec, primitive_spec):
        raise SwaggerMappingError(
            'Spec {0} says this is a required value'.format(primitive_spec))

    value = formatter.to_python(swagger_spec, primitive_spec, value)
    return value
Ejemplo n.º 22
0
def unmarshal_primitive(spec, value):
    """Unmarshal a jsonschema primitive type into a python primitive.

    :type spec: dict or jsonref.JsonRef
    :type value: int, long, float, boolean, string, unicode, etc
    :rtype: int, long, float, boolean, string, unicode, or an object
        based on 'format'
    :raises: TypeError
    """
    if value is None and schema.is_required(spec):
        # TODO: Error message needs more context. Consider adding a stack like
        #       `context` object to each `unmarshal_*` method that acts like
        #       breadcrumbs.
        raise TypeError('Spec {0} says this is a required value'.format(spec))

    value = formatter.to_python(spec, value)
    return value
Ejemplo n.º 23
0
def unmarshal_primitive(swagger_spec, primitive_spec, value):
    """Unmarshal a jsonschema primitive type into a python primitive.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :type primitive_spec: dict
    :type value: int, long, float, boolean, string, unicode, etc

    :rtype: int, long, float, boolean, string, unicode, or an object
        based on 'format'
    :raises: SwaggerMappingError
    """
    if value is None and schema.is_required(swagger_spec, primitive_spec):
        raise SwaggerMappingError(
            'Spec {0} says this is a required value'.format(primitive_spec))

    value = formatter.to_python(swagger_spec, primitive_spec, value)
    return value
Ejemplo n.º 24
0
def unmarshal_primitive(swagger_spec, primitive_spec, value):
    """Unmarshal a jsonschema primitive type into a python primitive.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :type primitive_spec: dict or jsonref.JsonRef
    :type value: int, long, float, boolean, string, unicode, etc
    :rtype: int, long, float, boolean, string, unicode, or an object
        based on 'format'
    :raises: SwaggerMappingError
    """
    if value is None and schema.is_required(primitive_spec):
        # TODO: Error message needs more context. Consider adding a stack like
        #       `context` object to each `unmarshal_*` method that acts like
        #       breadcrumbs.
        raise SwaggerMappingError(
            'Spec {0} says this is a required value'.format(primitive_spec))

    value = formatter.to_python(swagger_spec, primitive_spec, value)
    return value
Ejemplo n.º 25
0
def test_byte():
    spec = {'type': 'string', 'format': 'byte'}
    result = to_python(spec, 'x')
    assert 'x' == result
    assert isinstance(result, str)
Ejemplo n.º 26
0
def test_no_registered_format_returns_value_as_is(_):
    spec = {'type': 'foo', 'format': 'bar'}
    assert 'baz' == to_python(spec, 'baz')
Ejemplo n.º 27
0
def test_double(minimal_swagger_spec):
    double_spec = {'type': 'number', 'format': 'double'}
    result = to_python(minimal_swagger_spec, double_spec, float(3.14))
    assert 3.14 == result
    assert isinstance(result, float)
Ejemplo n.º 28
0
def test_int32_int(minimal_swagger_spec):
    integer_spec = {'type': 'integer', 'format': 'int32'}
    result = to_python(minimal_swagger_spec, integer_spec, 999)
    assert 999 == result
    assert isinstance(result, int)
Ejemplo n.º 29
0
def test_no_registered_format_returns_value_as_is_and_issues_warning(mock_warn, minimal_swagger_spec):
    string_spec = {"type": "string", "format": "bar"}
    assert "baz" == to_python(minimal_swagger_spec, string_spec, "baz")
    assert mock_warn.call_count == 1
Ejemplo n.º 30
0
def test_no_registered_format_returns_value_as_is_and_issues_warning(
        mock_warn, minimal_swagger_spec):
    string_spec = {'type': 'string', 'format': 'bar'}
    assert 'baz' == to_python(minimal_swagger_spec, string_spec, 'baz')
    assert mock_warn.call_count == 1
Ejemplo n.º 31
0
def test_date():
    spec = {'type': 'string', 'format': 'date'}
    assert date(2015, 4, 1) == to_python(spec, '2015-04-01')
Ejemplo n.º 32
0
def test_no_format_returns_value():
    spec = {'type': 'string'}
    assert 'boo' == to_python(spec, 'boo')
Ejemplo n.º 33
0
def test_double(minimal_swagger_spec):
    double_spec = {"type": "number", "format": "double"}
    result = to_python(minimal_swagger_spec, double_spec, float(3.14))
    assert 3.14 == result
    assert isinstance(result, float)
Ejemplo n.º 34
0
def test_byte(minimal_swagger_spec):
    byte_spec = {"type": "string", "format": "byte"}
    result = to_python(minimal_swagger_spec, byte_spec, "x")
    assert "x" == result
    assert isinstance(result, str)
Ejemplo n.º 35
0
def test_int64_long(minimal_swagger_spec):
    integer_spec = {'type': 'integer', 'format': 'int64'}
    result = to_python(minimal_swagger_spec, integer_spec, long(999))
    assert long(999) == result
Ejemplo n.º 36
0
def test_none(minimal_swagger_spec):
    string_spec = {"type": "string", "format": "date"}
    assert to_python(minimal_swagger_spec, string_spec, None) is None
Ejemplo n.º 37
0
def test_datetime():
    spec = {'type': 'string', 'format': 'date-time'}
    result = to_python(spec, '2015-03-22T13:19:54')
    assert datetime(2015, 3, 22, 13, 19, 54) == result
Ejemplo n.º 38
0
def test_int64_long():
    spec = {'type': 'integer', 'format': 'int64'}
    result = to_python(spec, 999L)
    assert 999L == result
Ejemplo n.º 39
0
def test_no_format_returns_value(minimal_swagger_spec):
    string_spec = {'type': 'string'}
    assert 'boo' == to_python(minimal_swagger_spec, string_spec, 'boo')
Ejemplo n.º 40
0
def test_int64_int():
    spec = {'type': 'integer', 'format': 'int64'}
    result = to_python(spec, 999)
    assert 999L == result
    assert isinstance(result, long)
Ejemplo n.º 41
0
def test_int32_int(minimal_swagger_spec):
    integer_spec = {"type": "integer", "format": "int32"}
    result = to_python(minimal_swagger_spec, integer_spec, 999)
    assert 999 == result
    assert isinstance(result, int)
Ejemplo n.º 42
0
def test_int32_int():
    spec = {'type': 'integer', 'format': 'int32'}
    result = to_python(spec, 999)
    assert 999 == result
    assert isinstance(result, int)
Ejemplo n.º 43
0
def test_datetime(minimal_swagger_spec):
    string_spec = {"type": "string", "format": "date-time"}
    result = to_python(minimal_swagger_spec, string_spec, "2015-03-22T13:19:54")
    assert datetime(2015, 3, 22, 13, 19, 54) == result
Ejemplo n.º 44
0
def test_none():
    spec = {'type': 'string', 'format': 'date'}
    assert to_python(spec, None) is None
Ejemplo n.º 45
0
def test_none(minimal_swagger_spec):
    string_spec = {'type': 'string', 'format': 'date'}
    assert to_python(minimal_swagger_spec, string_spec, None) is None
Ejemplo n.º 46
0
def test_double():
    spec = {'type': 'number', 'format': 'double'}
    result = to_python(spec, float(3.14))
    assert 3.14 == result
    assert isinstance(result, float)
Ejemplo n.º 47
0
def test_byte(minimal_swagger_spec):
    byte_spec = {'type': 'string', 'format': 'byte'}
    result = to_python(minimal_swagger_spec, byte_spec, 'x')
    assert 'x' == result
    assert isinstance(result, str)
Ejemplo n.º 48
0
def test_no_registered_format_throws_warning(mock_warn):
    to_python({'type': 'foo', 'format': 'bar'}, 'baz')
    mock_warn.assert_called_once()