Skip to content

Commit 4b720b5

Browse files
authored
Add more tests & fix linting (#109)
1 parent 0e2887a commit 4b720b5

File tree

7 files changed

+34
-8
lines changed

7 files changed

+34
-8
lines changed

sqlalchemy_file/file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def __init__(
5656
self._freeze()
5757
else:
5858
self.content_path = content_path
59-
if content is None:
59+
if content_path is not None:
6060
self.original_content = None
6161
filename = filename or os.path.basename(content_path)
6262
size = os.path.getsize(content_path)

sqlalchemy_file/processors.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ def process(self, file: "File", upload_storage: Optional[str] = None) -> None:
122122
)
123123
extra.update({"content_type": content_type, "meta_data": metadata})
124124
stored_file = file.store_content(
125-
output, upload_storage=upload_storage, extra=extra, headers=file.get("headers", None)
125+
output,
126+
upload_storage=upload_storage,
127+
extra=extra,
128+
headers=file.get("headers", None),
126129
)
127130
file.update(
128131
{

sqlalchemy_file/storage.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ def save_file(
6868
cls,
6969
name: str,
7070
content: Optional[Iterator[bytes]] = None,
71-
content_path: Optional[str] = None,
7271
upload_storage: Optional[str] = None,
7372
metadata: Optional[Dict[str, Any]] = None,
7473
extra: Optional[Dict[str, Any]] = None,
7574
headers: Optional[Dict[str, str]] = None,
75+
content_path: Optional[str] = None,
7676
) -> StoredFile:
7777
if content is None and content_path is None:
78-
raise ValueError("Either conent or content_path must be specified")
78+
raise ValueError("Either content or content_path must be specified")
7979
if metadata is not None:
8080
warnings.warn(
8181
'metadata attribute is deprecated. Use extra={"meta_data": ...} instead',
@@ -104,12 +104,16 @@ def save_file(
104104
iterator=get_metadata_file_obj(extra["meta_data"]),
105105
object_name=f"{name}.metadata.json",
106106
)
107-
if content is None:
107+
if content_path is not None:
108108
return StoredFile(
109109
container.upload_object(
110-
file_path=content_path, object_name=name, extra=extra, headers=headers
110+
file_path=content_path,
111+
object_name=name,
112+
extra=extra,
113+
headers=headers,
111114
)
112115
)
116+
assert content is not None
113117
return StoredFile(
114118
container.upload_object_via_stream(
115119
iterator=content, object_name=name, extra=extra, headers=headers

sqlalchemy_file/validators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,4 @@ def process(self, file: "File", attr_key: str) -> None:
225225
f"{self.min_aspect_ratio} - {self.max_aspect_ratio}",
226226
)
227227
file.update({"width": width, "height": height})
228-
file.original_content.seek(0)
228+
file.original_content.seek(0) # type: ignore[union-attr]

tests/test_file.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import pytest
2+
from sqlalchemy_file.file import File
3+
4+
5+
def test_file_missing_content():
6+
with pytest.raises(
7+
ValueError, match="Either content or content_path must be specified"
8+
):
9+
File()

tests/test_single_field.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ def test_create_fromfile(self, fake_file, fake_content) -> None:
102102

103103
def test_create_frompath(self, fake_file, fake_content) -> None:
104104
with Session(engine) as session:
105-
session.add(Attachment(name="Create Fake file", content=File(content_path=fake_file.name)))
105+
session.add(
106+
Attachment(
107+
name="Create Fake file", content=File(content_path=fake_file.name)
108+
)
109+
)
106110
session.commit()
107111
attachment = session.execute(
108112
select(Attachment).where(Attachment.name == "Create Fake file")

tests/test_storage_manager.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,9 @@ def test_unique_storage_name(self) -> None:
3737
StorageManager.add_storage("first", get_dummy_container("first"))
3838
with pytest.raises(RuntimeError):
3939
StorageManager.add_storage("first", get_dummy_container("second"))
40+
41+
def test_save_file_missing_content(self):
42+
with pytest.raises(
43+
ValueError, match="Either content or content_path must be specified"
44+
):
45+
StorageManager.save_file("id")

0 commit comments

Comments
 (0)