コード例 #1
0
def test_to_wire_with_wrong_format(minimal_swagger_spec):
    with pytest.raises(
            SwaggerMappingError,
            match=
            "Error while marshalling value=random-test to type=string/date."):
        to_wire(minimal_swagger_spec, {
            'type': 'string',
            'format': 'date'
        }, 'random-test')
コード例 #2
0
def test_int64_long(minimal_swagger_spec):
    integer_spec = {'type': 'integer', 'format': 'int64'}
    if six.PY3:
        result = to_wire(minimal_swagger_spec, integer_spec, 999)
        assert 999 == result
        assert isinstance(result, int)
    else:
        result = to_wire(minimal_swagger_spec, integer_spec, long(999))
        assert long(999) == result
        assert isinstance(result, long)
コード例 #3
0
def test_int64_long(minimal_swagger_spec):
    integer_spec = {'type': 'integer', 'format': 'int64'}
    if six.PY3:
        result = to_wire(minimal_swagger_spec, integer_spec, 999)
        assert 999 == result
        assert isinstance(result, int)
    else:
        result = to_wire(minimal_swagger_spec, integer_spec, long(999))
        assert long(999) == result
        assert isinstance(result, long)
コード例 #4
0
ファイル: marshal.py プロジェクト: nickdirienzo/bravado-core
def marshal_primitive(swagger_spec, primitive_spec, value):
    """Marshal a python primitive type into a jsonschema primitive.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :type primitive_spec: dict
    :type value: int, long, float, boolean, string, unicode, or an object
        based on 'format'

    :rtype: int, long, float, boolean, string, unicode, etc
    :raises: SwaggerMappingError
    """
    default_used = False

    if value is None and schema.has_default(swagger_spec, primitive_spec):
        default_used = True
        value = schema.get_default(swagger_spec, primitive_spec)

    if value is None and schema.is_required(swagger_spec, primitive_spec):
        raise SwaggerMappingError(
            'Spec {0} is a required value'.format(primitive_spec))

    if not default_used:
        value = formatter.to_wire(swagger_spec, primitive_spec, value)

    return value
コード例 #5
0
def test_localized_datetime(minimal_swagger_spec):
    string_spec = {'type': 'string', 'format': 'date-time'}
    assert '2015-03-22T13:19:54-07:00' == to_wire(
        minimal_swagger_spec,
        string_spec,
        timezone('America/Los_Angeles').localize(datetime(2015, 3, 22, 13, 19, 54)),
    )
コード例 #6
0
ファイル: marshal.py プロジェクト: wting/bravado-core
def marshal_primitive(swagger_spec, primitive_spec, value):
    """Marshal a python primitive type into a jsonschema primitive.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :type primitive_spec: dict
    :type value: int, long, float, boolean, string, unicode, or an object
        based on 'format'

    :rtype: int, long, float, boolean, string, unicode, etc
    :raises: SwaggerMappingError
    """
    default_used = False

    if value is None and schema.has_default(swagger_spec, primitive_spec):
        default_used = True
        value = schema.get_default(swagger_spec, primitive_spec)

    if value is None and schema.is_required(swagger_spec, primitive_spec):
        raise SwaggerMappingError(
            'Spec {0} is a required value'.format(primitive_spec))

    if not default_used:
        value = formatter.to_wire(swagger_spec, primitive_spec, value)

    return value
コード例 #7
0
def test_success(register_base64_format):
    spec = {
        'name': 'username',
        'type': 'string',
        'format': 'base64',
    }
    assert b'darwin' == to_python(spec, to_wire(spec, b'darwin'))
コード例 #8
0
def test_int32_long(minimal_swagger_spec):
    if six.PY3:  # test irrelevant in py3
        return
    integer_spec = {'type': 'integer', 'format': 'int32'}
    result = to_wire(minimal_swagger_spec, integer_spec, long(999))
    assert 999 == result
    assert isinstance(result, int)
コード例 #9
0
def test_naive_datetime(minimal_swagger_spec):
    string_spec = {'type': 'string', 'format': 'date-time'}
    assert '2015-03-22T13:19:54+00:00' == to_wire(
        minimal_swagger_spec,
        string_spec,
        datetime(2015, 3, 22, 13, 19, 54),
    )
コード例 #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_wire(minimal_swagger_spec, integer_spec, long(999))
    assert 999 == result
    assert isinstance(result, int)
コード例 #11
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_wire(swagger_spec, number_spec, '8bits')

    assert '8bits' == result
    assert isinstance(result, str)
    assert type(result) is StringType
コード例 #12
0
def test_date(minimal_swagger_spec):
    string_spec = {'type': 'string', 'format': 'date'}
    assert '2015-04-01' == to_wire(
        minimal_swagger_spec,
        string_spec,
        date(2015, 4, 1),
    )
コード例 #13
0
def test_localized_datetime(minimal_swagger_spec):
    string_spec = {'type': 'string', 'format': 'date-time'}
    assert '2015-03-22T13:19:54-07:00' == to_wire(
        minimal_swagger_spec,
        string_spec,
        timezone('America/Los_Angeles').localize(
            datetime(2015, 3, 22, 13, 19, 54)),
    )
コード例 #14
0
def test_int64_int():
    spec = {'type': 'integer', 'format': 'int64'}
    result = to_wire(spec, 999)
    if six.PY3:
        assert 999 == result
        assert isinstance(result, int)
    else:
        assert long(999) == result
        assert isinstance(result, long)
コード例 #15
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_wire(swagger_spec, schema, b'abc\xff')
    assert 'YWJj/w==' == result
    assert isinstance(result, str)
コード例 #16
0
ファイル: to_wire_test.py プロジェクト: JoelO/bravado-core
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_wire(swagger_spec, int_ref_spec, 999)
    assert 999 == result
    assert isinstance(result, int)
コード例 #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_wire(swagger_spec, int_ref_spec, 999)
    assert 999 == result
    assert isinstance(result, int)
コード例 #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']
コード例 #19
0
ファイル: marshal.py プロジェクト: bpicolo/bravado-core
def marshal_primitive(spec, value):
    """Marshal a python primitive type into a jsonschema primitive.

    :type spec: dict or jsonref.JsonRef
    :type value: int, long, float, boolean, string, unicode, or an object
        based on 'format'
    :rtype: int, long, float, boolean, string, unicode, etc
    :raises: TypeError
    """
    default_used = False

    if value is None and schema.has_default(spec):
        default_used = True
        value = schema.get_default(spec)

    if value is None and schema.is_required(spec):
        raise TypeError('Spec {0} is a required value'.format(spec))

    if not default_used:
        value = formatter.to_wire(spec, value)

    return value
コード例 #20
0
def test_success_validate(base64_spec, register_base64_format):
    validate_user_format(base64_spec, to_wire(base64_spec, b'darwin'))
コード例 #21
0
def test_byte_unicode():
    spec = {'type': 'string', 'format': 'byte'}
    result = to_wire(spec, u'x')
    assert 'x' == result
    assert isinstance(result, str)
コード例 #22
0
def test_double():
    spec = {'type': 'number', 'format': 'double'}
    result = to_wire(spec, 3.14)
    assert 3.14 == result
    assert isinstance(result, float)
コード例 #23
0
def test_int64_int(minimal_swagger_spec):
    integer_spec = {'type': 'integer', 'format': 'int64'}
    result = to_wire(minimal_swagger_spec, integer_spec, 999)
    assert long(999) == result
    assert isinstance(result, long)
コード例 #24
0
def test_datetime(minimal_swagger_spec):
    string_spec = {'type': 'string', 'format': 'date-time'}
    assert '2015-03-22T13:19:54' == to_wire(
        minimal_swagger_spec, string_spec, datetime(2015, 3, 22, 13, 19, 54))
コード例 #25
0
def test_no_registered_format_returns_value_as_is(_):
    spec = {'type': 'foo', 'format': 'bar'}
    assert 'baz' == to_wire(spec, 'baz')
コード例 #26
0
def test_no_format_returns_value(minimal_swagger_spec):
    string_spec = {'type': 'string'}
    assert 'boo' == to_wire(minimal_swagger_spec, string_spec, 'boo')
コード例 #27
0
def test_date():
    assert '2015-04-01' == to_wire({'format': 'date'}, date(2015, 4, 1))
コード例 #28
0
def test_datetime():
    result = to_wire({'format': 'date-time'}, datetime(2015, 3, 22, 13, 19, 54))
    assert '2015-03-22T13:19:54' == result
コード例 #29
0
def test_no_format_returns_value(minimal_swagger_spec):
    string_spec = {'type': 'string'}
    assert 'boo' == to_wire(minimal_swagger_spec, string_spec, 'boo')
コード例 #30
0
def test_double(minimal_swagger_spec):
    number_spec = {'type': 'number', 'format': 'double'}
    result = to_wire(minimal_swagger_spec, number_spec, 3.14)
    assert 3.14 == result
    assert isinstance(result, float)
コード例 #31
0
def test_int32_int(minimal_swagger_spec):
    integer_spec = {'type': 'integer', 'format': 'int32'}
    result = to_wire(minimal_swagger_spec, integer_spec, 999)
    assert 999 == result
    assert isinstance(result, int)
コード例 #32
0
def test_byte_unicode(minimal_swagger_spec):
    string_spec = {'type': 'string', 'format': 'byte'}
    result = to_wire(minimal_swagger_spec, string_spec, u'x')
    assert 'x' == result
    assert isinstance(result, str)
コード例 #33
0
def test_no_registered_format_throws_warning(mock_warn):
    to_wire({'type': 'foo', 'format': 'bar'}, 'baz')
    mock_warn.assert_called_once()
コード例 #34
0
ファイル: to_wire_test.py プロジェクト: bpicolo/bravado-core
def test_int64_int():
    spec = {'type': 'integer', 'format': 'int64'}
    result = to_wire(spec, 999)
    assert 999L == result
    assert isinstance(result, long)
コード例 #35
0
def test_none(minimal_swagger_spec):
    string_spec = {'type': 'string', 'format': 'date'}
    assert to_wire(minimal_swagger_spec, string_spec, None) is None
コード例 #36
0
def test_date(minimal_swagger_spec):
    string_spec = {'type': 'string', 'format': 'date'}
    assert '2015-04-01' == to_wire(
        minimal_swagger_spec, string_spec, date(2015, 4, 1))
コード例 #37
0
def test_int32_int():
    spec = {'type': 'integer', 'format': 'int32'}
    result = to_wire(spec, 999)
    assert 999 == result
    assert isinstance(result, int)
コード例 #38
0
def test_no_registered_format_returns_value_as_is(
        mock_warn, minimal_swagger_spec):
    string_spec = {'type': 'string', 'format': 'bar'}
    assert 'baz' == to_wire(minimal_swagger_spec, string_spec, 'baz')
    assert mock_warn.call_count == 1
コード例 #39
0
def test_none():
    spec = {'type': 'string', 'format': 'date'}
    assert to_wire(spec, None) is None
コード例 #40
0
def test_int32_int(minimal_swagger_spec):
    integer_spec = {'type': 'integer', 'format': 'int32'}
    result = to_wire(minimal_swagger_spec, integer_spec, 999)
    assert 999 == result
    assert isinstance(result, int)
コード例 #41
0
def test_no_registered_format_returns_value_as_is(mock_warn,
                                                  minimal_swagger_spec):
    string_spec = {'type': 'string', 'format': 'bar'}
    assert 'baz' == to_wire(minimal_swagger_spec, string_spec, 'baz')
    assert mock_warn.call_count == 1
コード例 #42
0
def test_none(minimal_swagger_spec):
    string_spec = {'type': 'string', 'format': 'date'}
    assert to_wire(minimal_swagger_spec, string_spec, None) is None
コード例 #43
0
def test_float(minimal_swagger_spec):
    number_spec = {'type': 'number', 'format': 'float'}
    result = to_wire(minimal_swagger_spec, number_spec, 3.14)
    assert 3.14 == result
    assert isinstance(result, float)
コード例 #44
0
def test_byte_unicode(minimal_swagger_spec):
    string_spec = {'type': 'string', 'format': 'byte'}
    result = to_wire(minimal_swagger_spec, string_spec, u'x')
    assert 'x' == result
    assert isinstance(result, str)
コード例 #45
0
def test_no_format_returns_value():
    spec = {'type': 'string'}
    assert 'boo' == to_wire(spec, 'boo')