-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_multiple_field.py
368 lines (341 loc) · 15.1 KB
/
test_multiple_field.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import tempfile
import pytest
from libcloud.storage.types import ObjectDoesNotExistError
from sqlalchemy import Column, Integer, String, select
from sqlalchemy.orm import Session, declarative_base
from sqlalchemy_file.file import File
from sqlalchemy_file.mutable_list import MutableList
from sqlalchemy_file.storage import StorageManager
from sqlalchemy_file.types import FileField
from tests.utils import get_test_container, get_test_engine
engine = get_test_engine()
Base = declarative_base()
@pytest.fixture
def fake_content():
return "This is a fake file"
@pytest.fixture
def fake_file(fake_content):
file = tempfile.NamedTemporaryFile()
file.write(fake_content.encode())
file.seek(0)
return file
class Attachment(Base):
__tablename__ = "attachment"
id = Column(Integer, autoincrement=True, primary_key=True)
name = Column(String(50), unique=True)
multiple_content = Column(FileField(multiple=True))
def __repr__(self):
return "<Attachment: id {} ; name: {}; multiple_content {}>".format(
self.id,
self.name,
self.multiple_content,
) # pragma: no cover
class TestMultipleField:
def setup_method(self, method) -> None:
Base.metadata.create_all(engine)
StorageManager._clear()
StorageManager.add_storage("test", get_test_container("test-multiple-field"))
def test_python_type(self) -> None:
field = FileField(multiple=True)
assert field.python_type == MutableList[File]
def test_create_multiple_content(self, fake_file, fake_content) -> None:
with Session(engine) as session:
session.add(
Attachment(
name="Create multiple",
multiple_content=[
"from str",
b"from bytes",
fake_file,
],
)
)
session.commit()
attachment = session.execute(
select(Attachment).where(Attachment.name == "Create multiple")
).scalar_one()
assert attachment.multiple_content[0].file.read().decode() == "from str"
assert attachment.multiple_content[1].file.read() == b"from bytes"
assert attachment.multiple_content[2].file.read() == fake_content.encode()
def test_create_multiple_content_rollback(self, fake_file, fake_content) -> None:
with Session(engine) as session:
session.add(
Attachment(
name="Create multiple content rollback",
multiple_content=[
"from str",
b"from bytes",
fake_file,
],
)
)
session.flush()
attachment = session.execute(
select(Attachment).where(
Attachment.name == "Create multiple content rollback"
)
).scalar_one()
paths = [p["path"] for p in attachment.multiple_content]
assert all(StorageManager.get_file(path) is not None for path in paths)
session.rollback()
for path in paths:
with pytest.raises(ObjectDoesNotExistError):
StorageManager.get_file(path)
def test_edit_existing_multiple_content(self) -> None:
with Session(engine) as session:
session.add(
Attachment(
name="Multiple content edit all",
multiple_content=[b"Content 1", b"Content 2"],
)
)
session.commit()
attachment = session.execute(
select(Attachment).where(Attachment.name == "Multiple content edit all")
).scalar_one()
old_paths = [f["path"] for f in attachment.multiple_content]
attachment.multiple_content = [b"Content 1 edit", b"Content 2 edit"]
session.add(attachment)
session.commit()
session.refresh(attachment)
assert attachment.multiple_content[0].file.read() == b"Content 1 edit"
assert attachment.multiple_content[1].file.read() == b"Content 2 edit"
for path in old_paths:
with pytest.raises(ObjectDoesNotExistError):
StorageManager.get_file(path)
def test_edit_existing_multiple_content_rollback(self) -> None:
with Session(engine) as session:
session.add(
Attachment(
name="Multiple content edit all rollback",
multiple_content=[b"Content 1", b"Content 2"],
)
)
session.commit()
attachment = session.execute(
select(Attachment).where(
Attachment.name == "Multiple content edit all rollback"
)
).scalar_one()
old_paths = [f["path"] for f in attachment.multiple_content]
attachment.multiple_content = [b"Content 1 edit", b"Content 2 edit"]
session.add(attachment)
session.flush()
session.refresh(attachment)
assert attachment.multiple_content[0].file.read() == b"Content 1 edit"
assert attachment.multiple_content[1].file.read() == b"Content 2 edit"
new_paths = [f["path"] for f in attachment.multiple_content]
session.rollback()
for path in new_paths:
with pytest.raises(ObjectDoesNotExistError):
StorageManager.get_file(path)
for path in old_paths:
assert StorageManager.get_file(path) is not None
def test_edit_existing_multiple_content_add_element(self) -> None:
with Session(engine) as session:
session.add(
Attachment(
name="Multiple content add element",
multiple_content=[b"Content 1", b"Content 2"],
)
)
session.commit()
attachment = session.execute(
select(Attachment).where(
Attachment.name == "Multiple content add element"
)
).scalar_one()
assert len(attachment.multiple_content) == 2
attachment.multiple_content.append(b"Content 3")
attachment.multiple_content += [b"Content 4"]
attachment.multiple_content.extend([b"Content 5"])
session.add(attachment)
session.commit()
session.refresh(attachment)
assert len(attachment.multiple_content) == 5
assert attachment.multiple_content[0].file.read() == b"Content 1"
assert attachment.multiple_content[1].file.read() == b"Content 2"
assert attachment.multiple_content[2].file.read() == b"Content 3"
assert attachment.multiple_content[3].file.read() == b"Content 4"
assert attachment.multiple_content[4].file.read() == b"Content 5"
def test_edit_existing_multiple_content_add_element_rollback(self) -> None:
with Session(engine) as session:
session.add(
Attachment(
name="Multiple content add element rollback",
multiple_content=[b"Content 1", b"Content 2"],
)
)
session.commit()
attachment = session.execute(
select(Attachment).where(
Attachment.name == "Multiple content add element rollback"
)
).scalar_one()
attachment.multiple_content += [b"Content 3", b"Content 4"]
session.add(attachment)
session.flush()
session.refresh(attachment)
assert len(attachment.multiple_content) == 4
path3 = attachment.multiple_content[2].path
path4 = attachment.multiple_content[3].path
assert StorageManager.get_file(path3) is not None
assert StorageManager.get_file(path4) is not None
session.rollback()
assert len(attachment.multiple_content) == 2
for path in (path3, path4):
with pytest.raises(ObjectDoesNotExistError):
StorageManager.get_file(path)
def test_edit_existing_multiple_content_remove_element(self) -> None:
with Session(engine) as session:
session.add(
Attachment(
name="Multiple content remove element",
multiple_content=[
b"Content 1",
b"Content 2",
b"Content 3",
b"Content 4",
b"Content 5",
],
)
)
session.commit()
attachment = session.execute(
select(Attachment).where(
Attachment.name == "Multiple content remove element"
)
).scalar_one()
first_removed = attachment.multiple_content.pop(1)
second_removed = attachment.multiple_content[3]
attachment.multiple_content.remove(second_removed)
third_removed = attachment.multiple_content[2]
del attachment.multiple_content[2]
session.add(attachment)
session.commit()
session.refresh(attachment)
assert len(attachment.multiple_content) == 2
assert attachment.multiple_content[0].file.read() == b"Content 1"
assert attachment.multiple_content[1].file.read() == b"Content 3"
for file in (first_removed, second_removed, third_removed):
with pytest.raises(ObjectDoesNotExistError):
StorageManager.get_file(file.path)
def test_edit_existing_multiple_content_remove_element_rollback(self) -> None:
with Session(engine) as session:
session.add(
Attachment(
name="Multiple content remove element rollback",
multiple_content=[b"Content 1", b"Content 2", b"Content 3"],
)
)
session.commit()
attachment = session.execute(
select(Attachment).where(
Attachment.name == "Multiple content remove element rollback"
)
).scalar_one()
attachment.multiple_content.pop(0)
session.add(attachment)
session.flush()
session.refresh(attachment)
assert len(attachment.multiple_content) == 2
assert attachment.multiple_content[0].file.read() == b"Content 2"
assert attachment.multiple_content[1].file.read() == b"Content 3"
session.rollback()
assert len(attachment.multiple_content) == 3
assert attachment.multiple_content[0].file.read() == b"Content 1"
assert attachment.multiple_content[1].file.read() == b"Content 2"
assert attachment.multiple_content[2].file.read() == b"Content 3"
def test_edit_existing_multiple_content_replace_element(self, fake_file) -> None:
with Session(engine) as session:
session.add(
Attachment(
name="Multiple content replace",
multiple_content=[b"Content 1", b"Content 2", b"Content 3"],
)
)
session.commit()
attachment = session.execute(
select(Attachment).where(Attachment.name == "Multiple content replace")
).scalar_one()
before_replaced_path = attachment.multiple_content[1].path
attachment.multiple_content[1] = b"Content 2 replaced"
session.add(attachment)
session.commit()
session.refresh(attachment)
assert attachment.multiple_content[1].file.read() == b"Content 2 replaced"
with pytest.raises(ObjectDoesNotExistError):
StorageManager.get_file(before_replaced_path)
def test_edit_existing_multiple_content_replace_element_rollback(
self, fake_file
) -> None:
with Session(engine) as session:
session.add(
Attachment(
name="Multiple content replace rollback",
multiple_content=[b"Content 1", b"Content 2", b"Content 3"],
)
)
session.commit()
attachment = session.execute(
select(Attachment).where(
Attachment.name == "Multiple content replace rollback"
)
).scalar_one()
attachment.multiple_content[1] = b"Content 2 replaced"
session.add(attachment)
session.flush()
session.refresh(attachment)
assert attachment.multiple_content[1].file.read() == b"Content 2 replaced"
new_path = attachment.multiple_content[1].path
session.rollback()
assert attachment.multiple_content[1].file.read() == b"Content 2"
with pytest.raises(ObjectDoesNotExistError):
StorageManager.get_file(new_path)
def test_delete_existing_multiple_content(self, fake_file) -> None:
with Session(engine) as session:
session.add(
Attachment(
name="Deleting multiple content",
multiple_content=[b"Content 1", b"Content 2", b"Content 3"],
)
)
session.commit()
attachment = session.execute(
select(Attachment).where(Attachment.name == "Deleting multiple content")
).scalar_one()
file_ids = [f.file_id for f in attachment.multiple_content]
for file_id in file_ids:
assert StorageManager.get().get_object(file_id) is not None
session.delete(attachment)
session.commit()
for file_id in file_ids:
with pytest.raises(ObjectDoesNotExistError):
StorageManager.get().get_object(file_id)
def test_delete_existing_multiple_content_rollback(self, fake_file) -> None:
with Session(engine) as session:
session.add(
Attachment(
name="Deleting multiple content rollback",
multiple_content=[b"Content 1", b"Content 2", b"Content 3"],
)
)
session.commit()
attachment = session.execute(
select(Attachment).where(
Attachment.name == "Deleting multiple content rollback"
)
).scalar_one()
file_ids = [f.file_id for f in attachment.multiple_content]
for file_id in file_ids:
assert StorageManager.get().get_object(file_id) is not None
session.delete(attachment)
session.flush()
session.rollback()
for file_id in file_ids:
assert StorageManager.get().get_object(file_id) is not None
def teardown_method(self, method):
for obj in StorageManager.get().list_objects():
obj.delete()
StorageManager.get().delete()
Base.metadata.drop_all(engine)