test_processor.py 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # -*- coding: utf-8 -*-
  2. import marshmallow as marshmallow
  3. import pytest
  4. from hapic.exception import OutputValidationException
  5. from hapic.processor import MarshmallowOutputProcessor
  6. from hapic.processor import MarshmallowInputProcessor
  7. from tests.base import Base
  8. class MySchema(marshmallow.Schema):
  9. first_name = marshmallow.fields.String(required=True)
  10. last_name = marshmallow.fields.String(missing='Doe')
  11. class TestProcessor(Base):
  12. def test_unit__marshmallow_output_processor__ok__process_success(self):
  13. processor = MarshmallowOutputProcessor()
  14. processor.schema = MySchema()
  15. tested_data = {
  16. 'first_name': 'Alan',
  17. 'last_name': 'Turing',
  18. }
  19. data = processor.process(tested_data)
  20. assert data == tested_data
  21. def test_unit__marshmallow_output_processor__ok__missing_data(self):
  22. processor = MarshmallowOutputProcessor()
  23. processor.schema = MySchema()
  24. tested_data = {
  25. 'last_name': 'Turing',
  26. }
  27. with pytest.raises(OutputValidationException):
  28. processor.process(tested_data)
  29. def test_unit__marshmallow_input_processor__ok__process_success(self):
  30. processor = MarshmallowInputProcessor()
  31. processor.schema = MySchema()
  32. tested_data = {
  33. 'first_name': 'Alan',
  34. 'last_name': 'Turing',
  35. }
  36. data = processor.process(tested_data)
  37. assert data == tested_data
  38. def test_unit__marshmallow_input_processor__error__validation_error(self):
  39. processor = MarshmallowInputProcessor()
  40. processor.schema = MySchema()
  41. tested_data = {
  42. 'last_name': 'Turing',
  43. }
  44. with pytest.raises(OutputValidationException):
  45. processor.process(tested_data)
  46. errors = processor.get_validation_error(tested_data)
  47. assert errors.error_details
  48. assert 'first_name' in errors.error_details
  49. def test_unit__marshmallow_input_processor__ok__completed_data(self):
  50. processor = MarshmallowInputProcessor()
  51. processor.schema = MySchema()
  52. tested_data = {
  53. 'first_name': 'Alan',
  54. }
  55. data = processor.process(tested_data)
  56. assert {
  57. 'first_name': 'Alan',
  58. 'last_name': 'Doe',
  59. } == data