test_marshmallow_decoration.py 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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(fake_files_parameters={
  11. 'file_abc': '10101010101',
  12. }))
  13. class MySchema(marshmallow.Schema):
  14. file_abc = marshmallow.fields.Raw(required=True)
  15. @hapic.with_api_doc()
  16. @hapic.input_files(MySchema())
  17. def my_controller(hapic_data=None):
  18. assert hapic_data
  19. assert hapic_data.files
  20. return 'OK'
  21. result = my_controller()
  22. assert 'OK' == result
  23. def test_unit__input_files__ok__file_is_not_present(self):
  24. hapic = Hapic()
  25. hapic.set_context(MyContext(fake_files_parameters={
  26. # No file here
  27. }))
  28. class MySchema(marshmallow.Schema):
  29. file_abc = marshmallow.fields.Raw(required=True)
  30. @hapic.with_api_doc()
  31. @hapic.input_files(MySchema())
  32. def my_controller(hapic_data=None):
  33. assert hapic_data
  34. assert hapic_data.files
  35. return 'OK'
  36. result = my_controller()
  37. assert 'http_code' in result
  38. assert HTTPStatus.BAD_REQUEST == result['http_code']
  39. assert {
  40. 'file_abc': ['Missing data for required field.']
  41. } == result['original_error'].details
  42. def test_unit__input_files__ok__file_is_empty_string(self):
  43. hapic = Hapic()
  44. hapic.set_context(MyContext(fake_files_parameters={
  45. 'file_abc': '',
  46. }))
  47. class MySchema(marshmallow.Schema):
  48. file_abc = marshmallow.fields.Raw(required=True)
  49. @hapic.with_api_doc()
  50. @hapic.input_files(MySchema())
  51. def my_controller(hapic_data=None):
  52. assert hapic_data
  53. assert hapic_data.files
  54. return 'OK'
  55. result = my_controller()
  56. assert 'http_code' in result
  57. assert HTTPStatus.BAD_REQUEST == result['http_code']
  58. assert {'file_abc': ['Missing data for required field']} == result['original_error'].details