Skip to content

Commit 04de52b

Browse files
committed
style(tests): apply consistent formatting and spacing across test files
1 parent 1aebb10 commit 04de52b

8 files changed

+246
-166
lines changed

tests/conftest.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Comprehensive test configuration with improved fixtures and utilities.
33
"""
4+
45
import pytest
56
import tempfile
67
import os
@@ -14,15 +15,17 @@
1415
def app():
1516
"""Create application for the tests with session scope."""
1617
app = create_app()
17-
app.config.update({
18-
"TESTING": True,
19-
"SQLALCHEMY_DATABASE_URI": "sqlite:///:memory:",
20-
"WTF_CSRF_ENABLED": False,
21-
"SECRET_KEY": "test-secret-key",
22-
"SERVER_NAME": "localhost.localdomain",
23-
"APPLICATION_ROOT": "/",
24-
"PREFERRED_URL_SCHEME": "http"
25-
})
18+
app.config.update(
19+
{
20+
"TESTING": True,
21+
"SQLALCHEMY_DATABASE_URI": "sqlite:///:memory:",
22+
"WTF_CSRF_ENABLED": False,
23+
"SECRET_KEY": "test-secret-key",
24+
"SERVER_NAME": "localhost.localdomain",
25+
"APPLICATION_ROOT": "/",
26+
"PREFERRED_URL_SCHEME": "http",
27+
}
28+
)
2629

2730
return app
2831

@@ -116,32 +119,37 @@ def sample_messages(app):
116119
def db_helper():
117120
"""Database helper for tests."""
118121
from tests.test_utils import DatabaseHelper
122+
119123
return DatabaseHelper()
120124

121125

122126
@pytest.fixture
123127
def response_helper():
124128
"""Response helper for tests."""
125129
from tests.test_utils import ResponseHelper
130+
126131
return ResponseHelper()
127132

128133

129134
@pytest.fixture
130135
def route_helper():
131136
"""Route helper for tests."""
132137
from tests.test_utils import RouteHelper
138+
133139
return RouteHelper()
134140

135141

136142
@pytest.fixture
137143
def validation_helper():
138144
"""Validation helper for tests."""
139145
from tests.test_utils import ValidationHelper
146+
140147
return ValidationHelper()
141148

142149

143150
@pytest.fixture
144151
def benchmark_helper():
145152
"""Benchmark helper for tests."""
146153
from tests.test_utils import BenchmarkHelper
154+
147155
return BenchmarkHelper()

tests/test_callbacks_functional.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Comprehensive tests for MVC Flask callback middleware system.
33
"""
4+
45
import pytest
56
from flask import url_for, request, Response
67

@@ -10,6 +11,7 @@
1011

1112
# Callback Middleware Tests
1213

14+
1315
def test_before_request_execution(client):
1416
"""Test that before_request callback is executed."""
1517
response = client.get(url_for("callbacks.index"))
@@ -80,10 +82,12 @@ def test_hook_execution_with_matching_endpoint(app):
8082
with app.test_request_context("/callbacks"):
8183
# Mock the request to simulate endpoint matching
8284
from unittest.mock import patch
83-
with patch('flask.request') as mock_request:
85+
86+
with patch("flask.request") as mock_request:
8487
mock_request.endpoint = "callbacks.index"
8588

8689
executed = []
90+
8791
def mock_hook():
8892
executed.append("called")
8993

@@ -99,10 +103,12 @@ def test_hook_execution_without_matching_endpoint(app):
99103
with app.test_request_context("/other"):
100104
# Mock the request to simulate endpoint not matching
101105
from unittest.mock import patch
102-
with patch('flask.request') as mock_request:
106+
107+
with patch("flask.request") as mock_request:
103108
mock_request.endpoint = "other.index"
104109

105110
executed = []
111+
106112
def mock_hook():
107113
executed.append("called")
108114

@@ -112,22 +118,24 @@ def mock_hook():
112118

113119
# Callback Configuration Tests
114120

121+
115122
def test_multiple_actions_before_request(app):
116123
"""Test before_request callback with multiple actions."""
124+
117125
class MultiActionController:
118126
before_request = dict(callback="setup", actions="index show edit")
119127

120128
def setup(self):
121129
self.data = "initialized"
122130

123131
def index(self):
124-
return getattr(self, 'data', 'not initialized')
132+
return getattr(self, "data", "not initialized")
125133

126134
def show(self, id):
127-
return getattr(self, 'data', 'not initialized')
135+
return getattr(self, "data", "not initialized")
128136

129137
def edit(self, id):
130-
return getattr(self, 'data', 'not initialized')
138+
return getattr(self, "data", "not initialized")
131139

132140
controller = MultiActionController()
133141
middleware = CallbackMiddleware(app, "multi", controller)
@@ -138,6 +146,7 @@ def edit(self, id):
138146

139147
def test_after_request_with_response_modification(app):
140148
"""Test after_request callback modifying response."""
149+
141150
class ResponseModifierController:
142151
after_request = dict(callback="modify_response", actions="index")
143152

@@ -154,7 +163,8 @@ def modify_response(self, response):
154163
with app.test_request_context("/modifier"):
155164
# Mock the request to simulate endpoint matching
156165
from unittest.mock import patch
157-
with patch('flask.request') as mock_request:
166+
167+
with patch("flask.request") as mock_request:
158168
mock_request.endpoint = "modifier.index"
159169

160170
response = Response("test")
@@ -167,8 +177,10 @@ def modify_response(self, response):
167177

168178
# Edge Cases Tests
169179

180+
170181
def test_callback_without_actions(app):
171182
"""Test callback configuration without actions."""
183+
172184
class NoActionsController:
173185
before_request = dict(callback="setup", actions="")
174186

@@ -184,6 +196,7 @@ def setup(self):
184196

185197
def test_callback_with_invalid_method(app):
186198
"""Test callback configuration with invalid method."""
199+
187200
class InvalidMethodController:
188201
before_request = dict(callback="nonexistent_method", actions="index")
189202

@@ -202,6 +215,7 @@ def setup(self):
202215

203216
def test_empty_controller(app):
204217
"""Test middleware with controller that has no callbacks."""
218+
205219
class EmptyController:
206220
def index(self):
207221
return "empty"
@@ -216,6 +230,7 @@ def index(self):
216230

217231
def test_malformed_callback_config(app):
218232
"""Test malformed callback configuration."""
233+
219234
class MalformedController:
220235
before_request = "not a dict"
221236

@@ -228,6 +243,7 @@ class MalformedController:
228243

229244
# Integration Tests
230245

246+
231247
def test_full_request_cycle_with_callbacks(client):
232248
"""Test complete request cycle with both before and after callbacks."""
233249
response = client.get(url_for("callbacks.index"))

tests/test_controllers_functional.py

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Comprehensive tests for MVC Flask controllers functionality.
33
"""
4+
45
import pytest
56
import json
67
from flask import url_for
@@ -11,6 +12,7 @@
1112

1213
# CRUD Operations Tests
1314

15+
1416
def test_index_with_messages(client, sample_messages):
1517
"""Test index action displays messages correctly."""
1618
response = client.get(url_for("messages.index"))
@@ -55,9 +57,7 @@ def test_create_with_json(empty_client):
5557
headers = {"Content-Type": "application/json"}
5658

5759
response = empty_client.post(
58-
url_for("messages.create"),
59-
data=json.dumps(data),
60-
headers=headers
60+
url_for("messages.create"), data=json.dumps(data), headers=headers
6161
)
6262

6363
assert response.status_code == 201
@@ -73,9 +73,7 @@ def test_create_without_data(empty_client):
7373

7474
with pytest.raises(KeyError):
7575
empty_client.post(
76-
url_for("messages.create"),
77-
data=json.dumps({}),
78-
headers=headers
76+
url_for("messages.create"), data=json.dumps({}), headers=headers
7977
)
8078

8179

@@ -96,9 +94,7 @@ def test_update_with_json(client):
9694
headers = {"Content-Type": "application/json"}
9795

9896
response = client.put(
99-
url_for("messages.update", id=message.id),
100-
data=json.dumps(data),
101-
headers=headers
97+
url_for("messages.update", id=message.id), data=json.dumps(data), headers=headers
10298
)
10399

104100
assert response.status_code == 200
@@ -116,7 +112,7 @@ def test_update_with_form_data(client):
116112
response = client.put(
117113
url_for("messages.update", id=message.id),
118114
data={"title": "Form Updated Title"},
119-
headers={"Content-Type": "application/x-www-form-urlencoded"}
115+
headers={"Content-Type": "application/x-www-form-urlencoded"},
120116
)
121117

122118
assert response.status_code == 200
@@ -132,9 +128,7 @@ def test_update_nonexistent_message(client):
132128

133129
try:
134130
response = client.put(
135-
url_for("messages.update", id=99999),
136-
data=json.dumps(data),
137-
headers=headers
131+
url_for("messages.update", id=99999), data=json.dumps(data), headers=headers
138132
)
139133
assert response.status_code in [200, 404, 500]
140134
except Exception:
@@ -166,6 +160,7 @@ def test_delete_nonexistent_message(empty_client):
166160

167161
# Controller Helper Tests
168162

163+
169164
def test_multiple_messages_handling(empty_client):
170165
"""Test controller behavior with multiple messages."""
171166
messages = []
@@ -185,14 +180,13 @@ def test_multiple_messages_handling(empty_client):
185180

186181
# Error Handling Tests
187182

183+
188184
def test_malformed_json_in_create(empty_client):
189185
"""Test create action with malformed JSON."""
190186
headers = {"Content-Type": "application/json"}
191187

192188
response = empty_client.post(
193-
url_for("messages.create"),
194-
data="invalid json",
195-
headers=headers
189+
url_for("messages.create"), data="invalid json", headers=headers
196190
)
197191

198192
assert response.status_code in [400, 500]
@@ -205,7 +199,7 @@ def test_missing_content_type_in_update(client):
205199
try:
206200
response = client.put(
207201
url_for("messages.update", id=message.id),
208-
data=json.dumps({"title": "Updated"})
202+
data=json.dumps({"title": "Updated"}),
209203
)
210204
assert response.status_code in [200, 400, 500]
211205
except KeyError:
@@ -218,22 +212,21 @@ def test_empty_title_in_create(empty_client):
218212
headers = {"Content-Type": "application/json"}
219213

220214
response = empty_client.post(
221-
url_for("messages.create"),
222-
data=json.dumps(data),
223-
headers=headers
215+
url_for("messages.create"), data=json.dumps(data), headers=headers
224216
)
225217

226218
assert response.status_code == 201
227219

228220

229221
# Integration Tests
230222

223+
231224
def test_full_crud_workflow(empty_client):
232225
"""Test complete CRUD workflow."""
233226
create_response = empty_client.post(
234227
url_for("messages.create"),
235228
data=json.dumps({"title": "Workflow Test Message"}),
236-
headers={"Content-Type": "application/json"}
229+
headers={"Content-Type": "application/json"},
237230
)
238231
assert create_response.status_code == 201
239232

@@ -249,7 +242,7 @@ def test_full_crud_workflow(empty_client):
249242
update_response = empty_client.put(
250243
url_for("messages.update", id=message.id),
251244
data=json.dumps({"title": "Updated Workflow Message"}),
252-
headers={"Content-Type": "application/json"}
245+
headers={"Content-Type": "application/json"},
253246
)
254247
assert update_response.status_code == 200
255248

@@ -262,17 +255,14 @@ def test_full_crud_workflow(empty_client):
262255

263256
def test_concurrent_operations(empty_client):
264257
"""Test concurrent operations on messages."""
265-
messages_data = [
266-
{"title": f"Concurrent Message {i}"}
267-
for i in range(5)
268-
]
258+
messages_data = [{"title": f"Concurrent Message {i}"} for i in range(5)]
269259

270260
created_messages = []
271261
for data in messages_data:
272262
response = empty_client.post(
273263
url_for("messages.create"),
274264
data=json.dumps(data),
275-
headers={"Content-Type": "application/json"}
265+
headers={"Content-Type": "application/json"},
276266
)
277267
assert response.status_code == 201
278268
created_messages.append(response.json)

0 commit comments

Comments
 (0)