Repository navigation
Expand file tree
/
Copy pathtest_attachments.py
More file actions
49 lines (32 loc) · 1.64 KB
/
Copy pathtest_attachments.py
File metadata and controls
49 lines (32 loc) · 1.64 KB
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
"""Resource-ownership tests for attachment snapshots."""
from pathlib import Path
from eventcalendar.core.attachments import AttachmentStore, ImageAttachmentPayload
def _payload(path: Path, source: str) -> ImageAttachmentPayload:
path.write_bytes(b"image")
return ImageAttachmentPayload(source_path=source, mime_type="image/png", temp_path=str(path))
def test_store_owns_accepted_files_and_disposes_rejected_files(tmp_path: Path) -> None:
store = AttachmentStore(limit=1)
accepted = _payload(tmp_path / "accepted.png", "source-a")
over_limit = _payload(tmp_path / "rejected.png", "source-b")
assert store.add([accepted, over_limit]) == [accepted]
assert Path(accepted.temp_path).exists()
assert not Path(over_limit.temp_path).exists()
assert list(store.payloads) == [accepted]
store.clear()
assert not Path(accepted.temp_path).exists()
assert list(store.payloads) == []
def test_store_rejects_duplicate_source_and_cleans_candidate(tmp_path: Path) -> None:
store = AttachmentStore(limit=2)
first = _payload(tmp_path / "first.png", "same-source")
duplicate = _payload(tmp_path / "duplicate.png", "same-source")
assert store.add([first]) == [first]
assert store.add([duplicate]) == []
assert not Path(duplicate.temp_path).exists()
store.clear()
def test_resubmitting_same_payload_does_not_delete_owned_file(tmp_path: Path) -> None:
store = AttachmentStore(limit=2)
payload = _payload(tmp_path / "same.png", "same-source")
assert store.add([payload, payload]) == [payload]
assert Path(payload.temp_path).exists()
store.clear()
assert not Path(payload.temp_path).exists()