Skip to content

Commit adc33a6

Browse files
style: fix docstring and linting issues
Co-Authored-By: Rushil Srivastava <[email protected]>
1 parent 5aa1fd7 commit adc33a6

File tree

3 files changed

+65
-72
lines changed

3 files changed

+65
-72
lines changed

tests/test_delayed_route.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
"""Tests for DelayedRouteBuilder functionality.
1+
"""
2+
Tests for DelayedRouteBuilder functionality.
23
34
This module verifies the core functionality of DelayedRouteBuilder, including:
45
- Basic task creation and execution
@@ -9,7 +10,7 @@
910
"""
1011

1112
import pytest
12-
from fastapi import APIRouter, Depends, HTTPException
13+
from fastapi import APIRouter, Depends
1314
from google.protobuf import duration_pb2
1415
from pydantic import BaseModel
1516

@@ -87,8 +88,9 @@ async def test_task(payload: TestPayload):
8788

8889

8990
def test_delayed_task_with_task_id(app, delayed_route, test_client):
90-
"""Test task creation with task ID.
91-
91+
"""
92+
Test task creation with task ID.
93+
9294
This test verifies that:
9395
1. Tasks can be created with unique IDs
9496
2. Duplicate task IDs are handled correctly
@@ -103,24 +105,21 @@ async def test_task(payload: TestPayload):
103105
app.include_router(router)
104106

105107
# Test with unique task ID
106-
task1 = test_task.options(task_id="unique-task-1").delay(
107-
payload=TestPayload(message="test1")
108-
)
108+
task1 = test_task.options(task_id="unique-task-1").delay(payload=TestPayload(message="test1"))
109109
assert task1 is not None
110110

111111
# Test with duplicate task ID (should be idempotent)
112-
task2 = test_task.options(task_id="unique-task-1").delay(
113-
payload=TestPayload(message="test1")
114-
)
112+
task2 = test_task.options(task_id="unique-task-1").delay(payload=TestPayload(message="test1"))
115113
assert task2 is not None
116114

117115
response = test_client.post("/test-task-id", json={"message": "test"})
118116
assert response.status_code == 200
119117

120118

121119
def test_delayed_task_error_handling(app, test_client):
122-
"""Test error handling in delayed routes.
123-
120+
"""
121+
Test error handling in delayed routes.
122+
124123
This test verifies that:
125124
1. Invalid configurations are caught
126125
2. Task creation failures are handled
@@ -158,8 +157,9 @@ def test_delayed_task_error_handling(app, test_client):
158157

159158

160159
def test_delayed_task_queue_creation(app, test_client):
161-
"""Test queue auto-creation functionality.
162-
160+
"""
161+
Test queue auto-creation functionality.
162+
163163
This test verifies that:
164164
1. Queue is created if it doesn't exist
165165
2. DelayedRouteBuilder handles existing queues

tests/test_examples.py

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
"""Tests for example implementations.
1+
"""
2+
Tests for example implementations.
23
34
This test module verifies that the example implementations work as expected,
45
covering both simple and full examples from the examples directory.
56
"""
6-
import os
7+
78
from unittest.mock import patch
89

910
import pytest
10-
from fastapi import FastAPI
1111
from fastapi.testclient import TestClient
1212
from google.protobuf import duration_pb2
1313

14-
from examples.simple.main import app as simple_app
1514
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
1716

1817

1918
@pytest.fixture
@@ -29,8 +28,9 @@ def full_client():
2928

3029

3130
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+
3434
This test verifies that:
3535
1. Emulator client is used in local mode
3636
2. Tasks are properly queued
@@ -40,23 +40,21 @@ def test_simple_example_local_mode(simple_client, monkeypatch):
4040
# Ensure we're in local mode
4141
monkeypatch.setenv("IS_LOCAL", "true")
4242
monkeypatch.setenv("TASK_LISTENER_BASE_URL", "http://localhost:8000")
43-
43+
4444
# Test trigger endpoint
4545
response = simple_client.get("/trigger")
4646
assert response.status_code == 200
4747
assert response.json() == {"message": "Basic hello task triggered"}
4848

4949
# 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"})
5451
assert response.status_code == 200
5552

5653

5754
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+
6058
This test verifies that:
6159
1. OIDC and deadline hooks work together
6260
2. Hook order is preserved
@@ -67,32 +65,25 @@ def test_full_example_chained_hooks(full_client, monkeypatch):
6765
monkeypatch.setenv("IS_LOCAL", "true")
6866
monkeypatch.setenv("CLOUD_TASKS_EMULATOR_URL", "http://localhost:8123")
6967
monkeypatch.setenv("TASK_LISTENER_BASE_URL", "http://localhost:8000")
70-
68+
7169
# 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"})
7671
assert response.status_code == 200
7772

7873
# Test fail_twice with retries
7974
response = full_client.post("/delayed/fail_twice")
8075
assert response.status_code == 500 # Should fail after 2 retries
8176

8277
# 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"})
8779
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"}
9181

9282

9383
def test_full_example_hook_configuration():
94-
"""Test hook configuration in full example.
95-
84+
"""
85+
Test hook configuration in full example.
86+
9687
This test verifies that:
9788
1. OIDC token is properly configured
9889
2. Deadline duration is set correctly
@@ -101,20 +92,18 @@ def test_full_example_hook_configuration():
10192
with patch("fastapi_gcp_tasks.hooks.oidc_delayed_hook") as mock_oidc_hook:
10293
with patch("fastapi_gcp_tasks.hooks.deadline_delayed_hook") as mock_deadline_hook:
10394
# Import here to trigger hook creation with mocks
104-
from examples.full.tasks import DelayedRoute
105-
95+
10696
# Verify OIDC hook was called
10797
mock_oidc_hook.assert_called_once()
108-
98+
10999
# 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))
113101

114102

115103
def test_simple_example_environment_handling(monkeypatch):
116-
"""Test environment handling in simple example.
117-
104+
"""
105+
Test environment handling in simple example.
106+
118107
This test verifies that:
119108
1. Local mode uses emulator client
120109
2. Environment variables are properly handled
@@ -123,21 +112,25 @@ def test_simple_example_environment_handling(monkeypatch):
123112
# Test local mode
124113
monkeypatch.setenv("IS_LOCAL", "true")
125114
from examples.simple.main import client
115+
126116
assert client is not None
127-
117+
128118
# Test non-local mode
129119
monkeypatch.setenv("IS_LOCAL", "false")
130120
with patch("examples.simple.main.tasks_v2.CloudTasksClient") as mock_client:
131121
# Reimport to trigger client creation
132122
from importlib import reload
123+
133124
import examples.simple.main
125+
134126
reload(examples.simple.main)
135127
mock_client.assert_called_once()
136128

137129

138130
def test_deployed_environment_scheduling(full_client, monkeypatch):
139-
"""Test scheduling in deployed environment.
140-
131+
"""
132+
Test scheduling in deployed environment.
133+
141134
This test verifies that:
142135
1. Scheduling only occurs when not local
143136
2. OIDC tokens are properly used
@@ -150,38 +143,35 @@ def test_deployed_environment_scheduling(full_client, monkeypatch):
150143
monkeypatch.setenv("IS_LOCAL", "false")
151144
monkeypatch.setenv("TASK_LISTENER_BASE_URL", "https://example.com")
152145
monkeypatch.setenv("SCHEDULED_OIDC_TOKEN", "test-token")
153-
146+
154147
# Reload module to trigger scheduling
155148
from importlib import reload
149+
156150
import examples.full.tasks
151+
157152
reload(examples.full.tasks)
158-
153+
159154
# Verify scheduler client was used
160155
mock_scheduler.assert_called_once()
161-
156+
162157
# Verify scheduled task creation
163158
scheduler_instance = mock_scheduler.return_value
164159
create_job = scheduler_instance.create_job
165160
assert create_job.called
166-
161+
167162
# Verify job configuration
168163
job_args = create_job.call_args[0]
169164
assert len(job_args) == 2 # parent and job args
170165
job = job_args[1]
171-
166+
172167
# Verify schedule
173168
assert job.schedule == "*/5 * * * *"
174169
assert job.time_zone == "Asia/Kolkata"
175-
170+
176171
# Verify OIDC token
177172
assert job.http_target.oidc_token.service_account_email == "test-token"
178-
173+
179174
# 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"})
184176
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"}

tests/test_scheduled_route.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
"""Tests for ScheduledRouteBuilder functionality.
1+
"""
2+
Tests for ScheduledRouteBuilder functionality.
23
34
This module verifies the core functionality of ScheduledRouteBuilder, including:
45
- Basic scheduled task creation
@@ -64,8 +65,9 @@ async def test_task(payload: TestPayload):
6465

6566

6667
def test_scheduled_route_cron_validation(app, test_client):
67-
"""Test cron schedule validation.
68-
68+
"""
69+
Test cron schedule validation.
70+
6971
This test verifies that:
7072
1. Valid cron expressions are accepted
7173
2. Invalid expressions are rejected
@@ -126,8 +128,9 @@ async def test_task(payload: TestPayload):
126128

127129

128130
def test_scheduled_route_job_creation(app, test_client):
129-
"""Test Cloud Scheduler job creation.
130-
131+
"""
132+
Test Cloud Scheduler job creation.
133+
131134
This test verifies that:
132135
1. Jobs are created with correct settings
133136
2. Job names are unique

0 commit comments

Comments
 (0)