diff options
| author | pi coding agent <pi@localhost> | 2026-10-22 20:16:40 +0000 |
|---|---|---|
| committer | Frederick Muriuki Muriithi <fredmanglis@gmail.com> | 2026-10-22 15:42:58 -0500 |
| commit | f33e8d64b735141f31c4cdc501f4998013d61e30 (patch) | |
| tree | 3a7a180a26d557629d18cefdeeeda43fe63291a5 | |
| parent | d3b7c7e43380fa86bbe1f539f6e25747aae0c999 (diff) | |
| download | gn-uploader-f33e8d64b735141f31c4cdc501f4998013d61e30.tar.gz | |
Test: cover resolving uploaded files generically
Add unit tests for a generic `resolve_uploaded_file` in the uploader.files package: direct multipart and resumable uploads (returning both the path and the original name), hashed vs unhashed saving, and the error cases (missing file, malformed resumable data, wrong field).
| -rw-r--r-- | tests/uploader/test_files.py | 137 |
1 files changed, 136 insertions, 1 deletions
diff --git a/tests/uploader/test_files.py b/tests/uploader/test_files.py index cb22fff..8b9d675 100644 --- a/tests/uploader/test_files.py +++ b/tests/uploader/test_files.py @@ -1,9 +1,33 @@ """Tests functions in the `uploader.files` module.""" +import io +import json from pathlib import Path import pytest +from werkzeug.datastructures import FileStorage + +from uploader.files import resolve_uploaded_file, sha256_digest_over_file + +UPLOAD_FIELD = "genotypes-records-file" +CONTENTS = b"Locus,Chr\nm1,1\n" + + +def _right_value(result): + """Return the Right value, or None if the result is a Left.""" + return result.either(lambda _err: None, lambda val: val) + + +def _left_value(result): + """Return the Left value, or None if the result is a Right.""" + return result.either(lambda err: err, lambda _val: None) + + +def _file_storage(contents=CONTENTS, filename="genotypes.csv"): + """Build a FileStorage object for the tests.""" + return FileStorage( + stream=io.BytesIO(contents), filename=filename, + content_type="text/csv") -from uploader.files import sha256_digest_over_file @pytest.mark.unit_test @pytest.mark.parametrize( @@ -15,3 +39,114 @@ from uploader.files import sha256_digest_over_file def test_sha256_digest_over_file(filepath: Path, expectedhash: str): """Test the `sha256_digest_over_file` function.""" assert sha256_digest_over_file(filepath) == expectedhash + + +@pytest.mark.unit_test +def test_resolve_direct_upload_returns_path_and_original_name(tmp_path): + """ + GIVEN: a direct (non-resumable) multipart upload + WHEN: the uploaded file is resolved + THEN: the file is saved and its path and original name are returned + """ + result = resolve_uploaded_file( + {}, {UPLOAD_FIELD: _file_storage()}, tmp_path, UPLOAD_FIELD) + path, original_name = _right_value(result) + assert original_name == "genotypes.csv" + assert path.parent == tmp_path + assert path.read_bytes() == CONTENTS + + +@pytest.mark.unit_test +def test_resolve_direct_upload_honours_hashed_flag(tmp_path): + """ + GIVEN: a direct upload and `hashed=False` + WHEN: the uploaded file is resolved + THEN: the file is saved under its original (sanitised) name + """ + result = resolve_uploaded_file( + {}, {UPLOAD_FIELD: _file_storage()}, tmp_path, UPLOAD_FIELD, + hashed=False) + path, original_name = _right_value(result) + assert path.name == original_name == "genotypes.csv" + + +@pytest.mark.unit_test +def test_resolve_resumable_upload_returns_path_and_original_name(tmp_path): + """ + GIVEN: a resumable upload whose merged file already exists + WHEN: the uploaded file is resolved + THEN: the merged file's path and the original name are returned + """ + (tmp_path / "abc123.csv").write_text(CONTENTS.decode("utf8"), encoding="utf8") + form = { + "resumable-upload": "true", + UPLOAD_FIELD: json.dumps({ + "uploaded-file": "abc123.csv", + "original-name": "original.csv"})} + result = resolve_uploaded_file(form, {}, tmp_path, UPLOAD_FIELD) + assert _right_value(result) == (tmp_path / "abc123.csv", "original.csv") + + +@pytest.mark.unit_test +def test_resolve_resumable_upload_missing_file_is_left(tmp_path): + """ + GIVEN: a resumable upload whose merged file is missing + WHEN: the uploaded file is resolved + THEN: a Left describing the missing file is returned + """ + form = { + "resumable-upload": "true", + UPLOAD_FIELD: json.dumps({"uploaded-file": "missing.csv"})} + result = resolve_uploaded_file(form, {}, tmp_path, UPLOAD_FIELD) + assert result.is_left() + assert "missing.csv" in _left_value(result) + + +@pytest.mark.unit_test +def test_resolve_resumable_upload_bad_json_is_left(tmp_path): + """ + GIVEN: a resumable upload whose form data is not valid JSON + WHEN: the uploaded file is resolved + THEN: a Left is returned + """ + form = {"resumable-upload": "true", UPLOAD_FIELD: "not-json"} + result = resolve_uploaded_file(form, {}, tmp_path, UPLOAD_FIELD) + assert result.is_left() + + +@pytest.mark.unit_test +def test_resolve_resumable_upload_without_uploaded_file_is_left(tmp_path): + """ + GIVEN: a resumable upload whose form data lacks the file name + WHEN: the uploaded file is resolved + THEN: a Left is returned + """ + form = { + "resumable-upload": "true", + UPLOAD_FIELD: json.dumps({"original-name": "original.csv"})} + result = resolve_uploaded_file(form, {}, tmp_path, UPLOAD_FIELD) + assert result.is_left() + + +@pytest.mark.unit_test +def test_resolve_direct_upload_without_file_is_left(tmp_path): + """ + GIVEN: a direct upload with no file + WHEN: the uploaded file is resolved + THEN: a Left naming the expected field is returned + """ + result = resolve_uploaded_file({}, {}, tmp_path, UPLOAD_FIELD) + assert result.is_left() + assert UPLOAD_FIELD in _left_value(result) + + +@pytest.mark.unit_test +def test_resolve_upload_uses_the_requested_field(tmp_path): + """ + GIVEN: a file supplied under a different field name + WHEN: a differently named field is resolved + THEN: a Left is returned + """ + result = resolve_uploaded_file( + {}, {"some-other-field": _file_storage()}, tmp_path, UPLOAD_FIELD) + assert result.is_left() |
