test_bottle.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. import bottle
  3. from webtest import TestApp
  4. import hapic
  5. from hapic.ext.bottle import BottleContext
  6. from tests.base import Base
  7. class TestBottleExt(Base):
  8. def test_unit__map_binding__ok__decorated_function(self):
  9. hapic_ = hapic.Hapic()
  10. app = bottle.Bottle()
  11. context = hapic.ext.bottle.BottleContext(app=app)
  12. hapic_.set_context(context)
  13. @hapic_.with_api_doc()
  14. @app.route('/')
  15. def controller_a():
  16. pass
  17. assert hapic_.controllers
  18. decoration = hapic_.controllers[0]
  19. route = context.find_route(decoration)
  20. assert route
  21. assert route.original_route_object.callback != controller_a
  22. assert route.original_route_object.callback == decoration.reference.wrapped # nopep8
  23. assert route.original_route_object.callback != decoration.reference.wrapper # nopep8
  24. def test_unit__map_binding__ok__mapped_function(self):
  25. hapic_ = hapic.Hapic()
  26. app = bottle.Bottle()
  27. context = hapic.ext.bottle.BottleContext(app=app)
  28. hapic_.set_context(context)
  29. @hapic_.with_api_doc()
  30. def controller_a():
  31. pass
  32. app.route('/', callback=controller_a)
  33. assert hapic_.controllers
  34. decoration = hapic_.controllers[0]
  35. route = context.find_route(decoration)
  36. assert route
  37. assert route.original_route_object.callback == controller_a
  38. assert route.original_route_object.callback == decoration.reference.wrapper # nopep8
  39. assert route.original_route_object.callback != decoration.reference.wrapped # nopep8
  40. def test_unit__map_binding__ok__mapped_method(self):
  41. hapic_ = hapic.Hapic()
  42. app = bottle.Bottle()
  43. context = hapic.ext.bottle.BottleContext(app=app)
  44. hapic_.set_context(context)
  45. class MyControllers(object):
  46. def bind(self, app):
  47. app.route('/', callback=self.controller_a)
  48. @hapic_.with_api_doc()
  49. def controller_a(self):
  50. pass
  51. my_controllers = MyControllers()
  52. my_controllers.bind(app)
  53. assert hapic_.controllers
  54. decoration = hapic_.controllers[0]
  55. route = context.find_route(decoration)
  56. assert route
  57. # Important note: instance controller_a method is
  58. # not class controller_a, so no matches with callbacks
  59. assert route.original_route_object.callback != MyControllers.controller_a # nopep8
  60. assert route.original_route_object.callback != decoration.reference.wrapped # nopep8
  61. assert route.original_route_object.callback != decoration.reference.wrapper # nopep8
  62. def test_unit__general_exception_handling__ok__nominal_case(self):
  63. hapic_ = hapic.Hapic()
  64. app = bottle.Bottle()
  65. context = BottleContext(app=app)
  66. hapic_.set_context(context)
  67. def my_view():
  68. raise ZeroDivisionError('An exception message')
  69. app.route('/my-view', method='GET', callback=my_view)
  70. context.handle_exception(ZeroDivisionError, http_code=400)
  71. test_app = TestApp(app)
  72. response = test_app.get('/my-view', status='*')
  73. assert 400 == response.status_code