forked from GoogleCloudPlatform/functions-framework-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_decorator_functions.py
More file actions
178 lines (134 loc) · 5.47 KB
/
test_decorator_functions.py
File metadata and controls
178 lines (134 loc) · 5.47 KB
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
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pathlib
import sys
import pytest
from cloudevents import conversion as ce_conversion
from cloudevents.http import CloudEvent
import functions_framework._function_registry as registry
# Conditional import for Starlette
if sys.version_info >= (3, 8):
from starlette.testclient import TestClient as StarletteTestClient
else:
StarletteTestClient = None
from functions_framework import create_app
# Conditional import for async functionality
if sys.version_info >= (3, 8):
from functions_framework.aio import create_asgi_app
else:
create_asgi_app = None
TEST_FUNCTIONS_DIR = pathlib.Path(__file__).resolve().parent / "test_functions"
# Python 3.5: ModuleNotFoundError does not exist
try:
_ModuleNotFoundError = ModuleNotFoundError
except:
_ModuleNotFoundError = ImportError
@pytest.fixture(params=["decorator.py", "async_decorator.py"])
def cloud_event_decorator_client(request):
source = TEST_FUNCTIONS_DIR / "decorators" / request.param
target = "function_cloud_event"
if not request.param.startswith("async_"):
return create_app(target, source).test_client()
app = create_asgi_app(target, source)
return StarletteTestClient(app)
@pytest.fixture(params=["decorator.py", "async_decorator.py"])
def http_decorator_client(request):
source = TEST_FUNCTIONS_DIR / "decorators" / request.param
target = "function_http"
if not request.param.startswith("async_"):
return create_app(target, source).test_client()
app = create_asgi_app(target, source)
return StarletteTestClient(app)
@pytest.fixture
def cloud_event_1_0():
attributes = {
"specversion": "1.0",
"id": "my-id",
"source": "from-galaxy-far-far-away",
"type": "cloud_event.greet.you",
"time": "2020-08-16T13:58:54.471765",
}
data = {"name": "john"}
return CloudEvent(attributes, data)
def test_cloud_event_decorator(cloud_event_decorator_client, cloud_event_1_0):
headers, data = ce_conversion.to_structured(cloud_event_1_0)
resp = cloud_event_decorator_client.post("/", headers=headers, data=data)
assert resp.status_code == 200
assert resp.text == "OK"
def test_http_decorator(http_decorator_client):
resp = http_decorator_client.post("/my_path", json={"mode": "path"})
assert resp.status_code == 200
assert resp.text == "/my_path"
def test_aio_sync_cloud_event_decorator(cloud_event_1_0):
"""Test aio decorator with sync cloud event function."""
source = TEST_FUNCTIONS_DIR / "decorators" / "async_decorator.py"
target = "function_cloud_event_sync"
app = create_asgi_app(target, source)
client = StarletteTestClient(app)
headers, data = ce_conversion.to_structured(cloud_event_1_0)
resp = client.post("/", headers=headers, data=data)
assert resp.status_code == 200
assert resp.text == "OK"
def test_aio_sync_http_decorator():
source = TEST_FUNCTIONS_DIR / "decorators" / "async_decorator.py"
target = "function_http_sync"
app = create_asgi_app(target, source)
client = StarletteTestClient(app)
resp = client.post("/my_path?mode=path")
assert resp.status_code == 200
assert resp.text == "/my_path"
resp = client.post("/other_path")
assert resp.status_code == 200
assert resp.text == "sync response"
def test_aio_http_dict_response():
source = TEST_FUNCTIONS_DIR / "decorators" / "async_decorator.py"
target = "function_http_dict_response"
app = create_asgi_app(target, source)
client = StarletteTestClient(app)
resp = client.post("/")
assert resp.status_code == 200
assert resp.json() == {"message": "hello", "count": 42, "success": True}
@pytest.fixture
def clean_registry():
"""Save and restore registry state."""
original_registry_map = registry.REGISTRY_MAP.copy()
original_asgi_functions = registry.ASGI_FUNCTIONS.copy()
registry.REGISTRY_MAP.clear()
registry.ASGI_FUNCTIONS.clear()
yield
registry.REGISTRY_MAP.clear()
registry.REGISTRY_MAP.update(original_registry_map)
registry.ASGI_FUNCTIONS.clear()
registry.ASGI_FUNCTIONS.update(original_asgi_functions)
def test_aio_decorators_register_asgi_functions(clean_registry):
"""Test that @aio decorators add function names to ASGI_FUNCTIONS registry."""
from functions_framework.aio import cloud_event, http
@http
async def test_http_func(request):
return "test"
@cloud_event
async def test_cloud_event_func(event):
pass
assert "test_http_func" in registry.ASGI_FUNCTIONS
assert "test_cloud_event_func" in registry.ASGI_FUNCTIONS
assert registry.REGISTRY_MAP["test_http_func"] == "http"
assert registry.REGISTRY_MAP["test_cloud_event_func"] == "cloudevent"
@http
def test_http_sync(request):
return "sync"
@cloud_event
def test_cloud_event_sync(event):
pass
assert "test_http_sync" in registry.ASGI_FUNCTIONS
assert "test_cloud_event_sync" in registry.ASGI_FUNCTIONS