1
- """Tests for example implementations.
1
+ """
2
+ Tests for example implementations.
2
3
3
4
This test module verifies that the example implementations work as expected,
4
5
covering both simple and full examples from the examples directory.
5
6
"""
6
- import os
7
+
7
8
from unittest .mock import patch
8
9
9
10
import pytest
10
- from fastapi import FastAPI
11
11
from fastapi .testclient import TestClient
12
12
from google .protobuf import duration_pb2
13
13
14
- from examples .simple .main import app as simple_app
15
14
from examples .full .tasks import app as full_app
16
- from fastapi_gcp_tasks . utils import emulator_client , queue_path
15
+ from examples . simple . main import app as simple_app
17
16
18
17
19
18
@pytest .fixture
@@ -29,8 +28,9 @@ def full_client():
29
28
30
29
31
30
def test_simple_example_local_mode (simple_client , monkeypatch ):
32
- """Test simple example in local mode.
33
-
31
+ """
32
+ Test simple example in local mode.
33
+
34
34
This test verifies that:
35
35
1. Emulator client is used in local mode
36
36
2. Tasks are properly queued
@@ -40,23 +40,21 @@ def test_simple_example_local_mode(simple_client, monkeypatch):
40
40
# Ensure we're in local mode
41
41
monkeypatch .setenv ("IS_LOCAL" , "true" )
42
42
monkeypatch .setenv ("TASK_LISTENER_BASE_URL" , "http://localhost:8000" )
43
-
43
+
44
44
# Test trigger endpoint
45
45
response = simple_client .get ("/trigger" )
46
46
assert response .status_code == 200
47
47
assert response .json () == {"message" : "Basic hello task triggered" }
48
48
49
49
# Test hello task endpoint
50
- response = simple_client .post (
51
- "/delayed/hello" ,
52
- json = {"message" : "test message" }
53
- )
50
+ response = simple_client .post ("/delayed/hello" , json = {"message" : "test message" })
54
51
assert response .status_code == 200
55
52
56
53
57
54
def test_full_example_chained_hooks (full_client , monkeypatch ):
58
- """Test full example with chained hooks.
59
-
55
+ """
56
+ Test full example with chained hooks.
57
+
60
58
This test verifies that:
61
59
1. OIDC and deadline hooks work together
62
60
2. Hook order is preserved
@@ -67,32 +65,25 @@ def test_full_example_chained_hooks(full_client, monkeypatch):
67
65
monkeypatch .setenv ("IS_LOCAL" , "true" )
68
66
monkeypatch .setenv ("CLOUD_TASKS_EMULATOR_URL" , "http://localhost:8123" )
69
67
monkeypatch .setenv ("TASK_LISTENER_BASE_URL" , "http://localhost:8000" )
70
-
68
+
71
69
# Test hello task with chained hooks
72
- response = full_client .post (
73
- "/delayed/hello" ,
74
- json = {"message" : "test with hooks" }
75
- )
70
+ response = full_client .post ("/delayed/hello" , json = {"message" : "test with hooks" })
76
71
assert response .status_code == 200
77
72
78
73
# Test fail_twice with retries
79
74
response = full_client .post ("/delayed/fail_twice" )
80
75
assert response .status_code == 500 # Should fail after 2 retries
81
76
82
77
# Test scheduled hello with hooks
83
- response = full_client .post (
84
- "/scheduled/timed_hello" ,
85
- json = {"message" : "test scheduled with hooks" }
86
- )
78
+ response = full_client .post ("/scheduled/timed_hello" , json = {"message" : "test scheduled with hooks" })
87
79
assert response .status_code == 200
88
- assert response .json () == {
89
- "message" : "Scheduled hello task ran with payload: test scheduled with hooks"
90
- }
80
+ assert response .json () == {"message" : "Scheduled hello task ran with payload: test scheduled with hooks" }
91
81
92
82
93
83
def test_full_example_hook_configuration ():
94
- """Test hook configuration in full example.
95
-
84
+ """
85
+ Test hook configuration in full example.
86
+
96
87
This test verifies that:
97
88
1. OIDC token is properly configured
98
89
2. Deadline duration is set correctly
@@ -101,20 +92,18 @@ def test_full_example_hook_configuration():
101
92
with patch ("fastapi_gcp_tasks.hooks.oidc_delayed_hook" ) as mock_oidc_hook :
102
93
with patch ("fastapi_gcp_tasks.hooks.deadline_delayed_hook" ) as mock_deadline_hook :
103
94
# Import here to trigger hook creation with mocks
104
- from examples .full .tasks import DelayedRoute
105
-
95
+
106
96
# Verify OIDC hook was called
107
97
mock_oidc_hook .assert_called_once ()
108
-
98
+
109
99
# Verify deadline hook was called with correct duration
110
- mock_deadline_hook .assert_called_once_with (
111
- duration = duration_pb2 .Duration (seconds = 1800 )
112
- )
100
+ mock_deadline_hook .assert_called_once_with (duration = duration_pb2 .Duration (seconds = 1800 ))
113
101
114
102
115
103
def test_simple_example_environment_handling (monkeypatch ):
116
- """Test environment handling in simple example.
117
-
104
+ """
105
+ Test environment handling in simple example.
106
+
118
107
This test verifies that:
119
108
1. Local mode uses emulator client
120
109
2. Environment variables are properly handled
@@ -123,21 +112,25 @@ def test_simple_example_environment_handling(monkeypatch):
123
112
# Test local mode
124
113
monkeypatch .setenv ("IS_LOCAL" , "true" )
125
114
from examples .simple .main import client
115
+
126
116
assert client is not None
127
-
117
+
128
118
# Test non-local mode
129
119
monkeypatch .setenv ("IS_LOCAL" , "false" )
130
120
with patch ("examples.simple.main.tasks_v2.CloudTasksClient" ) as mock_client :
131
121
# Reimport to trigger client creation
132
122
from importlib import reload
123
+
133
124
import examples .simple .main
125
+
134
126
reload (examples .simple .main )
135
127
mock_client .assert_called_once ()
136
128
137
129
138
130
def test_deployed_environment_scheduling (full_client , monkeypatch ):
139
- """Test scheduling in deployed environment.
140
-
131
+ """
132
+ Test scheduling in deployed environment.
133
+
141
134
This test verifies that:
142
135
1. Scheduling only occurs when not local
143
136
2. OIDC tokens are properly used
@@ -150,38 +143,35 @@ def test_deployed_environment_scheduling(full_client, monkeypatch):
150
143
monkeypatch .setenv ("IS_LOCAL" , "false" )
151
144
monkeypatch .setenv ("TASK_LISTENER_BASE_URL" , "https://example.com" )
152
145
monkeypatch .setenv ("SCHEDULED_OIDC_TOKEN" , "test-token" )
153
-
146
+
154
147
# Reload module to trigger scheduling
155
148
from importlib import reload
149
+
156
150
import examples .full .tasks
151
+
157
152
reload (examples .full .tasks )
158
-
153
+
159
154
# Verify scheduler client was used
160
155
mock_scheduler .assert_called_once ()
161
-
156
+
162
157
# Verify scheduled task creation
163
158
scheduler_instance = mock_scheduler .return_value
164
159
create_job = scheduler_instance .create_job
165
160
assert create_job .called
166
-
161
+
167
162
# Verify job configuration
168
163
job_args = create_job .call_args [0 ]
169
164
assert len (job_args ) == 2 # parent and job args
170
165
job = job_args [1 ]
171
-
166
+
172
167
# Verify schedule
173
168
assert job .schedule == "*/5 * * * *"
174
169
assert job .time_zone == "Asia/Kolkata"
175
-
170
+
176
171
# Verify OIDC token
177
172
assert job .http_target .oidc_token .service_account_email == "test-token"
178
-
173
+
179
174
# Test endpoint still works
180
- response = full_client .post (
181
- "/scheduled/timed_hello" ,
182
- json = {"message" : "test in deployed mode" }
183
- )
175
+ response = full_client .post ("/scheduled/timed_hello" , json = {"message" : "test in deployed mode" })
184
176
assert response .status_code == 200
185
- assert response .json () == {
186
- "message" : "Scheduled hello task ran with payload: test in deployed mode"
187
- }
177
+ assert response .json () == {"message" : "Scheduled hello task ran with payload: test in deployed mode" }
0 commit comments