-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_metadata.py
78 lines (71 loc) · 3.23 KB
/
test_metadata.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
import pytest
from libcloud.storage.drivers.local import LocalStorageDriver
from libcloud.storage.types import ObjectDoesNotExistError
from sqlalchemy_file.helpers import get_content_from_file_obj
from sqlalchemy_file.storage import StorageManager
from tests.utils import get_test_container
class TestMetadata:
def setup_method(self, method) -> None:
StorageManager._clear()
StorageManager.add_storage("test", get_test_container("test-metadata"))
def test_add_metadata_deprecated(self):
with pytest.warns(DeprecationWarning, match="metadata attribute is deprecated"):
name = "test_metadata.txt"
stored_file = StorageManager.save_file(
name,
get_content_from_file_obj(b"Test metadata"),
metadata={
"content_type": "text/plain",
"filename": "test_metadata.txt",
},
)
if isinstance(stored_file.object.driver, LocalStorageDriver):
assert (
stored_file.object.container.get_object(f"{name}.metadata.json")
is not None
)
else:
with pytest.raises(ObjectDoesNotExistError):
stored_file.object.container.get_object(f"{name}.metadata.json")
assert stored_file.filename == "test_metadata.txt"
assert stored_file.content_type == "text/plain"
StorageManager.delete_file("test/test_metadata.txt")
with pytest.raises(ObjectDoesNotExistError):
StorageManager.get().get_object("test_metadata.txt.metadata.json")
def test_add_metadata(self):
name = "test_metadata.txt"
stored_file = StorageManager.save_file(
name,
get_content_from_file_obj(b"Test metadata"),
extra={
"meta_data": {
"content_type": "text/plain",
"filename": "test_metadata.txt",
}
},
)
if isinstance(stored_file.object.driver, LocalStorageDriver):
assert (
stored_file.object.container.get_object(f"{name}.metadata.json")
is not None
)
else:
with pytest.raises(ObjectDoesNotExistError):
stored_file.object.container.get_object(f"{name}.metadata.json")
assert stored_file.filename == "test_metadata.txt"
assert stored_file.content_type == "text/plain"
StorageManager.delete_file("test/test_metadata.txt")
with pytest.raises(ObjectDoesNotExistError):
StorageManager.get().get_object("test_metadata.txt.metadata.json")
def test_no_metadata(self):
name = "test_metadata.txt"
stored_file = StorageManager.save_file(
name, get_content_from_file_obj(b"Test metadata")
)
with pytest.raises(ObjectDoesNotExistError):
stored_file.object.container.get_object(f"{name}.metadata.json")
assert stored_file.filename == "unnamed"
assert stored_file.content_type == "application/octet-stream"
StorageManager.delete_file("test/test_metadata.txt")
def teardown_method(self, method):
StorageManager.get().delete()