test_processor.py 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. """
  23. Important note: Actually marshmallow don't validate when deserialize.
  24. But we think about make it possible:
  25. https://github.com/marshmallow-code/marshmallow/issues/684
  26. """
  27. processor = MarshmallowOutputProcessor()
  28. processor.schema = MySchema()
  29. tested_data = {
  30. 'last_name': 'Turing',
  31. }
  32. data = processor.process(tested_data)
  33. assert {
  34. 'last_name': 'Turing',
  35. } == data
  36. def test_unit__marshmallow_input_processor__ok__process_success(self):
  37. processor = MarshmallowInputProcessor()
  38. processor.schema = MySchema()
  39. tested_data = {
  40. 'first_name': 'Alan',
  41. 'last_name': 'Turing',
  42. }
  43. data = processor.process(tested_data)
  44. assert data == tested_data
  45. def test_unit__marshmallow_input_processor__error__validation_error(self):
  46. processor = MarshmallowInputProcessor()
  47. processor.schema = MySchema()
  48. tested_data = {
  49. 'last_name': 'Turing',
  50. }
  51. with pytest.raises(OutputValidationException):
  52. processor.process(tested_data)
  53. errors = processor.get_validation_error(tested_data)
  54. assert errors.error_details
  55. assert 'first_name' in errors.error_details
  56. def test_unit__marshmallow_input_processor__ok__completed_data(self):
  57. processor = MarshmallowInputProcessor()
  58. processor.schema = MySchema()
  59. tested_data = {
  60. 'first_name': 'Alan',
  61. }
  62. data = processor.process(tested_data)
  63. assert {
  64. 'first_name': 'Alan',
  65. 'last_name': 'Doe',
  66. } == data