Skip to content

Commit

Permalink
Add more tests & fix linting (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
jowilf authored Sep 7, 2023
1 parent 0e2887a commit 4b720b5
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion sqlalchemy_file/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion sqlalchemy_file/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
12 changes: 8 additions & 4 deletions sqlalchemy_file/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sqlalchemy_file/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
9 changes: 9 additions & 0 deletions tests/test_file.py
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 5 additions & 1 deletion tests/test_single_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions tests/test_storage_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 4b720b5

Please sign in to comment.