-
Notifications
You must be signed in to change notification settings - Fork 631
/
Copy pathtest_lfs.py
179 lines (154 loc) · 8.73 KB
/
test_lfs.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
import os
import unittest
from hashlib import sha256
from io import BytesIO
from huggingface_hub.lfs import UploadInfo
from huggingface_hub.utils import SoftTemporaryDirectory
from huggingface_hub.utils._lfs import SliceFileObj
class TestUploadInfo(unittest.TestCase):
def setUp(self) -> None:
self.content = b"RandOm ConTEnT" * 1024
self.size = len(self.content)
self.sha = sha256(self.content).digest()
self.sample = self.content[:512]
def test_upload_info_from_path(self):
with SoftTemporaryDirectory() as tmpdir:
filepath = os.path.join(tmpdir, "file.bin")
with open(filepath, "wb+") as file:
file.write(self.content)
upload_info = UploadInfo.from_path(filepath)
self.assertEqual(upload_info.sample, self.sample)
self.assertEqual(upload_info.size, self.size)
self.assertEqual(upload_info.sha256, self.sha)
def test_upload_info_from_bytes(self):
upload_info = UploadInfo.from_bytes(self.content)
self.assertEqual(upload_info.sample, self.sample)
self.assertEqual(upload_info.size, self.size)
self.assertEqual(upload_info.sha256, self.sha)
def test_upload_info_from_bytes_io(self):
upload_info = UploadInfo.from_fileobj(BytesIO(self.content))
self.assertEqual(upload_info.sample, self.sample)
self.assertEqual(upload_info.size, self.size)
self.assertEqual(upload_info.sha256, self.sha)
class TestSliceFileObj(unittest.TestCase):
def setUp(self) -> None:
self.content = b"RANDOM self.content uauabciabeubahveb" * 1024
def test_slice_fileobj_BytesIO(self):
fileobj = BytesIO(self.content)
prev_pos = fileobj.tell()
# Test read
with SliceFileObj(fileobj, seek_from=24, read_limit=18) as fileobj_slice:
self.assertEqual(fileobj_slice.tell(), 0)
self.assertEqual(fileobj_slice.read(), self.content[24:42])
self.assertEqual(fileobj_slice.tell(), 18)
self.assertEqual(fileobj_slice.read(), b"")
self.assertEqual(fileobj_slice.tell(), 18)
self.assertEqual(fileobj.tell(), prev_pos)
with SliceFileObj(fileobj, seek_from=0, read_limit=990) as fileobj_slice:
self.assertEqual(fileobj_slice.tell(), 0)
self.assertEqual(fileobj_slice.read(200), self.content[0:200])
self.assertEqual(fileobj_slice.read(500), self.content[200:700])
self.assertEqual(fileobj_slice.read(200), self.content[700:900])
self.assertEqual(fileobj_slice.read(200), self.content[900:990])
self.assertEqual(fileobj_slice.read(200), b"")
# Test seek with whence = os.SEEK_SET
with SliceFileObj(fileobj, seek_from=100, read_limit=100) as fileobj_slice:
self.assertEqual(fileobj_slice.tell(), 0)
fileobj_slice.seek(2, os.SEEK_SET)
self.assertEqual(fileobj_slice.tell(), 2)
self.assertEqual(fileobj_slice.fileobj.tell(), 102)
fileobj_slice.seek(-4, os.SEEK_SET)
self.assertEqual(fileobj_slice.tell(), 0)
self.assertEqual(fileobj_slice.fileobj.tell(), 100)
fileobj_slice.seek(100 + 4, os.SEEK_SET)
self.assertEqual(fileobj_slice.tell(), 100)
self.assertEqual(fileobj_slice.fileobj.tell(), 200)
# Test seek with whence = os.SEEK_CUR
with SliceFileObj(fileobj, seek_from=100, read_limit=100) as fileobj_slice:
self.assertEqual(fileobj_slice.tell(), 0)
fileobj_slice.seek(-5, os.SEEK_CUR)
self.assertEqual(fileobj_slice.tell(), 0)
self.assertEqual(fileobj_slice.fileobj.tell(), 100)
fileobj_slice.seek(50, os.SEEK_CUR)
self.assertEqual(fileobj_slice.tell(), 50)
self.assertEqual(fileobj_slice.fileobj.tell(), 150)
fileobj_slice.seek(100, os.SEEK_CUR)
self.assertEqual(fileobj_slice.tell(), 100)
self.assertEqual(fileobj_slice.fileobj.tell(), 200)
fileobj_slice.seek(-300, os.SEEK_CUR)
self.assertEqual(fileobj_slice.tell(), 0)
self.assertEqual(fileobj_slice.fileobj.tell(), 100)
# Test seek with whence = os.SEEK_END
with SliceFileObj(fileobj, seek_from=100, read_limit=100) as fileobj_slice:
self.assertEqual(fileobj_slice.tell(), 0)
fileobj_slice.seek(-5, os.SEEK_END)
self.assertEqual(fileobj_slice.tell(), 95)
self.assertEqual(fileobj_slice.fileobj.tell(), 195)
fileobj_slice.seek(50, os.SEEK_END)
self.assertEqual(fileobj_slice.tell(), 100)
self.assertEqual(fileobj_slice.fileobj.tell(), 200)
fileobj_slice.seek(-200, os.SEEK_END)
self.assertEqual(fileobj_slice.tell(), 0)
self.assertEqual(fileobj_slice.fileobj.tell(), 100)
def test_slice_fileobj_file(self):
self.content = b"RANDOM self.content uauabciabeubahveb" * 1024
with SoftTemporaryDirectory() as tmpdir:
filepath = os.path.join(tmpdir, "file.bin")
with open(filepath, "wb+") as f:
f.write(self.content)
with open(filepath, "rb") as fileobj:
prev_pos = fileobj.tell()
# Test read
with SliceFileObj(fileobj, seek_from=24, read_limit=18) as fileobj_slice:
self.assertEqual(fileobj_slice.tell(), 0)
self.assertEqual(fileobj_slice.read(), self.content[24:42])
self.assertEqual(fileobj_slice.tell(), 18)
self.assertEqual(fileobj_slice.read(), b"")
self.assertEqual(fileobj_slice.tell(), 18)
self.assertEqual(fileobj.tell(), prev_pos)
with SliceFileObj(fileobj, seek_from=0, read_limit=990) as fileobj_slice:
self.assertEqual(fileobj_slice.tell(), 0)
self.assertEqual(fileobj_slice.read(200), self.content[0:200])
self.assertEqual(fileobj_slice.read(500), self.content[200:700])
self.assertEqual(fileobj_slice.read(200), self.content[700:900])
self.assertEqual(fileobj_slice.read(200), self.content[900:990])
self.assertEqual(fileobj_slice.read(200), b"")
# Test seek with whence = os.SEEK_SET
with SliceFileObj(fileobj, seek_from=100, read_limit=100) as fileobj_slice:
self.assertEqual(fileobj_slice.tell(), 0)
fileobj_slice.seek(2, os.SEEK_SET)
self.assertEqual(fileobj_slice.tell(), 2)
self.assertEqual(fileobj_slice.fileobj.tell(), 102)
fileobj_slice.seek(-4, os.SEEK_SET)
self.assertEqual(fileobj_slice.tell(), 0)
self.assertEqual(fileobj_slice.fileobj.tell(), 100)
fileobj_slice.seek(100 + 4, os.SEEK_SET)
self.assertEqual(fileobj_slice.tell(), 100)
self.assertEqual(fileobj_slice.fileobj.tell(), 200)
# Test seek with whence = os.SEEK_CUR
with SliceFileObj(fileobj, seek_from=100, read_limit=100) as fileobj_slice:
self.assertEqual(fileobj_slice.tell(), 0)
fileobj_slice.seek(-5, os.SEEK_CUR)
self.assertEqual(fileobj_slice.tell(), 0)
self.assertEqual(fileobj_slice.fileobj.tell(), 100)
fileobj_slice.seek(50, os.SEEK_CUR)
self.assertEqual(fileobj_slice.tell(), 50)
self.assertEqual(fileobj_slice.fileobj.tell(), 150)
fileobj_slice.seek(100, os.SEEK_CUR)
self.assertEqual(fileobj_slice.tell(), 100)
self.assertEqual(fileobj_slice.fileobj.tell(), 200)
fileobj_slice.seek(-300, os.SEEK_CUR)
self.assertEqual(fileobj_slice.tell(), 0)
self.assertEqual(fileobj_slice.fileobj.tell(), 100)
# Test seek with whence = os.SEEK_END
with SliceFileObj(fileobj, seek_from=100, read_limit=100) as fileobj_slice:
self.assertEqual(fileobj_slice.tell(), 0)
fileobj_slice.seek(-5, os.SEEK_END)
self.assertEqual(fileobj_slice.tell(), 95)
self.assertEqual(fileobj_slice.fileobj.tell(), 195)
fileobj_slice.seek(50, os.SEEK_END)
self.assertEqual(fileobj_slice.tell(), 100)
self.assertEqual(fileobj_slice.fileobj.tell(), 200)
fileobj_slice.seek(-200, os.SEEK_END)
self.assertEqual(fileobj_slice.tell(), 0)
self.assertEqual(fileobj_slice.fileobj.tell(), 100)