-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconftest.py
More file actions
310 lines (240 loc) · 7.65 KB
/
conftest.py
File metadata and controls
310 lines (240 loc) · 7.65 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
"""
Pytest configuration and fixtures for DTK Backend tests.
"""
import os
import sys
import pytest
import tempfile
import shutil
from pathlib import Path
backend_dir = Path(__file__).parent
if str(backend_dir) not in sys.path:
sys.path.insert(0, str(backend_dir))
@pytest.fixture(scope="session")
def backend_root():
"""Return the backend root directory."""
return Path(__file__).parent
@pytest.fixture(scope="session")
def test_data_dir(backend_root):
"""Return the test data directory."""
test_dir = backend_root / "test_data"
test_dir.mkdir(exist_ok=True)
return test_dir
@pytest.fixture
def temp_project_dir(tmp_path):
"""
Create a temporary project directory for testing.
Returns a Path object to a temporary directory that will be
cleaned up after the test completes.
"""
project_dir = tmp_path / "test_project"
project_dir.mkdir(parents=True, exist_ok=True)
(project_dir / "images" / "main").mkdir(parents=True, exist_ok=True)
return project_dir
@pytest.fixture
def mock_camera_config():
"""Return a standard CameraConfig for testing."""
from capture import CameraConfig
return CameraConfig(
camera_index=0,
img_size=(1920, 1080),
timeout=50,
nopreview=True,
quality=85,
awb="auto",
vflip=False,
hflip=False,
)
@pytest.fixture
def dual_camera_configs():
"""Return a tuple of two CameraConfigs for dual capture testing."""
from capture import CameraConfig
cam1 = CameraConfig(
camera_index=0,
img_size=(1920, 1080),
timeout=50,
nopreview=True,
quality=85,
awb="auto",
vflip=False,
hflip=False,
)
cam2 = CameraConfig(
camera_index=1,
img_size=(1920, 1080),
timeout=50,
nopreview=True,
quality=85,
awb="auto",
vflip=False,
hflip=False,
)
return cam1, cam2
@pytest.fixture
def override_projects_root(tmp_path, monkeypatch):
"""
Override the PROJECTS_ROOT setting for testing.
This fixture creates a temporary projects directory and patches
the settings to use it, preventing tests from affecting real data.
"""
test_projects_dir = tmp_path / "test_projects"
test_projects_dir.mkdir(parents=True, exist_ok=True)
# Patch the environment variable
monkeypatch.setenv("PROJECTS_ROOT", str(test_projects_dir))
# Reload settings to pick up the new value
from app.core import config
import importlib
importlib.reload(config)
return test_projects_dir
@pytest.fixture
def skip_if_no_camera():
"""
Skip test if no cameras are detected.
Usage:
def test_camera_function(skip_if_no_camera):
# Test will be skipped if no cameras found
...
"""
from capture import is_camera_connected
if not is_camera_connected(0):
pytest.skip("No camera detected - skipping hardware test")
@pytest.fixture
def skip_if_single_camera():
"""
Skip test if fewer than 2 cameras are detected.
Usage:
def test_dual_camera_function(skip_if_single_camera):
# Test will be skipped if not enough cameras
...
"""
from capture import is_camera_connected
if not (is_camera_connected(0) and is_camera_connected(1)):
pytest.skip("Dual cameras not detected - skipping test")
@pytest.fixture(params=["subprocess", "picamera2"])
def camera_backend_type(request):
"""
Parametrized fixture that runs tests with both backends.
Usage:
def test_with_both_backends(camera_backend_type):
# This test will run twice, once for each backend
backend_name = camera_backend_type
...
"""
return request.param
@pytest.fixture
def set_camera_backend(monkeypatch, camera_backend_type):
"""
Set the camera backend for testing via environment variable.
Usage with camera_backend_type parametrization:
def test_capture(set_camera_backend, camera_backend_type):
# Backend is automatically set based on parameter
...
"""
monkeypatch.setenv("CAMERA_BACKEND", camera_backend_type)
# Reload settings
from app.core import config
import importlib
importlib.reload(config)
return camera_backend_type
# Pytest hooks
def pytest_configure(config):
"""Configure pytest with custom markers."""
config.addinivalue_line(
"markers", "camera: mark test as requiring camera hardware"
)
config.addinivalue_line(
"markers", "slow: mark test as slow running"
)
config.addinivalue_line(
"markers", "integration: mark test as integration test"
)
config.addinivalue_line(
"markers", "unit: mark test as unit test"
)
config.addinivalue_line(
"markers", "backend: mark test as camera backend specific"
)
def pytest_collection_modifyitems(config, items):
"""
Modify test collection to add markers automatically.
- Tests with "camera" in the name get @pytest.mark.camera
- Tests with "slow" in the name get @pytest.mark.slow
"""
for item in items:
# Auto-mark camera tests
if "camera" in item.nodeid.lower():
item.add_marker(pytest.mark.camera)
# Auto-mark backend tests
if "backend" in item.nodeid.lower():
item.add_marker(pytest.mark.backend)
# ==================== Database Fixtures ====================
@pytest.fixture
def db_session(override_projects_root):
"""
Create a test database session with fresh schema.
Each test gets its own in-memory SQLite database.
"""
import tempfile
from app.core.db import Base, engine, SessionLocal
from sqlalchemy import create_engine
# Use temporary file-based SQLite for testing
temp_db = tempfile.NamedTemporaryFile(suffix=".db", delete=False)
test_db_url = f"sqlite:///{temp_db.name}"
test_engine = create_engine(test_db_url, connect_args={"check_same_thread": False})
# Create tables
Base.metadata.create_all(test_engine)
# Create session
SessionLocal.configure(bind=test_engine)
session = SessionLocal()
yield session
session.close()
Path(temp_db.name).unlink(missing_ok=True)
@pytest.fixture
def client(db_session):
"""
Create a test client for API endpoints.
"""
from fastapi.testclient import TestClient
from app.main import app
from app.api.deps import get_db_dependency
def override_get_db():
try:
yield db_session
finally:
pass
app.dependency_overrides[get_db_dependency] = override_get_db
client = TestClient(app)
yield client
app.dependency_overrides.clear()
@pytest.fixture
def test_user(db_session):
"""
Create a test user in the database.
"""
from app.models.user import User
from app.core.security import hash_password
user = User(
username="testuser",
email="test@example.com",
hashed_password=hash_password("testpassword"),
is_active=True
)
db_session.add(user)
db_session.commit()
db_session.refresh(user)
return user
@pytest.fixture
def test_project(db_session, test_user):
"""
Create a test project in the database.
"""
from app.models.project import Project
project = Project(
name="test_project",
description="Test project for integration tests",
created_by=test_user.username
)
db_session.add(project)
db_session.commit()
db_session.refresh(project)
return project