1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
import errno
import os
import flask
import pytest
import flask_caching as fsc
try:
__import__("pytest_xprocess")
from xprocess import ProcessStarter
except ImportError:
@pytest.fixture(scope="session")
def xprocess():
pytest.skip("pytest-xprocess not installed.")
@pytest.fixture
def app(request):
app = flask.Flask(
request.module.__name__, template_folder=os.path.dirname(__file__)
)
app.testing = True
app.config["CACHE_TYPE"] = "simple"
return app
@pytest.fixture
def cache(app):
return fsc.Cache(app)
@pytest.fixture(
params=[method for method in fsc.SUPPORTED_HASH_FUNCTIONS],
ids=[method.__name__ for method in fsc.SUPPORTED_HASH_FUNCTIONS],
)
def hash_method(request):
return request.param
@pytest.fixture(scope="class")
def redis_server(xprocess):
try:
import redis # noqa
except ImportError:
pytest.skip("Python package 'redis' is not installed.")
class Starter(ProcessStarter):
pattern = "[Rr]eady to accept connections"
args = ["redis-server"]
try:
xprocess.ensure("redis_server", Starter)
except OSError as e:
# xprocess raises FileNotFoundError
if e.errno == errno.ENOENT:
pytest.skip("Redis is not installed.")
else:
raise
yield
xprocess.getinfo("redis_server").terminate()
@pytest.fixture(scope="class")
def memcache_server(xprocess):
try:
import pylibmc as memcache
except ImportError:
try:
from google.appengine.api import memcache
except ImportError:
try:
import memcache # noqa
except ImportError:
pytest.skip(
"Python package for memcache is not installed. Need one of "
"pylibmc', 'google.appengine', or 'memcache'."
)
class Starter(ProcessStarter):
pattern = ""
args = ["memcached", "-vv"]
try:
xprocess.ensure("memcached", Starter)
except OSError as e:
# xprocess raises FileNotFoundError
if e.errno == errno.ENOENT:
pytest.skip("Memcached is not installed.")
else:
raise
yield
xprocess.getinfo("memcached").terminate()
|