|
20 | 20 | from click.testing import CliRunner |
21 | 21 |
|
22 | 22 | import functions_framework |
| 23 | +import functions_framework._function_registry as _function_registry |
| 24 | +import functions_framework.aio |
23 | 25 |
|
24 | 26 | from functions_framework._cli import _cli |
25 | 27 |
|
@@ -124,3 +126,49 @@ def test_asgi_cli(monkeypatch): |
124 | 126 | assert result.exit_code == 0 |
125 | 127 | assert create_asgi_app.calls == [pretend.call("foo", None, "http")] |
126 | 128 | assert asgi_server.run.calls == [pretend.call("0.0.0.0", 8080)] |
| 129 | + |
| 130 | + |
| 131 | +def test_auto_asgi_for_aio_decorated_functions(monkeypatch): |
| 132 | + original_asgi_functions = _function_registry.ASGI_FUNCTIONS.copy() |
| 133 | + _function_registry.ASGI_FUNCTIONS.clear() |
| 134 | + _function_registry.ASGI_FUNCTIONS.add("my_aio_func") |
| 135 | + |
| 136 | + asgi_app = pretend.stub() |
| 137 | + create_asgi_app = pretend.call_recorder(lambda *a, **k: asgi_app) |
| 138 | + aio_module = pretend.stub(create_asgi_app=create_asgi_app) |
| 139 | + monkeypatch.setitem(sys.modules, "functions_framework.aio", aio_module) |
| 140 | + |
| 141 | + asgi_server = pretend.stub(run=pretend.call_recorder(lambda host, port: None)) |
| 142 | + create_server = pretend.call_recorder(lambda app, debug: asgi_server) |
| 143 | + monkeypatch.setattr(functions_framework._cli, "create_server", create_server) |
| 144 | + |
| 145 | + runner = CliRunner() |
| 146 | + result = runner.invoke(_cli, ["--target", "my_aio_func"]) |
| 147 | + |
| 148 | + assert create_asgi_app.calls == [pretend.call("my_aio_func", None, "http")] |
| 149 | + assert asgi_server.run.calls == [pretend.call("0.0.0.0", 8080)] |
| 150 | + |
| 151 | + _function_registry.ASGI_FUNCTIONS.clear() |
| 152 | + _function_registry.ASGI_FUNCTIONS.update(original_asgi_functions) |
| 153 | + |
| 154 | + |
| 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() |
| 158 | + |
| 159 | + app = pretend.stub() |
| 160 | + create_app = pretend.call_recorder(lambda *a, **k: app) |
| 161 | + monkeypatch.setattr(functions_framework._cli, "create_app", create_app) |
| 162 | + |
| 163 | + flask_server = pretend.stub(run=pretend.call_recorder(lambda host, port: None)) |
| 164 | + create_server = pretend.call_recorder(lambda app, debug: flask_server) |
| 165 | + monkeypatch.setattr(functions_framework._cli, "create_server", create_server) |
| 166 | + |
| 167 | + runner = CliRunner() |
| 168 | + result = runner.invoke(_cli, ["--target", "regular_func"]) |
| 169 | + |
| 170 | + assert create_app.calls == [pretend.call("regular_func", None, "http")] |
| 171 | + 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) |
0 commit comments