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
Example #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
Example #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
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
Example #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',
    )
Example #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'))
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)
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)
Example #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)
Example #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
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)
Example #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)
Example #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)
Example #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)
Example #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)
Example #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)
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']
Example #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
Example #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
Example #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
Example #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
Example #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
Example #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
Example #25
0
def test_byte():
    spec = {'type': 'string', 'format': 'byte'}
    result = to_python(spec, 'x')
    assert 'x' == result
    assert isinstance(result, str)
Example #26
0
def test_no_registered_format_returns_value_as_is(_):
    spec = {'type': 'foo', 'format': 'bar'}
    assert 'baz' == to_python(spec, 'baz')
Example #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)
Example #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)
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
Example #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
Example #31
0
def test_date():
    spec = {'type': 'string', 'format': 'date'}
    assert date(2015, 4, 1) == to_python(spec, '2015-04-01')
Example #32
0
def test_no_format_returns_value():
    spec = {'type': 'string'}
    assert 'boo' == to_python(spec, 'boo')
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)
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)
Example #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
def test_none(minimal_swagger_spec):
    string_spec = {"type": "string", "format": "date"}
    assert to_python(minimal_swagger_spec, string_spec, None) is None
Example #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
Example #38
0
def test_int64_long():
    spec = {'type': 'integer', 'format': 'int64'}
    result = to_python(spec, 999L)
    assert 999L == result
Example #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')
Example #40
0
def test_int64_int():
    spec = {'type': 'integer', 'format': 'int64'}
    result = to_python(spec, 999)
    assert 999L == result
    assert isinstance(result, long)
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)
Example #42
0
def test_int32_int():
    spec = {'type': 'integer', 'format': 'int32'}
    result = to_python(spec, 999)
    assert 999 == result
    assert isinstance(result, int)
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
Example #44
0
def test_none():
    spec = {'type': 'string', 'format': 'date'}
    assert to_python(spec, None) is None
Example #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
Example #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)
Example #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)
Example #48
0
def test_no_registered_format_throws_warning(mock_warn):
    to_python({'type': 'foo', 'format': 'bar'}, 'baz')
    mock_warn.assert_called_once()