Skip to content

Commit 87d2fbc

Browse files
committed
fix: add better tests
1 parent a52cb05 commit 87d2fbc

File tree

3 files changed

+45
-59
lines changed

3 files changed

+45
-59
lines changed

tests/test_cli.py

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import os
16+
import pathlib
1517
import sys
1618

1719
import pretend
1820
import pytest
1921

2022
from click.testing import CliRunner
23+
from starlette.applications import Starlette
2124

2225
import functions_framework
2326
import functions_framework._function_registry as _function_registry
@@ -26,6 +29,20 @@
2629
from functions_framework._cli import _cli
2730

2831

32+
@pytest.fixture
33+
def clean_registries():
34+
"""Clean up both REGISTRY_MAP and ASGI_FUNCTIONS registries."""
35+
original_registry_map = _function_registry.REGISTRY_MAP.copy()
36+
original_asgi = _function_registry.ASGI_FUNCTIONS.copy()
37+
_function_registry.REGISTRY_MAP.clear()
38+
_function_registry.ASGI_FUNCTIONS.clear()
39+
yield
40+
_function_registry.REGISTRY_MAP.clear()
41+
_function_registry.REGISTRY_MAP.update(original_registry_map)
42+
_function_registry.ASGI_FUNCTIONS.clear()
43+
_function_registry.ASGI_FUNCTIONS.update(original_asgi)
44+
45+
2946
def test_cli_no_arguments():
3047
runner = CliRunner()
3148
result = runner.invoke(_cli)
@@ -128,47 +145,17 @@ def test_asgi_cli(monkeypatch):
128145
assert asgi_server.run.calls == [pretend.call("0.0.0.0", 8080)]
129146

130147

131-
@pytest.fixture
132-
def clean_registry():
133-
"""Save and restore function registry state."""
134-
original_asgi = _function_registry.ASGI_FUNCTIONS.copy()
135-
_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):
142-
_function_registry.ASGI_FUNCTIONS.add("my_aio_func")
143-
144-
asgi_app = pretend.stub()
145-
create_asgi_app = pretend.call_recorder(lambda *a, **k: asgi_app)
146-
aio_module = pretend.stub(create_asgi_app=create_asgi_app)
147-
monkeypatch.setitem(sys.modules, "functions_framework.aio", aio_module)
148-
149-
asgi_server = pretend.stub(run=pretend.call_recorder(lambda host, port: None))
150-
create_server = pretend.call_recorder(lambda app, debug: asgi_server)
151-
monkeypatch.setattr(functions_framework._cli, "create_server", create_server)
152-
153-
runner = CliRunner()
154-
result = runner.invoke(_cli, ["--target", "my_aio_func"])
155-
156-
assert create_asgi_app.calls == [pretend.call("my_aio_func", None, "http")]
157-
assert asgi_server.run.calls == [pretend.call("0.0.0.0", 8080)]
158-
159-
160-
def test_no_auto_asgi_for_regular_functions(monkeypatch, clean_registry):
148+
def test_cli_auto_detects_asgi_decorator(clean_registries):
149+
"""Test that CLI auto-detects @aio decorated functions without --asgi flag."""
150+
# Use the actual async_decorator.py test file which has @aio.http decorated functions
151+
test_functions_dir = pathlib.Path(__file__).parent / "test_functions" / "decorators"
152+
source = test_functions_dir / "async_decorator.py"
161153

162-
app = pretend.stub()
163-
create_app = pretend.call_recorder(lambda *a, **k: app)
164-
monkeypatch.setattr(functions_framework._cli, "create_app", create_app)
165-
166-
flask_server = pretend.stub(run=pretend.call_recorder(lambda host, port: None))
167-
create_server = pretend.call_recorder(lambda app, debug: flask_server)
168-
monkeypatch.setattr(functions_framework._cli, "create_server", create_server)
154+
# Call create_app without any asgi flag - should auto-detect
155+
app = functions_framework.create_app(target="function_http", source=str(source))
169156

170-
runner = CliRunner()
171-
result = runner.invoke(_cli, ["--target", "regular_func"])
157+
# Verify it created a Starlette app (ASGI)
158+
assert isinstance(app, Starlette)
172159

173-
assert create_app.calls == [pretend.call("regular_func", None, "http")]
174-
assert flask_server.run.calls == [pretend.call("0.0.0.0", 8080)]
160+
# Verify the function was registered in ASGI_FUNCTIONS
161+
assert "function_http" in _function_registry.ASGI_FUNCTIONS

tests/test_decorator_functions.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@
3737

3838
TEST_FUNCTIONS_DIR = pathlib.Path(__file__).resolve().parent / "test_functions"
3939

40+
41+
@pytest.fixture
42+
def clean_registries():
43+
"""Clean up both REGISTRY_MAP and ASGI_FUNCTIONS registries."""
44+
original_registry_map = registry.REGISTRY_MAP.copy()
45+
original_asgi = registry.ASGI_FUNCTIONS.copy()
46+
registry.REGISTRY_MAP.clear()
47+
registry.ASGI_FUNCTIONS.clear()
48+
yield
49+
registry.REGISTRY_MAP.clear()
50+
registry.REGISTRY_MAP.update(original_registry_map)
51+
registry.ASGI_FUNCTIONS.clear()
52+
registry.ASGI_FUNCTIONS.update(original_asgi)
53+
54+
4055
# Python 3.5: ModuleNotFoundError does not exist
4156
try:
4257
_ModuleNotFoundError = ModuleNotFoundError
@@ -132,23 +147,7 @@ def test_aio_http_dict_response():
132147
assert resp.json() == {"message": "hello", "count": 42, "success": True}
133148

134149

135-
@pytest.fixture
136-
def clean_registry():
137-
"""Save and restore registry state."""
138-
original_registry_map = registry.REGISTRY_MAP.copy()
139-
original_asgi_functions = registry.ASGI_FUNCTIONS.copy()
140-
registry.REGISTRY_MAP.clear()
141-
registry.ASGI_FUNCTIONS.clear()
142-
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):
150+
def test_aio_decorators_register_asgi_functions(clean_registries):
152151
"""Test that @aio decorators add function names to ASGI_FUNCTIONS registry."""
153152
from functions_framework.aio import cloud_event, http
154153

tests/test_functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ def test_error_paths(http_trigger_client, path):
495495
def test_lazy_wsgi_app(monkeypatch, target, source, signature_type):
496496
actual_app_stub = pretend.stub()
497497
wsgi_app = pretend.call_recorder(lambda *a, **kw: actual_app_stub)
498-
create_app = pretend.call_recorder(lambda *a: wsgi_app)
498+
create_app = pretend.call_recorder(lambda *a, **kw: wsgi_app)
499499
monkeypatch.setattr(functions_framework, "create_app", create_app)
500500

501501
# Test that it's lazy

0 commit comments

Comments
 (0)