test_doc.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 'consumes' in doc['paths']['/upload']['post']
  24. assert 'multipart/form-data' in doc['paths']['/upload']['post']['consumes'] # nopep8
  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 'consumes' in doc['paths']['/upload']['post']
  49. assert 'multipart/form-data' in doc['paths']['/upload']['post']['consumes'] # nopep8
  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 'produces' in doc['paths']['/avatar']['get']
  76. assert 'image/jpeg' in doc['paths']['/avatar']['get']['produces']
  77. assert 200 in doc['paths']['/avatar']['get']['responses']
  78. def test_func__input_files_doc__ok__one_file_and_text(self):
  79. hapic = Hapic()
  80. hapic.set_context(MyContext())
  81. app = bottle.Bottle()
  82. class MySchema(marshmallow.Schema):
  83. name = marshmallow.fields.String(required=True)
  84. class MyFilesSchema(marshmallow.Schema):
  85. file_abc = marshmallow.fields.Raw(required=True)
  86. @hapic.with_api_doc()
  87. @hapic.input_files(MyFilesSchema())
  88. @hapic.input_body(MySchema())
  89. def my_controller(hapic_data=None):
  90. assert hapic_data
  91. assert hapic_data.files
  92. app.route('/upload', method='POST', callback=my_controller)
  93. doc = hapic.generate_doc(app)
  94. assert doc
  95. assert '/upload' in doc['paths']
  96. assert 'consumes' in doc['paths']['/upload']['post']
  97. assert 'multipart/form-data' in doc['paths']['/upload']['post']['consumes'] # nopep8
  98. assert 'parameters' in doc['paths']['/upload']['post']
  99. assert {
  100. 'name': 'file_abc',
  101. 'required': True,
  102. 'in': 'formData',
  103. 'type': 'file',
  104. } in doc['paths']['/upload']['post']['parameters']
  105. def test_func__docstring__ok__simple_case(self):
  106. hapic = Hapic()
  107. hapic.set_context(MyContext())
  108. app = bottle.Bottle()
  109. # TODO BS 20171113: Make this test non-bottle
  110. @hapic.with_api_doc()
  111. def my_controller(hapic_data=None):
  112. """
  113. Hello doc
  114. """
  115. assert hapic_data
  116. assert hapic_data.files
  117. app.route('/upload', method='POST', callback=my_controller)
  118. doc = hapic.generate_doc(app)
  119. assert doc.get('paths')
  120. assert '/upload' in doc['paths']
  121. assert 'post' in doc['paths']['/upload']
  122. assert 'description' in doc['paths']['/upload']['post']
  123. assert 'Hello doc' == doc['paths']['/upload']['post']['description']