From 4b720b59df6d6f5e53dd372b0d8a739eb0c1d7b9 Mon Sep 17 00:00:00 2001 From: Jocelin Hounon Date: Thu, 7 Sep 2023 17:35:57 -0500 Subject: [PATCH] Add more tests & fix linting (#109) --- sqlalchemy_file/file.py | 2 +- sqlalchemy_file/processors.py | 5 ++++- sqlalchemy_file/storage.py | 12 ++++++++---- sqlalchemy_file/validators.py | 2 +- tests/test_file.py | 9 +++++++++ tests/test_single_field.py | 6 +++++- tests/test_storage_manager.py | 6 ++++++ 7 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 tests/test_file.py diff --git a/sqlalchemy_file/file.py b/sqlalchemy_file/file.py index e11da45..62d7151 100644 --- a/sqlalchemy_file/file.py +++ b/sqlalchemy_file/file.py @@ -56,7 +56,7 @@ def __init__( self._freeze() else: self.content_path = content_path - if content is None: + if content_path is not None: self.original_content = None filename = filename or os.path.basename(content_path) size = os.path.getsize(content_path) diff --git a/sqlalchemy_file/processors.py b/sqlalchemy_file/processors.py index a9ee491..a21b010 100644 --- a/sqlalchemy_file/processors.py +++ b/sqlalchemy_file/processors.py @@ -122,7 +122,10 @@ def process(self, file: "File", upload_storage: Optional[str] = None) -> None: ) extra.update({"content_type": content_type, "meta_data": metadata}) stored_file = file.store_content( - output, upload_storage=upload_storage, extra=extra, headers=file.get("headers", None) + output, + upload_storage=upload_storage, + extra=extra, + headers=file.get("headers", None), ) file.update( { diff --git a/sqlalchemy_file/storage.py b/sqlalchemy_file/storage.py index 3df86c4..4943a8d 100644 --- a/sqlalchemy_file/storage.py +++ b/sqlalchemy_file/storage.py @@ -68,14 +68,14 @@ def save_file( cls, name: str, content: Optional[Iterator[bytes]] = None, - content_path: Optional[str] = None, upload_storage: Optional[str] = None, metadata: Optional[Dict[str, Any]] = None, extra: Optional[Dict[str, Any]] = None, headers: Optional[Dict[str, str]] = None, + content_path: Optional[str] = None, ) -> StoredFile: if content is None and content_path is None: - raise ValueError("Either conent or content_path must be specified") + raise ValueError("Either content or content_path must be specified") if metadata is not None: warnings.warn( 'metadata attribute is deprecated. Use extra={"meta_data": ...} instead', @@ -104,12 +104,16 @@ def save_file( iterator=get_metadata_file_obj(extra["meta_data"]), object_name=f"{name}.metadata.json", ) - if content is None: + if content_path is not None: return StoredFile( container.upload_object( - file_path=content_path, object_name=name, extra=extra, headers=headers + file_path=content_path, + object_name=name, + extra=extra, + headers=headers, ) ) + assert content is not None return StoredFile( container.upload_object_via_stream( iterator=content, object_name=name, extra=extra, headers=headers diff --git a/sqlalchemy_file/validators.py b/sqlalchemy_file/validators.py index c2743dc..6fdf69f 100644 --- a/sqlalchemy_file/validators.py +++ b/sqlalchemy_file/validators.py @@ -225,4 +225,4 @@ def process(self, file: "File", attr_key: str) -> None: f"{self.min_aspect_ratio} - {self.max_aspect_ratio}", ) file.update({"width": width, "height": height}) - file.original_content.seek(0) + file.original_content.seek(0) # type: ignore[union-attr] diff --git a/tests/test_file.py b/tests/test_file.py new file mode 100644 index 0000000..bf111e4 --- /dev/null +++ b/tests/test_file.py @@ -0,0 +1,9 @@ +import pytest +from sqlalchemy_file.file import File + + +def test_file_missing_content(): + with pytest.raises( + ValueError, match="Either content or content_path must be specified" + ): + File() diff --git a/tests/test_single_field.py b/tests/test_single_field.py index 9d647b5..b6b3deb 100644 --- a/tests/test_single_field.py +++ b/tests/test_single_field.py @@ -102,7 +102,11 @@ def test_create_fromfile(self, fake_file, fake_content) -> None: def test_create_frompath(self, fake_file, fake_content) -> None: with Session(engine) as session: - session.add(Attachment(name="Create Fake file", content=File(content_path=fake_file.name))) + session.add( + Attachment( + name="Create Fake file", content=File(content_path=fake_file.name) + ) + ) session.commit() attachment = session.execute( select(Attachment).where(Attachment.name == "Create Fake file") diff --git a/tests/test_storage_manager.py b/tests/test_storage_manager.py index 1e34469..c829968 100644 --- a/tests/test_storage_manager.py +++ b/tests/test_storage_manager.py @@ -37,3 +37,9 @@ def test_unique_storage_name(self) -> None: StorageManager.add_storage("first", get_dummy_container("first")) with pytest.raises(RuntimeError): StorageManager.add_storage("first", get_dummy_container("second")) + + def test_save_file_missing_content(self): + with pytest.raises( + ValueError, match="Either content or content_path must be specified" + ): + StorageManager.save_file("id")