Skip to content

Commit 321fc84

Browse files
add sqlalchemy python_type (#153)
* add sqlalchemy python_type * add tests
1 parent bbdc68f commit 321fc84

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

sqlalchemy_file/types.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ class FileField(types.TypeDecorator): # type: ignore
3535
impl = types.JSON
3636
cache_ok = False
3737

38+
@property
39+
def python_type(self) -> Type[Union[File, List[File]]]:
40+
if self.multiple:
41+
return MutableList[File]
42+
return File
43+
3844
def __init__(
3945
self,
4046
*args: Tuple[Any],

tests/test_multiple_field.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from libcloud.storage.types import ObjectDoesNotExistError
55
from sqlalchemy import Column, Integer, String, select
66
from sqlalchemy.orm import Session, declarative_base
7+
from sqlalchemy_file.file import File
8+
from sqlalchemy_file.mutable_list import MutableList
79
from sqlalchemy_file.storage import StorageManager
810
from sqlalchemy_file.types import FileField
911

@@ -47,6 +49,10 @@ def setup_method(self, method) -> None:
4749
StorageManager._clear()
4850
StorageManager.add_storage("test", get_test_container("test-multiple-field"))
4951

52+
def test_python_type(self) -> None:
53+
field = FileField(multiple=True)
54+
assert field.python_type == MutableList[File]
55+
5056
def test_create_multiple_content(self, fake_file, fake_content) -> None:
5157
with Session(engine) as session:
5258
session.add(

tests/test_single_field.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ def setup_method(self, method) -> None:
6868
StorageManager._clear()
6969
StorageManager.add_storage("test", get_test_container("test-simple-field"))
7070

71+
def test_python_type(self) -> None:
72+
field = FileField()
73+
assert field.python_type is File
74+
7175
def test_create_from_string(self, fake_content) -> None:
7276
with Session(engine) as session:
7377
session.add(Attachment(name="Create fake string", content=fake_content))

0 commit comments

Comments
 (0)