test_processor.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. # Missing 'first_name' key
  43. 'last_name': 'Turing',
  44. }
  45. with pytest.raises(OutputValidationException):
  46. processor.process(tested_data)
  47. errors = processor.get_validation_error(tested_data)
  48. assert errors.details
  49. assert 'first_name' in errors.details
  50. def test_unit__marshmallow_input_processor__error__validation_error_no_data(self): # nopep8
  51. processor = MarshmallowInputProcessor()
  52. processor.schema = MySchema()
  53. # Schema will not valid it because require first_name field
  54. tested_data = {}
  55. with pytest.raises(OutputValidationException):
  56. processor.process(tested_data)
  57. errors = processor.get_validation_error(tested_data)
  58. assert errors.details
  59. assert 'first_name' in errors.details
  60. def test_unit__marshmallow_input_processor__error__validation_error_no_data_none(self): # nopep8
  61. processor = MarshmallowInputProcessor()
  62. processor.schema = MySchema()
  63. # Schema will not valid it because require first_name field
  64. tested_data = None
  65. with pytest.raises(OutputValidationException):
  66. processor.process(tested_data)
  67. errors = processor.get_validation_error(tested_data)
  68. assert errors.details
  69. assert 'first_name' in errors.details
  70. def test_unit__marshmallow_input_processor__error__validation_error_no_data_empty_string(self): # nopep8
  71. processor = MarshmallowInputProcessor()
  72. processor.schema = MySchema()
  73. # Schema will not valid it because require first_name field
  74. tested_data = ''
  75. with pytest.raises(OutputValidationException):
  76. processor.process(tested_data)
  77. errors = processor.get_validation_error(tested_data)
  78. assert errors.details
  79. assert {'_schema': ['Invalid input type.']} == errors.details
  80. def test_unit__marshmallow_input_processor__ok__completed_data(self):
  81. processor = MarshmallowInputProcessor()
  82. processor.schema = MySchema()
  83. tested_data = {
  84. 'first_name': 'Alan',
  85. }
  86. data = processor.process(tested_data)
  87. assert {
  88. 'first_name': 'Alan',
  89. 'last_name': 'Doe',
  90. } == data