test_marshmallow_decoration.py 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # coding: utf-8
  2. try: # Python 3.5+
  3. from http import HTTPStatus
  4. except ImportError:
  5. from http import client as HTTPStatus
  6. import marshmallow
  7. from hapic import Hapic
  8. from tests.base import Base
  9. from tests.base import MyContext
  10. class TestMarshmallowDecoration(Base):
  11. def test_unit__input_files__ok__file_is_present(self):
  12. hapic = Hapic()
  13. hapic.set_context(MyContext(
  14. app=None,
  15. fake_files_parameters={
  16. 'file_abc': '10101010101',
  17. }
  18. ))
  19. class MySchema(marshmallow.Schema):
  20. file_abc = marshmallow.fields.Raw(required=True)
  21. @hapic.with_api_doc()
  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.with_api_doc()
  40. @hapic.input_files(MySchema())
  41. def my_controller(hapic_data=None):
  42. assert hapic_data
  43. assert hapic_data.files
  44. return 'OK'
  45. result = my_controller()
  46. assert 'http_code' in result
  47. assert HTTPStatus.BAD_REQUEST == result['http_code']
  48. assert {
  49. 'file_abc': ['Missing data for required field.']
  50. } == result['original_error'].details
  51. def test_unit__input_files__ok__file_is_empty_string(self):
  52. hapic = Hapic()
  53. hapic.set_context(MyContext(
  54. app=None,
  55. fake_files_parameters={
  56. 'file_abc': '',
  57. }
  58. ))
  59. class MySchema(marshmallow.Schema):
  60. file_abc = marshmallow.fields.Raw(required=True)
  61. @hapic.with_api_doc()
  62. @hapic.input_files(MySchema())
  63. def my_controller(hapic_data=None):
  64. assert hapic_data
  65. assert hapic_data.files
  66. return 'OK'
  67. result = my_controller()
  68. assert 'http_code' in result
  69. assert HTTPStatus.BAD_REQUEST == result['http_code']
  70. assert {'file_abc': ['Missing data for required field']} == result['original_error'].details