def test_integer_default(self): default_value = '123' schema = Schema('integer', default=default_value) value = None with pytest.raises(InvalidSchemaValue): schema.unmarshal(value)
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)
def test_string_default(self): default_value = 'default' schema = Schema('string', default=default_value) value = None with pytest.raises(InvalidSchemaValue): schema.unmarshal(value)
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({})
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([])
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)
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})
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)
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)
def test_array_valid(self): schema = Schema('array', items=Schema('integer')) value = [1, 2, 3] result = schema.unmarshal(value) assert result == value
def test_integer_valid(self): schema = Schema('integer') value = '123' result = schema.unmarshal(value) assert result == int(value)
def test_integer_enum(self): schema = Schema('integer', enum=[1, 2, 3]) value = '2' result = schema.unmarshal(value) assert result == int(value)
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
def test_boolean_valid(self): schema = Schema('boolean') value = True result = schema.unmarshal(value) assert result == value
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)
def test_non_string_empty_value(self, schema_type): schema = Schema(schema_type) value = '' result = schema.unmarshal(value) assert result is None
def test_number_float(self): schema = Schema('number') value = 1.2 result = schema.unmarshal(value) assert result == 1.2 assert type(result) == float
def test_number_int(self): schema = Schema('number') value = 1 result = schema.unmarshal(value) assert result == 1 assert type(result) == int
def test_string_format_password(self): schema = Schema(SchemaType.STRING, schema_format=SchemaFormat.PASSWORD) value = 'password' result = schema.unmarshal(value) assert result == 'password'
def test_number_valid(self): schema = Schema('number') value = 1.23 result = schema.unmarshal(value) assert result == value
def test_string_valid(self): schema = Schema('string') value = 'test' result = schema.unmarshal(value) assert result == value
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)
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
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'
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
def test_deprecated(self): schema = Schema('string', deprecated=True) value = 'test' with pytest.warns(DeprecationWarning): result = schema.unmarshal(value) assert result == value
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)
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'
def test_string_none(self): schema = Schema('string') value = None with pytest.raises(InvalidSchemaValue): schema.unmarshal(value)
def test_integer_invalid(self): schema = Schema('integer') value = 'abc' with pytest.raises(InvalidSchemaValue): schema.unmarshal(value)