Esempio n. 1
0
    def test_integer_default(self):
        default_value = '123'
        schema = Schema('integer', default=default_value)
        value = None

        with pytest.raises(InvalidSchemaValue):
            schema.unmarshal(value)
Esempio n. 2
0
    def test_string_format_unknown(self):
        unknown_format = 'unknown'
        schema = Schema('string', schema_format=unknown_format)
        value = 'x'

        with pytest.raises(OpenAPISchemaError):
            schema.unmarshal(value)
Esempio n. 3
0
    def test_string_default(self):
        default_value = 'default'
        schema = Schema('string', default=default_value)
        value = None

        with pytest.raises(InvalidSchemaValue):
            schema.unmarshal(value)
Esempio n. 4
0
 def test_schema_any_one_of_no_valid(self):
     schema = Schema(one_of=[
         Schema('array', items=Schema('string')),
         Schema('array', items=Schema('number')),
     ])
     with pytest.raises(NoOneOfSchema):
         schema.unmarshal({})
Esempio n. 5
0
 def test_schema_any_one_of_mutiple(self):
     schema = Schema(one_of=[
         Schema('array', items=Schema('string')),
         Schema('array', items=Schema('number')),
     ])
     with pytest.raises(MultipleOneOfSchema):
         schema.unmarshal([])
Esempio n. 6
0
    def test_string_format_invalid_value(self):
        custom_format = 'custom'
        schema = Schema('string', schema_format=custom_format)
        value = 'x'

        with pytest.raises(
            InvalidSchemaValue, message='Failed to format value'
        ):
            schema.unmarshal(value)
Esempio n. 7
0
    def test_string_format_custom_value_error(self):
        def custom_formatter(value):
            raise ValueError
        custom_format = 'custom'
        schema = Schema('string', schema_format=custom_format)
        value = 'x'

        with pytest.raises(InvalidSchemaValue):
            schema.unmarshal(
                value, custom_formatters={custom_format: custom_formatter})
Esempio n. 8
0
    def test_string_format_invalid_value(self):
        custom_format = 'custom'
        schema = Schema('string', schema_format=custom_format)
        value = 'x'

        with mock.patch.dict(
                Schema.FORMAT_CALLABLE_GETTER,
            {custom_format: mock.Mock(side_effect=ValueError())},
        ), pytest.raises(InvalidSchemaValue, message='Failed to format value'):
            schema.unmarshal(value)
Esempio n. 9
0
    def test_string_format_invalid_value(self):
        custom_format = 'custom'
        schema = Schema('string', schema_format=custom_format)
        value = 'x'

        with pytest.raises(
                FormatterNotFoundError,
                message=(
                    'Formatter not found for custom format to unmarshal value x'
                ),
        ):
            schema.unmarshal(value)
Esempio n. 10
0
    def test_array_valid(self):
        schema = Schema('array', items=Schema('integer'))
        value = [1, 2, 3]

        result = schema.unmarshal(value)

        assert result == value
Esempio n. 11
0
    def test_integer_valid(self):
        schema = Schema('integer')
        value = '123'

        result = schema.unmarshal(value)

        assert result == int(value)
Esempio n. 12
0
    def test_integer_enum(self):
        schema = Schema('integer', enum=[1, 2, 3])
        value = '2'

        result = schema.unmarshal(value)

        assert result == int(value)
Esempio n. 13
0
    def test_string_format_uuid_uuid_quirks_valid(self):
        schema = Schema(SchemaType.STRING, schema_format=SchemaFormat.UUID)
        value = uuid.uuid4()

        result = schema.unmarshal(value, strict=False)

        assert result == value
Esempio n. 14
0
    def test_boolean_valid(self):
        schema = Schema('boolean')
        value = True

        result = schema.unmarshal(value)

        assert result == value
Esempio n. 15
0
    def test_string_format_uuid_valid(self):
        schema = Schema(SchemaType.STRING, schema_format=SchemaFormat.UUID)
        value = str(uuid.uuid4())

        result = schema.unmarshal(value)

        assert result == uuid.UUID(value)
Esempio n. 16
0
    def test_non_string_empty_value(self, schema_type):
        schema = Schema(schema_type)
        value = ''

        result = schema.unmarshal(value)

        assert result is None
Esempio n. 17
0
    def test_number_float(self):
        schema = Schema('number')
        value = 1.2
        result = schema.unmarshal(value)

        assert result == 1.2
        assert type(result) == float
Esempio n. 18
0
    def test_number_int(self):
        schema = Schema('number')
        value = 1
        result = schema.unmarshal(value)

        assert result == 1
        assert type(result) == int
Esempio n. 19
0
    def test_string_format_password(self):
        schema = Schema(SchemaType.STRING, schema_format=SchemaFormat.PASSWORD)
        value = 'password'

        result = schema.unmarshal(value)

        assert result == 'password'
Esempio n. 20
0
    def test_number_valid(self):
        schema = Schema('number')
        value = 1.23

        result = schema.unmarshal(value)

        assert result == value
Esempio n. 21
0
    def test_string_valid(self):
        schema = Schema('string')
        value = 'test'

        result = schema.unmarshal(value)

        assert result == value
Esempio n. 22
0
    def test_string_format_date(self):
        schema = Schema('string', schema_format='date')
        value = '2018-01-02'

        result = schema.unmarshal(value)

        assert result == datetime.date(2018, 1, 2)
Esempio n. 23
0
    def test_integer_default_nullable(self):
        default_value = '123'
        schema = Schema('integer', default=default_value, nullable=True)
        value = None

        result = schema.unmarshal(value)

        assert result == default_value
Esempio n. 24
0
    def test_string_format_unknown(self):
        unknown_format = 'unknown'
        schema = Schema('string', schema_format=unknown_format)
        value = 'x'

        result = schema.unmarshal(value)

        assert result == 'x'
Esempio n. 25
0
    def test_string_default_nullable(self):
        default_value = 'default'
        schema = Schema('string', default=default_value, nullable=True)
        value = None

        result = schema.unmarshal(value)

        assert result == default_value
Esempio n. 26
0
    def test_deprecated(self):
        schema = Schema('string', deprecated=True)
        value = 'test'

        with pytest.warns(DeprecationWarning):
            result = schema.unmarshal(value)

        assert result == value
Esempio n. 27
0
    def test_string_format_custom(self):
        def custom_formatter(value):
            return 'x-custom'
        custom_format = 'custom'
        schema = Schema('string', schema_format=custom_format)
        value = 'x'

        result = schema.unmarshal(
            value, custom_formatters={custom_format: custom_formatter})

        assert result == custom_formatter(value)
Esempio n. 28
0
    def test_string_format_custom(self):
        custom_format = 'custom'
        schema = Schema('string', schema_format=custom_format)
        value = 'x'

        with mock.patch.dict(
                Schema.FORMAT_CALLABLE_GETTER,
            {custom_format: lambda x: x + '-custom'},
        ):
            result = schema.unmarshal(value)

        assert result == 'x-custom'
Esempio n. 29
0
    def test_string_none(self):
        schema = Schema('string')
        value = None

        with pytest.raises(InvalidSchemaValue):
            schema.unmarshal(value)
Esempio n. 30
0
    def test_integer_invalid(self):
        schema = Schema('integer')
        value = 'abc'

        with pytest.raises(InvalidSchemaValue):
            schema.unmarshal(value)