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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
"""Test Flask scopes."""
import flask
import pytest
from picobox.ext import flaskscopes
@pytest.fixture()
def appscope():
return flaskscopes.application()
@pytest.fixture()
def reqscope():
return flaskscopes.request()
@pytest.fixture()
def flaskapp():
return flask.Flask("test")
@pytest.fixture()
def appcontext(flaskapp):
return flaskapp.app_context
@pytest.fixture()
def reqcontext(flaskapp):
return flaskapp.test_request_context
@pytest.mark.parametrize(
("scopename", "ctx"),
[
("appscope", "appcontext"),
("reqscope", "reqcontext"),
],
)
def test_scope_set(request, scopename, ctx, supported_key, supported_value):
scope = request.getfixturevalue(scopename)
ctxfn = request.getfixturevalue(ctx)
with ctxfn():
scope.set(supported_key, supported_value)
assert scope.get(supported_key) is supported_value
@pytest.mark.parametrize(
"scopename",
[
"appscope",
"reqscope",
],
)
def test_scope_set_nocontext(request, scopename):
scope = request.getfixturevalue(scopename)
with pytest.raises(RuntimeError) as excinfo:
scope.set("the-key", "the-value")
excinfo.match("Working outside of application context.")
@pytest.mark.parametrize(
("scopename", "ctx"),
[
("appscope", "appcontext"),
("reqscope", "reqcontext"),
],
)
def test_scope_set_value_overwrite(request, scopename, ctx):
scope = request.getfixturevalue(scopename)
ctxfn = request.getfixturevalue(ctx)
value = object()
with ctxfn():
scope.set("the-key", value)
assert scope.get("the-key") is value
scope.set("the-key", "overwrite")
assert scope.get("the-key") == "overwrite"
@pytest.mark.parametrize(
("scopename", "ctx"),
[
("appscope", "appcontext"),
("reqscope", "reqcontext"),
],
)
def test_scope_get_keyerror(request, scopename, ctx, supported_key):
scope = request.getfixturevalue(scopename)
ctxfn = request.getfixturevalue(ctx)
with pytest.raises(KeyError, match=repr(supported_key)):
with ctxfn():
scope.get(supported_key)
@pytest.mark.parametrize(
("scopename", "ctx"),
[
("appscope", "appcontext"),
("reqscope", "reqcontext"),
],
)
def test_scope_state_not_leaked(request, scopename, ctx):
ctxfn = request.getfixturevalue(ctx)
scope_a = request.getfixturevalue(scopename)
value_a = object()
scope_b = type(scope_a)()
value_b = object()
with ctxfn():
scope_a.set("the-key", value_a)
assert scope_a.get("the-key") is value_a
with pytest.raises(KeyError, match="the-key"):
scope_b.get("the-key")
scope_b.set("the-key", value_b)
assert scope_b.get("the-key") is value_b
scope_a.set("the-key", value_a)
assert scope_a.get("the-key") is value_a
@pytest.mark.parametrize(
("scopename", "ctx"),
[
("appscope", "appcontext"),
],
)
def test_scope_value_shared(request, scopename, ctx):
scope = request.getfixturevalue(scopename)
ctxfn = request.getfixturevalue(ctx)
value = object()
with ctxfn():
scope.set("the-key", value)
with ctxfn():
assert scope.get("the-key") is value
@pytest.mark.parametrize(
("scopename", "ctx"),
[
("reqscope", "reqcontext"),
],
)
def test_scope_value_not_shared(request, scopename, ctx):
scope = request.getfixturevalue(scopename)
ctxfn = request.getfixturevalue(ctx)
with ctxfn():
scope.set("the-key", "the-value")
with pytest.raises(KeyError, match="the-key"):
with ctxfn():
scope.get("the-key")
def test_scope_value_not_shared_between_apps(appscope):
eggs = flask.Flask("eggs")
rice = flask.Flask("rice")
value = object()
with eggs.app_context():
appscope.set("the-key", value)
with eggs.app_context():
assert appscope.get("the-key") is value
with pytest.raises(KeyError, match="the-key"):
with rice.app_context():
appscope.get("the-key")
with eggs.app_context():
assert appscope.get("the-key") is value
|