|
1 | 1 | import io |
| 2 | +import re |
| 3 | +from unittest.mock import MagicMock |
| 4 | +from unittest.mock import patch |
2 | 5 | import zipfile |
3 | | -from unittest.mock import MagicMock, patch |
4 | 6 |
|
5 | | -import pytest |
6 | 7 | from click.testing import CliRunner |
7 | | - |
8 | 8 | from pyodk.errors import PyODKError |
| 9 | +import pytest |
9 | 10 |
|
10 | | -from src.download_results_forms import ( |
11 | | - export_center_candidate_results, |
12 | | - export_form_submissions, |
13 | | - main, |
14 | | -) |
| 11 | +from src.download_results_forms import create_upload_bundle |
| 12 | +from src.download_results_forms import export_center_candidate_results |
| 13 | +from src.download_results_forms import export_form_submissions |
| 14 | +from src.download_results_forms import main |
15 | 15 |
|
16 | 16 |
|
17 | 17 | def make_test_zip(center_id, include_media=True): |
@@ -220,6 +220,89 @@ def test_skips_existing_valid_zip(self, output_dir): |
220 | 220 | assert len(df) == 2 |
221 | 221 |
|
222 | 222 |
|
| 223 | +class TestCreateUploadBundle: |
| 224 | + def test_bundle_contains_csv_and_media(self, tmp_path): |
| 225 | + output_dir = tmp_path / "output" |
| 226 | + output_dir.mkdir() |
| 227 | + csv_path = output_dir / "candidate_results.csv" |
| 228 | + csv_path.write_text("a,b\n1,2\n") |
| 229 | + media_dir = output_dir / "media" |
| 230 | + media_dir.mkdir() |
| 231 | + (media_dir / "100_sig.jpg").write_bytes(b"img1") |
| 232 | + (media_dir / "100_page.jpg").write_bytes(b"img2") |
| 233 | + |
| 234 | + bundle_path = create_upload_bundle( |
| 235 | + output_dir=output_dir, |
| 236 | + csv_path=csv_path, |
| 237 | + media_dir=media_dir, |
| 238 | + project_id=14, |
| 239 | + ) |
| 240 | + |
| 241 | + assert bundle_path.exists() |
| 242 | + assert bundle_path.parent == output_dir |
| 243 | + with zipfile.ZipFile(bundle_path) as zf: |
| 244 | + names = set(zf.namelist()) |
| 245 | + assert "candidate_results.csv" in names |
| 246 | + assert "media/100_sig.jpg" in names |
| 247 | + assert "media/100_page.jpg" in names |
| 248 | + # ZIP_STORED = no compression |
| 249 | + for info in zf.infolist(): |
| 250 | + assert info.compress_type == zipfile.ZIP_STORED |
| 251 | + |
| 252 | + def test_bundle_filename_has_project_id_and_timestamp(self, tmp_path): |
| 253 | + output_dir = tmp_path / "output" |
| 254 | + output_dir.mkdir() |
| 255 | + csv_path = output_dir / "candidate_results.csv" |
| 256 | + csv_path.write_text("a,b\n1,2\n") |
| 257 | + |
| 258 | + bundle_path = create_upload_bundle( |
| 259 | + output_dir=output_dir, |
| 260 | + csv_path=csv_path, |
| 261 | + media_dir=output_dir / "media", |
| 262 | + project_id=42, |
| 263 | + ) |
| 264 | + |
| 265 | + assert re.fullmatch( |
| 266 | + r"results_export_p42_\d{8}_\d{6}\.zip", bundle_path.name |
| 267 | + ) |
| 268 | + |
| 269 | + def test_bundle_handles_missing_media_dir(self, tmp_path): |
| 270 | + output_dir = tmp_path / "output" |
| 271 | + output_dir.mkdir() |
| 272 | + csv_path = output_dir / "candidate_results.csv" |
| 273 | + csv_path.write_text("a,b\n1,2\n") |
| 274 | + |
| 275 | + bundle_path = create_upload_bundle( |
| 276 | + output_dir=output_dir, |
| 277 | + csv_path=csv_path, |
| 278 | + media_dir=output_dir / "media", |
| 279 | + project_id=1, |
| 280 | + ) |
| 281 | + |
| 282 | + assert bundle_path.exists() |
| 283 | + with zipfile.ZipFile(bundle_path) as zf: |
| 284 | + names = set(zf.namelist()) |
| 285 | + assert names == {"candidate_results.csv"} |
| 286 | + |
| 287 | + def test_bundle_handles_empty_media_dir(self, tmp_path): |
| 288 | + output_dir = tmp_path / "output" |
| 289 | + output_dir.mkdir() |
| 290 | + csv_path = output_dir / "candidate_results.csv" |
| 291 | + csv_path.write_text("a,b\n1,2\n") |
| 292 | + media_dir = output_dir / "media" |
| 293 | + media_dir.mkdir() |
| 294 | + |
| 295 | + bundle_path = create_upload_bundle( |
| 296 | + output_dir=output_dir, |
| 297 | + csv_path=csv_path, |
| 298 | + media_dir=media_dir, |
| 299 | + project_id=1, |
| 300 | + ) |
| 301 | + |
| 302 | + with zipfile.ZipFile(bundle_path) as zf: |
| 303 | + assert set(zf.namelist()) == {"candidate_results.csv"} |
| 304 | + |
| 305 | + |
223 | 306 | class TestCLI: |
224 | 307 | def test_with_center_ids(self, output_dir): |
225 | 308 | zip_bytes = make_test_zip(100) |
@@ -272,6 +355,59 @@ def test_auto_discovers_centers(self, output_dir): |
272 | 355 | mock_client.forms.list.assert_called_once_with(project_id=1) |
273 | 356 | assert (output_dir / "candidate_results.csv").exists() |
274 | 357 |
|
| 358 | + def test_creates_bundle_by_default(self, output_dir): |
| 359 | + zip_bytes = make_test_zip(100) |
| 360 | + |
| 361 | + mock_response = MagicMock() |
| 362 | + mock_response.content = zip_bytes |
| 363 | + mock_response.raise_for_status = MagicMock() |
| 364 | + |
| 365 | + mock_client = MagicMock() |
| 366 | + mock_client.get.return_value = mock_response |
| 367 | + mock_client.__enter__ = MagicMock(return_value=mock_client) |
| 368 | + mock_client.__exit__ = MagicMock(return_value=False) |
| 369 | + |
| 370 | + runner = CliRunner() |
| 371 | + with patch("src.download_results_forms.Client", return_value=mock_client): |
| 372 | + result = runner.invoke( |
| 373 | + main, |
| 374 | + ["--project-id=14", "--output-dir", str(output_dir), "100"], |
| 375 | + ) |
| 376 | + |
| 377 | + assert result.exit_code == 0 |
| 378 | + bundles = list(output_dir.glob("results_export_p14_*.zip")) |
| 379 | + assert len(bundles) == 1 |
| 380 | + |
| 381 | + def test_bundle_false_skips_bundle(self, output_dir): |
| 382 | + zip_bytes = make_test_zip(100) |
| 383 | + |
| 384 | + mock_response = MagicMock() |
| 385 | + mock_response.content = zip_bytes |
| 386 | + mock_response.raise_for_status = MagicMock() |
| 387 | + |
| 388 | + mock_client = MagicMock() |
| 389 | + mock_client.get.return_value = mock_response |
| 390 | + mock_client.__enter__ = MagicMock(return_value=mock_client) |
| 391 | + mock_client.__exit__ = MagicMock(return_value=False) |
| 392 | + |
| 393 | + runner = CliRunner() |
| 394 | + with patch("src.download_results_forms.Client", return_value=mock_client): |
| 395 | + result = runner.invoke( |
| 396 | + main, |
| 397 | + [ |
| 398 | + "--project-id=14", |
| 399 | + "--output-dir", |
| 400 | + str(output_dir), |
| 401 | + "--bundle=false", |
| 402 | + "100", |
| 403 | + ], |
| 404 | + ) |
| 405 | + |
| 406 | + assert result.exit_code == 0 |
| 407 | + assert (output_dir / "candidate_results.csv").exists() |
| 408 | + bundles = list(output_dir.glob("results_export_p*.zip")) |
| 409 | + assert len(bundles) == 0 |
| 410 | + |
275 | 411 | def test_pyodk_error_exits_gracefully(self, output_dir): |
276 | 412 | mock_client = MagicMock() |
277 | 413 | mock_client.__enter__ = MagicMock(return_value=mock_client) |
|
0 commit comments