test_doc.py 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. # TODO BS 20171113: Make this test non-bottle
  11. app = bottle.Bottle()
  12. hapic.set_context(MyContext(app=app))
  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. app.route('/upload', method='POST', callback=my_controller)
  21. doc = hapic.generate_doc()
  22. assert doc
  23. assert '/upload' in doc['paths']
  24. assert 'consumes' in doc['paths']['/upload']['post']
  25. assert 'multipart/form-data' in doc['paths']['/upload']['post']['consumes'] # nopep8
  26. assert 'parameters' in doc['paths']['/upload']['post']
  27. assert {
  28. 'name': 'file_abc',
  29. 'required': True,
  30. 'in': 'formData',
  31. 'type': 'file',
  32. } in doc['paths']['/upload']['post']['parameters']
  33. def test_func__input_files_doc__ok__two_file(self):
  34. hapic = Hapic()
  35. # TODO BS 20171113: Make this test non-bottle
  36. app = bottle.Bottle()
  37. hapic.set_context(MyContext(app=app))
  38. class MySchema(marshmallow.Schema):
  39. file_abc = marshmallow.fields.Raw(required=True)
  40. file_def = marshmallow.fields.Raw(required=False)
  41. @hapic.with_api_doc()
  42. @hapic.input_files(MySchema())
  43. def my_controller(hapic_data=None):
  44. assert hapic_data
  45. assert hapic_data.files
  46. app.route('/upload', method='POST', callback=my_controller)
  47. doc = hapic.generate_doc()
  48. assert doc
  49. assert '/upload' in doc['paths']
  50. assert 'consumes' in doc['paths']['/upload']['post']
  51. assert 'multipart/form-data' in doc['paths']['/upload']['post']['consumes'] # nopep8
  52. assert 'parameters' in doc['paths']['/upload']['post']
  53. assert {
  54. 'name': 'file_abc',
  55. 'required': True,
  56. 'in': 'formData',
  57. 'type': 'file',
  58. } in doc['paths']['/upload']['post']['parameters']
  59. assert {
  60. 'name': 'file_def',
  61. 'required': False,
  62. 'in': 'formData',
  63. 'type': 'file',
  64. } in doc['paths']['/upload']['post']['parameters']
  65. def test_func__output_file_doc__ok__nominal_case(self):
  66. hapic = Hapic()
  67. # TODO BS 20171113: Make this test non-bottle
  68. app = bottle.Bottle()
  69. hapic.set_context(MyContext(app=app))
  70. @hapic.with_api_doc()
  71. @hapic.output_file(['image/jpeg'])
  72. def my_controller():
  73. return b'101010100101'
  74. app.route('/avatar', method='GET', callback=my_controller)
  75. doc = hapic.generate_doc()
  76. assert doc
  77. assert '/avatar' in doc['paths']
  78. assert 'produces' in doc['paths']['/avatar']['get']
  79. assert 'image/jpeg' in doc['paths']['/avatar']['get']['produces']
  80. assert 200 in doc['paths']['/avatar']['get']['responses']
  81. def test_func__input_files_doc__ok__one_file_and_text(self):
  82. hapic = Hapic()
  83. # TODO BS 20171113: Make this test non-bottle
  84. app = bottle.Bottle()
  85. hapic.set_context(MyContext(app=app))
  86. class MySchema(marshmallow.Schema):
  87. name = marshmallow.fields.String(required=True)
  88. class MyFilesSchema(marshmallow.Schema):
  89. file_abc = marshmallow.fields.Raw(required=True)
  90. @hapic.with_api_doc()
  91. @hapic.input_files(MyFilesSchema())
  92. @hapic.input_body(MySchema())
  93. def my_controller(hapic_data=None):
  94. assert hapic_data
  95. assert hapic_data.files
  96. app.route('/upload', method='POST', callback=my_controller)
  97. doc = hapic.generate_doc()
  98. assert doc
  99. assert '/upload' in doc['paths']
  100. assert 'consumes' in doc['paths']['/upload']['post']
  101. assert 'multipart/form-data' in doc['paths']['/upload']['post']['consumes'] # nopep8
  102. assert 'parameters' in doc['paths']['/upload']['post']
  103. assert {
  104. 'name': 'file_abc',
  105. 'required': True,
  106. 'in': 'formData',
  107. 'type': 'file',
  108. } in doc['paths']['/upload']['post']['parameters']
  109. def test_func__docstring__ok__simple_case(self):
  110. hapic = Hapic()
  111. app = bottle.Bottle()
  112. hapic.set_context(MyContext(app=app))
  113. # TODO BS 20171113: Make this test non-bottle
  114. @hapic.with_api_doc()
  115. def my_controller(hapic_data=None):
  116. """
  117. Hello doc
  118. """
  119. assert hapic_data
  120. assert hapic_data.files
  121. app.route('/upload', method='POST', callback=my_controller)
  122. doc = hapic.generate_doc()
  123. assert doc.get('paths')
  124. assert '/upload' in doc['paths']
  125. assert 'post' in doc['paths']['/upload']
  126. assert 'description' in doc['paths']['/upload']['post']
  127. assert 'Hello doc' == doc['paths']['/upload']['post']['description']
  128. def test_func__tags__ok__nominal_case(self):
  129. hapic = Hapic()
  130. app = bottle.Bottle()
  131. hapic.set_context(MyContext(app=app))
  132. @hapic.with_api_doc(tags=['foo', 'bar'])
  133. def my_controller(hapic_data=None):
  134. assert hapic_data
  135. assert hapic_data.files
  136. app.route('/upload', method='POST', callback=my_controller)
  137. doc = hapic.generate_doc()
  138. assert doc.get('paths')
  139. assert '/upload' in doc['paths']
  140. assert 'post' in doc['paths']['/upload']
  141. assert 'tags' in doc['paths']['/upload']['post']
  142. assert ['foo', 'bar'] == doc['paths']['/upload']['post']['tags']