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')
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)
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
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)), )
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(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)
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), )
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
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), )
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)), )
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)
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)
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)
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)
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']
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
def test_success_validate(base64_spec, register_base64_format): validate_user_format(base64_spec, to_wire(base64_spec, b'darwin'))
def test_byte_unicode(): spec = {'type': 'string', 'format': 'byte'} result = to_wire(spec, u'x') assert 'x' == result assert isinstance(result, str)
def test_double(): spec = {'type': 'number', 'format': 'double'} result = to_wire(spec, 3.14) assert 3.14 == result assert isinstance(result, float)
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)
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))
def test_no_registered_format_returns_value_as_is(_): spec = {'type': 'foo', 'format': 'bar'} assert 'baz' == to_wire(spec, 'baz')
def test_no_format_returns_value(minimal_swagger_spec): string_spec = {'type': 'string'} assert 'boo' == to_wire(minimal_swagger_spec, string_spec, 'boo')
def test_date(): assert '2015-04-01' == to_wire({'format': 'date'}, date(2015, 4, 1))
def test_datetime(): result = to_wire({'format': 'date-time'}, datetime(2015, 3, 22, 13, 19, 54)) assert '2015-03-22T13:19:54' == result
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)
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)
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)
def test_no_registered_format_throws_warning(mock_warn): to_wire({'type': 'foo', 'format': 'bar'}, 'baz') mock_warn.assert_called_once()
def test_int64_int(): spec = {'type': 'integer', 'format': 'int64'} result = to_wire(spec, 999) assert 999L == result assert isinstance(result, long)
def test_none(minimal_swagger_spec): string_spec = {'type': 'string', 'format': 'date'} assert to_wire(minimal_swagger_spec, string_spec, None) is None
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))
def test_int32_int(): spec = {'type': 'integer', 'format': 'int32'} result = to_wire(spec, 999) assert 999 == result assert isinstance(result, int)
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
def test_none(): spec = {'type': 'string', 'format': 'date'} assert to_wire(spec, None) is None
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
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)
def test_no_format_returns_value(): spec = {'type': 'string'} assert 'boo' == to_wire(spec, 'boo')