Merge branch 'dale-ensure-sync' of https://github.com/dalekube/flask into dale-ensure-sync
This commit is contained in:
commit
52f7787560
1 changed files with 8 additions and 6 deletions
|
|
@ -1972,18 +1972,21 @@ def test_app_freed_on_zero_refcount():
|
||||||
|
|
||||||
def test_sync_view_functions_cache(app, client):
|
def test_sync_view_functions_cache(app, client):
|
||||||
"""Test that the _sync_view_functions cache is populated and used."""
|
"""Test that the _sync_view_functions cache is populated and used."""
|
||||||
|
|
||||||
@app.route("/test")
|
@app.route("/test")
|
||||||
def test_view():
|
def test_view():
|
||||||
return "Hello"
|
return "Hello"
|
||||||
|
|
||||||
import unittest.mock
|
import unittest.mock
|
||||||
|
|
||||||
with unittest.mock.patch.object(app, 'ensure_sync', wraps=app.ensure_sync) as mock_ensure_sync:
|
with unittest.mock.patch.object(
|
||||||
|
app, "ensure_sync", wraps=app.ensure_sync
|
||||||
|
) as mock_ensure_sync:
|
||||||
# First request should call ensure_sync
|
# First request should call ensure_sync
|
||||||
response = client.get("/test")
|
response = client.get("/test")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert mock_ensure_sync.call_count == 1
|
assert mock_ensure_sync.call_count == 1
|
||||||
|
|
||||||
# Second request should hit the cache and not call ensure_sync
|
# Second request should hit the cache and not call ensure_sync
|
||||||
response = client.get("/test")
|
response = client.get("/test")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
@ -1992,13 +1995,12 @@ def test_sync_view_functions_cache(app, client):
|
||||||
# Direct mutation test (to verify the cache is bound to endpoint)
|
# Direct mutation test (to verify the cache is bound to endpoint)
|
||||||
def new_view():
|
def new_view():
|
||||||
return "World"
|
return "World"
|
||||||
|
|
||||||
# Simulating a user directly updating the view functions after setup
|
# Simulating a user directly updating the view functions after setup
|
||||||
# Because it's already cached, this mutation won't affect _sync_view_functions
|
# Because it's already cached, this mutation won't affect _sync_view_functions
|
||||||
app.view_functions["test"] = new_view
|
app.view_functions["test"] = new_view
|
||||||
|
|
||||||
# Ensure that it still returns the old result due to the cache
|
# Ensure that it still returns the old result due to the cache
|
||||||
response = client.get("/test")
|
response = client.get("/test")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert response.data == b"Hello"
|
assert response.data == b"Hello"
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue