test_marshmallow_decoration.py 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # coding: utf-8
  2. from http import HTTPStatus
  3. import marshmallow
  4. from hapic import Hapic
  5. from tests.base import Base
  6. from tests.base import MyContext
  7. class TestMarshmallowDecoration(Base):
  8. def test_unit__input_files__ok__file_is_present(self):
  9. hapic = Hapic()
  10. hapic.set_context(MyContext(
  11. app=None,
  12. fake_files_parameters={
  13. 'file_abc': '10101010101',
  14. }
  15. ))
  16. class MySchema(marshmallow.Schema):
  17. file_abc = marshmallow.fields.Raw(required=True)
  18. @hapic.with_api_doc()
  19. @hapic.input_files(MySchema())
  20. def my_controller(hapic_data=None):
  21. assert hapic_data
  22. assert hapic_data.files
  23. return 'OK'
  24. result = my_controller()
  25. assert 'OK' == result
  26. def test_unit__input_files__ok__file_is_not_present(self):
  27. hapic = Hapic()
  28. hapic.set_context(MyContext(
  29. app=None,
  30. fake_files_parameters={
  31. # No file here
  32. }
  33. ))
  34. class MySchema(marshmallow.Schema):
  35. file_abc = marshmallow.fields.Raw(required=True)
  36. @hapic.with_api_doc()
  37. @hapic.input_files(MySchema())
  38. def my_controller(hapic_data=None):
  39. assert hapic_data
  40. assert hapic_data.files
  41. return 'OK'
  42. result = my_controller()
  43. assert 'http_code' in result
  44. assert HTTPStatus.BAD_REQUEST == result['http_code']
  45. assert {
  46. 'file_abc': ['Missing data for required field.']
  47. } == result['original_error'].details
  48. def test_unit__input_files__ok__file_is_empty_string(self):
  49. hapic = Hapic()
  50. hapic.set_context(MyContext(
  51. app=None,
  52. fake_files_parameters={
  53. 'file_abc': '',
  54. }
  55. ))
  56. class MySchema(marshmallow.Schema):
  57. file_abc = marshmallow.fields.Raw(required=True)
  58. @hapic.with_api_doc()
  59. @hapic.input_files(MySchema())
  60. def my_controller(hapic_data=None):
  61. assert hapic_data
  62. assert hapic_data.files
  63. return 'OK'
  64. result = my_controller()
  65. assert 'http_code' in result
  66. assert HTTPStatus.BAD_REQUEST == result['http_code']
  67. assert {'file_abc': ['Missing data for required field']} == result['original_error'].details