import tempfile import unittest from pathlib import Path from unittest.mock import patch from B03_FileInput.B03_FileInput_Engine import ( resolve_upload_destination, save_upload_stream, ) from B03_FileInput.B03_FileInput_Schema import FileUploadDescriptor class ResolveUploadDestinationTest(unittest.TestCase): def test_resolve_upload_destination(self) -> None: with tempfile.TemporaryDirectory() as temporary_directory: descriptor = FileUploadDescriptor( original_filename="cloud_merged.LAS", size_bytes=1024, ) destination = resolve_upload_destination(temporary_directory, descriptor) self.assertEqual( destination, Path(temporary_directory).resolve() / "B03_FileInput" / "input" / "las" / "cloud_merged.LAS", ) self.assertTrue(destination.parent.is_dir()) class SaveUploadStreamTest(unittest.IsolatedAsyncioTestCase): async def test_save_upload_stream(self) -> None: class FakeUpload: def __init__(self, chunks: list[bytes]) -> None: self.chunks = iter(chunks) async def read(self, _size: int) -> bytes: return next(self.chunks, b"") with tempfile.TemporaryDirectory() as temporary_directory: destination = Path(temporary_directory) / "cloud.las" upload = FakeUpload([b"abc", b"def"]) written_bytes = await save_upload_stream(upload, destination) # type: ignore[arg-type] self.assertEqual(written_bytes, 6) self.assertEqual(destination.read_bytes(), b"abcdef") self.assertEqual(list(destination.parent.glob("*.upload")), []) with patch("B03_FileInput.B03_FileInput_Engine.UPLOAD_MAX_MB", 0): with self.assertRaises(ValueError): await save_upload_stream( # type: ignore[arg-type] FakeUpload([b"too-large"]), destination, )