test_marshmallow_decoration.py 2.4KB

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