test_marshmallow_decoration.py 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # coding: utf-8
  2. import json
  3. try: # Python 3.5+
  4. from http import HTTPStatus
  5. except ImportError:
  6. from http import client as HTTPStatus
  7. import marshmallow
  8. from hapic import Hapic
  9. from tests.base import Base
  10. from tests.base import MyContext
  11. class TestMarshmallowDecoration(Base):
  12. def test_unit__input_files__ok__file_is_present(self):
  13. hapic = Hapic()
  14. hapic.set_context(MyContext(
  15. app=None,
  16. fake_files_parameters={
  17. 'file_abc': '10101010101',
  18. }
  19. ))
  20. class MySchema(marshmallow.Schema):
  21. file_abc = marshmallow.fields.Raw(required=True)
  22. @hapic.input_files(MySchema())
  23. def my_controller(hapic_data=None):
  24. assert hapic_data
  25. assert hapic_data.files
  26. return 'OK'
  27. result = my_controller()
  28. assert 'OK' == result
  29. def test_unit__input_files__ok__file_is_not_present(self):
  30. hapic = Hapic()
  31. hapic.set_context(MyContext(
  32. app=None,
  33. fake_files_parameters={
  34. # No file here
  35. }
  36. ))
  37. class MySchema(marshmallow.Schema):
  38. file_abc = marshmallow.fields.Raw(required=True)
  39. @hapic.input_files(MySchema())
  40. def my_controller(hapic_data=None):
  41. assert hapic_data
  42. assert hapic_data.files
  43. return 'OK'
  44. result = my_controller()
  45. assert HTTPStatus.BAD_REQUEST == result.status_code
  46. assert {
  47. 'http_code': 400,
  48. 'original_error': {
  49. 'details': {
  50. 'file_abc': ['Missing data for required field.']
  51. },
  52. 'message': 'Validation error of input data'
  53. }
  54. } == json.loads(result.body)
  55. def test_unit__input_files__ok__file_is_empty_string(self):
  56. hapic = Hapic()
  57. hapic.set_context(MyContext(
  58. app=None,
  59. fake_files_parameters={
  60. 'file_abc': '',
  61. }
  62. ))
  63. class MySchema(marshmallow.Schema):
  64. file_abc = marshmallow.fields.Raw(required=True)
  65. @hapic.input_files(MySchema())
  66. def my_controller(hapic_data=None):
  67. assert hapic_data
  68. assert hapic_data.files
  69. return 'OK'
  70. result = my_controller()
  71. assert HTTPStatus.BAD_REQUEST == result.status_code
  72. assert {
  73. 'http_code': 400,
  74. 'original_error': {
  75. 'details': {
  76. 'file_abc': ['Missing data for required field']
  77. },
  78. 'message': 'Validation error of input data'
  79. }
  80. } == json.loads(result.body)