test_doc.py 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # coding: utf-8
  2. import marshmallow
  3. import bottle
  4. from hapic import Hapic
  5. from tests.base import Base
  6. from tests.base import MyContext
  7. class TestDocGeneration(Base):
  8. def test_func__input_files_doc__ok__one_file(self):
  9. hapic = Hapic()
  10. hapic.set_context(MyContext())
  11. app = bottle.Bottle()
  12. class MySchema(marshmallow.Schema):
  13. file_abc = marshmallow.fields.Raw(required=True)
  14. @hapic.with_api_doc()
  15. @hapic.input_files(MySchema())
  16. def my_controller(hapic_data=None):
  17. assert hapic_data
  18. assert hapic_data.files
  19. app.route('/upload', method='POST', callback=my_controller)
  20. doc = hapic.generate_doc(app)
  21. assert doc
  22. assert '/upload' in doc['paths']
  23. assert 'consume' in doc['paths']['/upload']['post']
  24. assert 'multipart/form-data' in doc['paths']['/upload']['post']['consume']
  25. assert 'parameters' in doc['paths']['/upload']['post']
  26. assert {
  27. 'name': 'file_abc',
  28. 'required': True,
  29. 'in': 'formData',
  30. 'type': 'file',
  31. } in doc['paths']['/upload']['post']['parameters']
  32. def test_func__input_files_doc__ok__two_file(self):
  33. hapic = Hapic()
  34. hapic.set_context(MyContext())
  35. app = bottle.Bottle()
  36. class MySchema(marshmallow.Schema):
  37. file_abc = marshmallow.fields.Raw(required=True)
  38. file_def = marshmallow.fields.Raw(required=False)
  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. app.route('/upload', method='POST', callback=my_controller)
  45. doc = hapic.generate_doc(app)
  46. assert doc
  47. assert '/upload' in doc['paths']
  48. assert 'consume' in doc['paths']['/upload']['post']
  49. assert 'multipart/form-data' in doc['paths']['/upload']['post']['consume']
  50. assert 'parameters' in doc['paths']['/upload']['post']
  51. assert {
  52. 'name': 'file_abc',
  53. 'required': True,
  54. 'in': 'formData',
  55. 'type': 'file',
  56. } in doc['paths']['/upload']['post']['parameters']
  57. assert {
  58. 'name': 'file_def',
  59. 'required': False,
  60. 'in': 'formData',
  61. 'type': 'file',
  62. } in doc['paths']['/upload']['post']['parameters']
  63. def test_func__output_file_doc__ok__nominal_case(self):
  64. hapic = Hapic()
  65. hapic.set_context(MyContext())
  66. app = bottle.Bottle()
  67. @hapic.with_api_doc()
  68. @hapic.output_file('image/jpeg')
  69. def my_controller():
  70. return b'101010100101'
  71. app.route('/avatar', method='GET', callback=my_controller)
  72. doc = hapic.generate_doc(app)
  73. assert doc
  74. assert '/avatar' in doc['paths']
  75. assert 'produce' in doc['paths']['/avatar']['get']
  76. assert 'image/jpeg' in doc['paths']['/avatar']['get']['produce']
  77. assert 200 in doc['paths']['/avatar']['get']['responses']