2
2
import tempfile
3
3
4
4
import pytest
5
+ from libcloud .storage .types import ObjectDoesNotExistError
6
+ from PIL import Image
5
7
from sqlalchemy import Column , Integer , String , select
6
8
from sqlalchemy .orm import Session , declarative_base
7
9
from sqlalchemy_file .storage import StorageManager
@@ -51,8 +53,6 @@ def setup_method(self, method) -> None:
51
53
52
54
def test_create_image_with_thumbnail (self , fake_image ) -> None :
53
55
with Session (engine ) as session :
54
- from PIL import Image
55
-
56
56
session .add (Book (title = "Pointless Meetings" , cover = fake_image ))
57
57
session .flush ()
58
58
book = session .execute (
@@ -66,6 +66,38 @@ def test_create_image_with_thumbnail(self, fake_image) -> None:
66
66
assert book .cover ["thumbnail" ]["width" ] == thumbnail .width
67
67
assert book .cover ["thumbnail" ]["height" ] == thumbnail .height
68
68
69
+ def test_update_image_with_thumbnail (self , fake_image ) -> None :
70
+ with Session (engine ) as session :
71
+ session .add (Book (title = "Pointless Meetings" , cover = fake_image ))
72
+ session .commit ()
73
+ book = session .execute (
74
+ select (Book ).where (Book .title == "Pointless Meetings" )
75
+ ).scalar_one ()
76
+ old_file_id = book .cover .path
77
+ old_thumbnail_file_id = book .cover .thumbnail ["path" ]
78
+ book .cover = fake_image
79
+ session .commit ()
80
+ with pytest .raises (ObjectDoesNotExistError ):
81
+ assert StorageManager .get_file (old_file_id )
82
+ with pytest .raises (ObjectDoesNotExistError ):
83
+ assert StorageManager .get_file (old_thumbnail_file_id )
84
+
85
+ def test_delete_image_with_thumbnail (self , fake_image ) -> None :
86
+ with Session (engine ) as session :
87
+ session .add (Book (title = "Pointless Meetings" , cover = fake_image ))
88
+ session .commit ()
89
+ book = session .execute (
90
+ select (Book ).where (Book .title == "Pointless Meetings" )
91
+ ).scalar_one ()
92
+ old_file_id = book .cover .path
93
+ old_thumbnail_file_id = book .cover .thumbnail ["path" ]
94
+ session .delete (book )
95
+ session .commit ()
96
+ with pytest .raises (ObjectDoesNotExistError ):
97
+ assert StorageManager .get_file (old_file_id )
98
+ with pytest .raises (ObjectDoesNotExistError ):
99
+ assert StorageManager .get_file (old_thumbnail_file_id )
100
+
69
101
def teardown_method (self , method ):
70
102
for obj in StorageManager .get ().list_objects ():
71
103
obj .delete ()
0 commit comments