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