test_bottle.py 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # -*- coding: utf-8 -*-
  2. import bottle
  3. import hapic
  4. from tests.base import Base
  5. class TestBottleExt(Base):
  6. def test_unit__map_binding__ok__decorated_function(self):
  7. hapic_ = hapic.Hapic()
  8. app = bottle.Bottle()
  9. context = hapic.ext.bottle.BottleContext(app=app)
  10. hapic_.set_context(context)
  11. @hapic_.with_api_doc()
  12. @app.route('/')
  13. def controller_a():
  14. pass
  15. assert hapic_.controllers
  16. decoration = hapic_.controllers[0]
  17. route = context.find_route(decoration)
  18. assert route
  19. assert route.original_route_object.callback != controller_a
  20. assert route.original_route_object.callback == decoration.reference.wrapped # nopep8
  21. assert route.original_route_object.callback != decoration.reference.wrapper # nopep8
  22. def test_unit__map_binding__ok__mapped_function(self):
  23. hapic_ = hapic.Hapic()
  24. app = bottle.Bottle()
  25. context = hapic.ext.bottle.BottleContext(app=app)
  26. hapic_.set_context(context)
  27. @hapic_.with_api_doc()
  28. def controller_a():
  29. pass
  30. app.route('/', callback=controller_a)
  31. assert hapic_.controllers
  32. decoration = hapic_.controllers[0]
  33. route = context.find_route(decoration)
  34. assert route
  35. assert route.original_route_object.callback == controller_a
  36. assert route.original_route_object.callback == decoration.reference.wrapper # nopep8
  37. assert route.original_route_object.callback != decoration.reference.wrapped # nopep8
  38. def test_unit__map_binding__ok__mapped_method(self):
  39. hapic_ = hapic.Hapic()
  40. app = bottle.Bottle()
  41. context = hapic.ext.bottle.BottleContext(app=app)
  42. hapic_.set_context(context)
  43. class MyControllers(object):
  44. def bind(self, app):
  45. app.route('/', callback=self.controller_a)
  46. @hapic_.with_api_doc()
  47. def controller_a(self):
  48. pass
  49. my_controllers = MyControllers()
  50. my_controllers.bind(app)
  51. assert hapic_.controllers
  52. decoration = hapic_.controllers[0]
  53. route = context.find_route(decoration)
  54. assert route
  55. # Important note: instance controller_a method is
  56. # not class controller_a, so no matches with callbacks
  57. assert route.original_route_object.callback != MyControllers.controller_a # nopep8
  58. assert route.original_route_object.callback != decoration.reference.wrapped # nopep8
  59. assert route.original_route_object.callback != decoration.reference.wrapper # nopep8