|
| 1 | +""" Unit tests for the metadata handling routines. """ |
| 2 | + |
| 3 | +import json |
| 4 | +import uuid |
| 5 | + |
| 6 | +from collections.abc import Generator |
| 7 | +from pathlib import Path as PyPath |
| 8 | +from pytest import fixture |
| 9 | + |
| 10 | +from staging_service.utils import Path |
| 11 | +from staging_service.metadata import some_metadata |
| 12 | + |
| 13 | +from tests.test_app import FileUtil |
| 14 | + |
| 15 | +# TODO TEST add more unit tests here. |
| 16 | + |
| 17 | + |
| 18 | +@fixture(scope="module") |
| 19 | +def temp_dir() -> Generator[PyPath, None, None]: |
| 20 | + with FileUtil() as fu: |
| 21 | + childdir = PyPath(fu.make_dir(str(uuid.uuid4()))).resolve() |
| 22 | + |
| 23 | + yield childdir |
| 24 | + |
| 25 | + # FileUtil will auto delete after exiting |
| 26 | + |
| 27 | + |
| 28 | +async def test_incomplete_metadata_file_update(temp_dir: Path): |
| 29 | + """ |
| 30 | + Tests the case where a file is listed or checked for existance prior to completing |
| 31 | + upload, and then an UPA is added to the file. This previously caused incomplete metadata |
| 32 | + to be returned as the logic for whether to run the metadata regeneration code based on |
| 33 | + the contents of the current metadata file was incorrect. |
| 34 | + See https://kbase-jira.atlassian.net/browse/PTV-1767 |
| 35 | + """ |
| 36 | + await _incomplete_metadata_file_update( |
| 37 | + temp_dir, |
| 38 | + {"source": "some place", "UPA": "1/2/3"}, |
| 39 | + "some place" |
| 40 | + ) |
| 41 | + |
| 42 | + await _incomplete_metadata_file_update( |
| 43 | + temp_dir, |
| 44 | + {"UPA": "1/2/3"}, |
| 45 | + "Unknown" |
| 46 | + ) |
| 47 | + |
| 48 | +async def _incomplete_metadata_file_update(temp_dir, metadict, source): |
| 49 | + target = Path( |
| 50 | + str(temp_dir / "full"), |
| 51 | + str(temp_dir / "meta"), |
| 52 | + "user_path", |
| 53 | + "myfilename", |
| 54 | + "super_fake_jgi_path") |
| 55 | + |
| 56 | + with open(target.full_path, "w") as p: |
| 57 | + p.writelines(make_test_lines(1, 6)) |
| 58 | + with open(target.metadata_path, "w") as p: |
| 59 | + p.write(json.dumps(metadict)) |
| 60 | + |
| 61 | + res = await some_metadata(target) |
| 62 | + |
| 63 | + assert "mtime" in res # just check the file time is there |
| 64 | + del res["mtime"] |
| 65 | + |
| 66 | + assert res == { |
| 67 | + "source": source, |
| 68 | + "md5": "9f07da9655223b777121141ff7735f25", |
| 69 | + "head": "".join(make_test_lines(1, 5)), |
| 70 | + "tail": "".join(make_test_lines(2, 6)), |
| 71 | + "UPA": "1/2/3", |
| 72 | + "isFolder": False, |
| 73 | + "lineCount": "5", |
| 74 | + "name": "myfilename", |
| 75 | + "path": "user_path", |
| 76 | + "size": 1280 |
| 77 | + } |
| 78 | + |
| 79 | +def make_test_lines(start, stop): |
| 80 | + return [str(i) + "a" * (256 - len(str(i)) - 1) + "\n" for i in range(start, stop)] |
0 commit comments