Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /Daylily-Informatics_bloom /tests /test_file_creations.py
| import os | |
| from io import BytesIO | |
| from pathlib import Path | |
| import boto3 | |
| import pytest | |
| import requests_mock | |
| from moto import mock_aws | |
| from bloom_lims.domain import BloomFile | |
| from bloom_lims.tapdb_adapter import BLOOMdb3 | |
| def s3_bucket(monkeypatch): | |
| monkeypatch.delenv("AWS_PROFILE", raising=False) | |
| monkeypatch.delenv("AWS_DEFAULT_PROFILE", raising=False) | |
| monkeypatch.setenv("AWS_ACCESS_KEY_ID", "testing") | |
| monkeypatch.setenv("AWS_SECRET_ACCESS_KEY", "testing") | |
| monkeypatch.setenv("AWS_SESSION_TOKEN", "testing") | |
| monkeypatch.setenv("AWS_DEFAULT_REGION", "us-east-1") | |
| with mock_aws(): | |
| s3 = boto3.client("s3", region_name="us-east-1") | |
| bucket_name = "daylily-dewey-0" | |
| s3.create_bucket(Bucket=bucket_name) | |
| yield bucket_name | |
| def db_session(): | |
| bdb = BLOOMdb3() | |
| yield bdb | |
| def bloom_file_instance(db_session, s3_bucket): | |
| return BloomFile(db_session, bucket_prefix="daylily-dewey-") | |
| def test_create_file_no_data(bloom_file_instance): | |
| new_file = bloom_file_instance.create_file( | |
| file_metadata={"description": "No data test", "import_or_remote": "import"} | |
| ) | |
| assert new_file is not None | |
| assert new_file.json_addl["properties"]["description"] == "No data test" | |
| assert ( | |
| new_file.json_addl["properties"]["current_s3_bucket_name"] == "daylily-dewey-0" | |
| ) | |
| def test_create_file_with_data(bloom_file_instance): | |
| data_path = Path("tests/test_pdf.pdf") | |
| with open(data_path, "rb") as f: | |
| file_data = BytesIO(f.read()) | |
| file_name = data_path.name | |
| new_file = bloom_file_instance.create_file( | |
| file_metadata={"description": "Data test", "import_or_remote": "import"}, | |
| file_data=file_data, | |
| file_name=file_name, | |
| ) | |
| assert new_file is not None | |
| assert new_file.json_addl["properties"]["description"] == "Data test" | |
| assert ( | |
| new_file.json_addl["properties"]["original_file_size_bytes"] | |
| == file_data.getbuffer().nbytes | |
| ) | |
| def test_create_file_with_local_path(bloom_file_instance): | |
| fn = "tests/test_png.png" | |
| new_file = bloom_file_instance.create_file( | |
| file_metadata={"description": "Local path test", "import_or_remote": "import"}, | |
| full_path_to_file=fn, | |
| ) | |
| assert new_file is not None | |
| assert new_file.json_addl["properties"]["description"] == "Local path test" | |
| assert new_file.json_addl["properties"][ | |
| "original_file_size_bytes" | |
| ] == os.path.getsize(fn) | |
| def test_create_file_with_url(bloom_file_instance): | |
| url = ( | |
| "https://github.com/Daylily-Informatics/bloom/blob/20240603b/tests/test_png.png" | |
| ) | |
| with requests_mock.Mocker() as m: | |
| m.get(url, content=b"test content") | |
| new_file = bloom_file_instance.create_file( | |
| file_metadata={"description": "URL test", "import_or_remote": "import"}, | |
| url=url, | |
| ) | |
| assert new_file is not None | |
| assert new_file.json_addl["properties"]["description"] == "URL test" | |
| assert new_file.json_addl["properties"]["original_file_size_bytes"] == len( | |
| b"test content" | |
| ) | |
| if __name__ == "__main__": | |
| pytest.main() | |
Xet Storage Details
- Size:
- 3.22 kB
- Xet hash:
- 3631728927e66ec3eac833a4c1244d0c00532f1bb883e9b15159b7141addeb13
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.