Skip to content

Commit f40604e

Browse files
committed
fix: refactor tests
1 parent 473ed8f commit f40604e

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

tests/test_cli.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,17 @@ def test_asgi_cli(monkeypatch):
128128
assert asgi_server.run.calls == [pretend.call("0.0.0.0", 8080)]
129129

130130

131-
def test_auto_asgi_for_aio_decorated_functions(monkeypatch):
132-
original_asgi_functions = _function_registry.ASGI_FUNCTIONS.copy()
131+
@pytest.fixture
132+
def clean_registry():
133+
"""Save and restore function registry state."""
134+
original_asgi = _function_registry.ASGI_FUNCTIONS.copy()
133135
_function_registry.ASGI_FUNCTIONS.clear()
136+
yield
137+
_function_registry.ASGI_FUNCTIONS.clear()
138+
_function_registry.ASGI_FUNCTIONS.update(original_asgi)
139+
140+
141+
def test_auto_asgi_for_aio_decorated_functions(monkeypatch, clean_registry):
134142
_function_registry.ASGI_FUNCTIONS.add("my_aio_func")
135143

136144
asgi_app = pretend.stub()
@@ -148,13 +156,8 @@ def test_auto_asgi_for_aio_decorated_functions(monkeypatch):
148156
assert create_asgi_app.calls == [pretend.call("my_aio_func", None, "http")]
149157
assert asgi_server.run.calls == [pretend.call("0.0.0.0", 8080)]
150158

151-
_function_registry.ASGI_FUNCTIONS.clear()
152-
_function_registry.ASGI_FUNCTIONS.update(original_asgi_functions)
153-
154159

155-
def test_no_auto_asgi_for_regular_functions(monkeypatch):
156-
original_asgi_functions = _function_registry.ASGI_FUNCTIONS.copy()
157-
_function_registry.ASGI_FUNCTIONS.clear()
160+
def test_no_auto_asgi_for_regular_functions(monkeypatch, clean_registry):
158161

159162
app = pretend.stub()
160163
create_app = pretend.call_recorder(lambda *a, **k: app)
@@ -169,6 +172,3 @@ def test_no_auto_asgi_for_regular_functions(monkeypatch):
169172

170173
assert create_app.calls == [pretend.call("regular_func", None, "http")]
171174
assert flask_server.run.calls == [pretend.call("0.0.0.0", 8080)]
172-
173-
_function_registry.ASGI_FUNCTIONS.clear()
174-
_function_registry.ASGI_FUNCTIONS.update(original_asgi_functions)

tests/test_decorator_functions.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,24 @@ def test_aio_http_dict_response():
132132
assert resp.json() == {"message": "hello", "count": 42, "success": True}
133133

134134

135-
def test_aio_decorators_register_asgi_functions():
136-
"""Test that @aio decorators add function names to ASGI_FUNCTIONS registry."""
135+
@pytest.fixture
136+
def clean_registry():
137+
"""Save and restore registry state."""
137138
original_registry_map = registry.REGISTRY_MAP.copy()
138139
original_asgi_functions = registry.ASGI_FUNCTIONS.copy()
139140
registry.REGISTRY_MAP.clear()
140141
registry.ASGI_FUNCTIONS.clear()
141142

143+
yield
144+
145+
registry.REGISTRY_MAP.clear()
146+
registry.REGISTRY_MAP.update(original_registry_map)
147+
registry.ASGI_FUNCTIONS.clear()
148+
registry.ASGI_FUNCTIONS.update(original_asgi_functions)
149+
150+
151+
def test_aio_decorators_register_asgi_functions(clean_registry):
152+
"""Test that @aio decorators add function names to ASGI_FUNCTIONS registry."""
142153
from functions_framework.aio import cloud_event, http
143154

144155
@http
@@ -165,8 +176,3 @@ def test_cloud_event_sync(event):
165176

166177
assert "test_http_sync" in registry.ASGI_FUNCTIONS
167178
assert "test_cloud_event_sync" in registry.ASGI_FUNCTIONS
168-
169-
registry.REGISTRY_MAP.clear()
170-
registry.REGISTRY_MAP.update(original_registry_map)
171-
registry.ASGI_FUNCTIONS.clear()
172-
registry.ASGI_FUNCTIONS.update(original_asgi_functions)

0 commit comments

Comments
 (0)