/tests/test_memcached.py

https://github.com/madkote/fastapi-plugins
Python | 155 lines | 118 code | 24 blank | 13 comment | 10 complexity | c721169b263aeb489630f13a5c452433 MD5 | raw file
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # tests.test_memcached
  4. '''
  5. :author: madkote
  6. :contact: madkote(at)bluewin.ch
  7. :copyright: Copyright 2021, madkote
  8. tests.test_memcached
  9. --------------------
  10. Memcached tests
  11. '''
  12. from __future__ import absolute_import
  13. import asyncio
  14. import unittest
  15. import uuid
  16. import fastapi
  17. import fastapi_plugins
  18. import pytest
  19. from . import VERSION
  20. from . import d2json
  21. __all__ = []
  22. __author__ = 'roman-telepathy-ai <roman.schroeder(at)telepathy.ai>'
  23. __version__ = '.'.join(str(x) for x in VERSION)
  24. __copyright__ = 'Copyright 2021, Telepathy Labs'
  25. @pytest.mark.memcached
  26. class MemcachedTest(unittest.TestCase):
  27. def fixture_get_config(self):
  28. from fastapi_plugins.memcached import MemcachedSettings
  29. return MemcachedSettings(
  30. memcached_prestart_tries=10,
  31. memcached_prestart_wait=1
  32. )
  33. def test_connect(self):
  34. from fastapi_plugins.memcached import memcached_plugin
  35. async def _test():
  36. app = fastapi_plugins.register_middleware(fastapi.FastAPI())
  37. config = self.fixture_get_config()
  38. await memcached_plugin.init_app(app=app, config=config)
  39. await memcached_plugin.init()
  40. await memcached_plugin.terminate()
  41. event_loop = asyncio.new_event_loop()
  42. asyncio.set_event_loop(event_loop)
  43. coro = asyncio.coroutine(_test)
  44. event_loop.run_until_complete(coro())
  45. event_loop.close()
  46. def test_ping(self):
  47. from fastapi_plugins.memcached import memcached_plugin
  48. async def _test():
  49. app = fastapi_plugins.register_middleware(fastapi.FastAPI())
  50. config = self.fixture_get_config()
  51. await memcached_plugin.init_app(app=app, config=config)
  52. await memcached_plugin.init()
  53. try:
  54. c = await memcached_plugin()
  55. r = await c.ping()
  56. self.assertTrue(r, 'set failed')
  57. finally:
  58. await memcached_plugin.terminate()
  59. event_loop = asyncio.new_event_loop()
  60. asyncio.set_event_loop(event_loop)
  61. coro = asyncio.coroutine(_test)
  62. event_loop.run_until_complete(coro())
  63. event_loop.close()
  64. def test_health(self):
  65. from fastapi_plugins.memcached import memcached_plugin
  66. async def _test():
  67. app = fastapi_plugins.register_middleware(fastapi.FastAPI())
  68. config = self.fixture_get_config()
  69. await memcached_plugin.init_app(app=app, config=config)
  70. await memcached_plugin.init()
  71. try:
  72. exp = dict(
  73. host=config.memcached_host,
  74. port=config.memcached_port,
  75. version='1.6.9'
  76. )
  77. res = await memcached_plugin.health()
  78. self.assertTrue(
  79. d2json(exp) == d2json(res),
  80. 'health failed: %s != %s' % (exp, res)
  81. )
  82. finally:
  83. await memcached_plugin.terminate()
  84. event_loop = asyncio.new_event_loop()
  85. asyncio.set_event_loop(event_loop)
  86. coro = asyncio.coroutine(_test)
  87. event_loop.run_until_complete(coro())
  88. event_loop.close()
  89. def test_version(self):
  90. from fastapi_plugins.memcached import memcached_plugin
  91. async def _test():
  92. app = fastapi_plugins.register_middleware(fastapi.FastAPI())
  93. config = self.fixture_get_config()
  94. await memcached_plugin.init_app(app=app, config=config)
  95. await memcached_plugin.init()
  96. try:
  97. c = await memcached_plugin()
  98. r = await c.version()
  99. self.assertTrue(r, 'set failed')
  100. finally:
  101. await memcached_plugin.terminate()
  102. event_loop = asyncio.new_event_loop()
  103. asyncio.set_event_loop(event_loop)
  104. coro = asyncio.coroutine(_test)
  105. event_loop.run_until_complete(coro())
  106. event_loop.close()
  107. def test_get_set(self):
  108. from fastapi_plugins.memcached import memcached_plugin
  109. async def _test():
  110. app = fastapi_plugins.register_middleware(fastapi.FastAPI())
  111. config = self.fixture_get_config()
  112. await memcached_plugin.init_app(app=app, config=config)
  113. await memcached_plugin.init()
  114. try:
  115. c = await memcached_plugin()
  116. value = str(uuid.uuid4()).encode()
  117. r = await c.set(b'x', value)
  118. self.assertTrue(r, 'set failed')
  119. r = await c.get(b'x')
  120. self.assertTrue(r == value, 'get failed')
  121. finally:
  122. await memcached_plugin.terminate()
  123. event_loop = asyncio.new_event_loop()
  124. asyncio.set_event_loop(event_loop)
  125. coro = asyncio.coroutine(_test)
  126. event_loop.run_until_complete(coro())
  127. event_loop.close()
  128. if __name__ == "__main__":
  129. # import sys;sys.argv = ['', 'Test.testName']
  130. unittest.main()