test_bottle.py 2.3KB

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