forked from ietf-tools/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
80 lines (72 loc) · 3.44 KB
/
Copy pathtests.py
File metadata and controls
80 lines (72 loc) · 3.44 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
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
# Copyright The IETF Trust 2025, All Rights Reserved
import datetime
from django.core.files.base import ContentFile
from ietf.utils.test_utils import TestCase
from .factories import BlobFactory
from .models import Blob
from .storage import BlobFile, BlobdbStorage
class StorageTests(TestCase):
def test_save(self):
storage = BlobdbStorage(bucket_name="my-bucket")
timestamp = datetime.datetime(
2025,
3,
17,
1,
2,
3,
tzinfo=datetime.timezone.utc,
)
# Create file to save
my_file = BlobFile(
content=b"These are my bytes.",
mtime=timestamp,
content_type="application/x-my-content-type",
)
# save the file
saved_name = storage.save("myfile.txt", my_file)
# validate the outcome
self.assertEqual(saved_name, "myfile.txt")
blob = Blob.objects.filter(bucket="my-bucket", name="myfile.txt").first()
self.assertIsNotNone(blob) # validates bucket and name
self.assertEqual(bytes(blob.content), b"These are my bytes.")
self.assertEqual(blob.mtime, timestamp)
self.assertEqual(blob.content_type, "application/x-my-content-type")
def test_save_naive_file(self):
storage = BlobdbStorage(bucket_name="my-bucket")
my_naive_file = ContentFile(content=b"These are my naive bytes.")
# save the file
saved_name = storage.save("myfile.txt", my_naive_file)
# validate the outcome
self.assertEqual(saved_name, "myfile.txt")
blob = Blob.objects.filter(bucket="my-bucket", name="myfile.txt").first()
self.assertIsNotNone(blob) # validates bucket and name
self.assertEqual(bytes(blob.content), b"These are my naive bytes.")
self.assertIsNone(blob.mtime)
self.assertEqual(blob.content_type, "")
def test_open(self):
"""BlobdbStorage open yields a BlobFile with specific mtime and content_type"""
mtime = datetime.datetime(2021, 1, 2, 3, 45, tzinfo=datetime.timezone.utc)
blob = BlobFactory(mtime=mtime, content_type="application/x-oh-no-you-didnt")
storage = BlobdbStorage(bucket_name=blob.bucket)
with storage.open(blob.name, "rb") as f:
self.assertTrue(isinstance(f, BlobFile))
assert isinstance(f, BlobFile) # redundant, narrows type for linter
self.assertEqual(f.read(), bytes(blob.content))
self.assertEqual(f.mtime, mtime)
self.assertEqual(f.content_type, "application/x-oh-no-you-didnt")
def test_open_null_mtime(self):
"""BlobdbStorage open yields a BlobFile with default mtime and content_type"""
blob = BlobFactory(content_type="application/x-oh-no-you-didnt") # does not set mtime
storage = BlobdbStorage(bucket_name=blob.bucket)
with storage.open(blob.name, "rb") as f:
self.assertTrue(isinstance(f, BlobFile))
assert isinstance(f, BlobFile) # redundant, narrows type for linter
self.assertEqual(f.read(), bytes(blob.content))
self.assertIsNotNone(f.mtime)
self.assertEqual(f.mtime, blob.modified)
self.assertEqual(f.content_type, "application/x-oh-no-you-didnt")
def test_open_file_not_found(self):
storage = BlobdbStorage(bucket_name="not-a-bucket")
with self.assertRaises(FileNotFoundError):
storage.open("definitely/not-a-file.txt")