| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 | # -*- coding: utf-8 -*-
import marshmallow as marshmallow
import pytest
from hapic.exception import OutputValidationException
from hapic.processor import MarshmallowOutputProcessor
from hapic.processor import MarshmallowInputProcessor
from tests.base import Base
class MySchema(marshmallow.Schema):
    first_name = marshmallow.fields.String(required=True)
    last_name = marshmallow.fields.String(missing='Doe')
class TestProcessor(Base):
    def test_unit__marshmallow_output_processor__ok__process_success(self):
        processor = MarshmallowOutputProcessor()
        processor.schema = MySchema()
        tested_data = {
            'first_name': 'Alan',
            'last_name': 'Turing',
        }
        data = processor.process(tested_data)
        assert data == tested_data
    def test_unit__marshmallow_output_processor__ok__missing_data(self):
        processor = MarshmallowOutputProcessor()
        processor.schema = MySchema()
        tested_data = {
            'last_name': 'Turing',
        }
        with pytest.raises(OutputValidationException):
            processor.process(tested_data)
    def test_unit__marshmallow_input_processor__ok__process_success(self):
        processor = MarshmallowInputProcessor()
        processor.schema = MySchema()
        tested_data = {
            'first_name': 'Alan',
            'last_name': 'Turing',
        }
        data = processor.process(tested_data)
        assert data == tested_data
    def test_unit__marshmallow_input_processor__error__validation_error(self):
        processor = MarshmallowInputProcessor()
        processor.schema = MySchema()
        tested_data = {
            # Missing 'first_name' key
            'last_name': 'Turing',
        }
        with pytest.raises(OutputValidationException):
            processor.process(tested_data)
        errors = processor.get_validation_error(tested_data)
        assert errors.details
        assert 'first_name' in errors.details
    def test_unit__marshmallow_input_processor__error__validation_error_no_data(self):  # nopep8
        processor = MarshmallowInputProcessor()
        processor.schema = MySchema()
        # Schema will not valid it because require first_name field
        tested_data = {}
        with pytest.raises(OutputValidationException):
            processor.process(tested_data)
        errors = processor.get_validation_error(tested_data)
        assert errors.details
        assert 'first_name' in errors.details
    def test_unit__marshmallow_input_processor__error__validation_error_no_data_none(self):  # nopep8
        processor = MarshmallowInputProcessor()
        processor.schema = MySchema()
        # Schema will not valid it because require first_name field
        tested_data = None
        with pytest.raises(OutputValidationException):
            processor.process(tested_data)
        errors = processor.get_validation_error(tested_data)
        assert errors.details
        assert 'first_name' in errors.details
    def test_unit__marshmallow_input_processor__error__validation_error_no_data_empty_string(self):  # nopep8
        processor = MarshmallowInputProcessor()
        processor.schema = MySchema()
        # Schema will not valid it because require first_name field
        tested_data = ''
        with pytest.raises(OutputValidationException):
            processor.process(tested_data)
        errors = processor.get_validation_error(tested_data)
        assert errors.details
        assert {'_schema': ['Invalid input type.']} == errors.details
    def test_unit__marshmallow_input_processor__ok__completed_data(self):
        processor = MarshmallowInputProcessor()
        processor.schema = MySchema()
        tested_data = {
            'first_name': 'Alan',
        }
        data = processor.process(tested_data)
        assert {
            'first_name': 'Alan',
            'last_name': 'Doe',
        } == data
 |