Skip to content

Commit 098aa90

Browse files
committed
Add tests and fixtures for build zip tree with sorted file list
In addition, remove left-over debug statements
1 parent 41fc95a commit 098aa90

File tree

3 files changed

+184
-8
lines changed

3 files changed

+184
-8
lines changed

mfr/extensions/zip/render.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ def sorted_obj_list_to_tree(self, sorted_obj_list: list) -> List[dict]:
5252
# Iterate through each path and build the tree
5353
for obj in sorted_obj_list:
5454
path_from_root = obj.filename
55-
print(path_from_root)
5655
path_segments = [segment for segment in path_from_root.split('/') if segment]
5756
# Find the parent node of the current object, always start from the root node
5857
parent = tree_root

tests/extensions/zip/fixtures/obj_tree.json

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,156 @@
149149
]
150150
}
151151
],
152+
"test_file_tree_sorted": [
153+
{
154+
"icon": "http://mfr.osf.io/assets/zip/img/file-ext-zip.png",
155+
"text": "test.zip",
156+
"children": [
157+
{
158+
"icon": "http://mfr.osf.io/assets/zip/img/folder.png",
159+
"text": "zip-test",
160+
"data": {
161+
"size": "",
162+
"date": "2018-04-16 11:52:50"
163+
},
164+
"children": [
165+
{
166+
"icon": "http://mfr.osf.io/assets/zip/img/file-ext-generic.png",
167+
"text": "file-generic-ext.mfr",
168+
"data": {
169+
"size": " 29B",
170+
"date": "2018-03-27 15:42:32"
171+
},
172+
"children": []
173+
},
174+
{
175+
"icon": "http://mfr.osf.io/assets/zip/img/file-ext-generic.png",
176+
"text": "file-no-ext",
177+
"data": {
178+
"size": " 19B",
179+
"date": "2018-03-27 15:42:32"
180+
},
181+
"children": []
182+
},
183+
{
184+
"icon": "http://mfr.osf.io/assets/zip/img/folder.png",
185+
"text": "folder-1",
186+
"data": {
187+
"size": "",
188+
"date": "2018-04-16 11:50:48"
189+
},
190+
"children": [
191+
{
192+
"icon": "http://mfr.osf.io/assets/zip/img/file-ext-png.png",
193+
"text": "image-1.png",
194+
"data": {
195+
"size": " 19B",
196+
"date": "2018-03-27 15:42:32"
197+
},
198+
"children": []
199+
},
200+
{
201+
"icon": "http://mfr.osf.io/assets/zip/img/file-ext-txt.png",
202+
"text": "text-1.txt",
203+
"data": {
204+
"size": " 35B",
205+
"date": "2018-03-27 15:42:32"
206+
},
207+
"children": []
208+
}
209+
]
210+
},
211+
{
212+
"icon": "http://mfr.osf.io/assets/zip/img/folder.png",
213+
"text": "folder-2",
214+
"data": {
215+
"size": "",
216+
"date": "2018-04-16 11:56:50"
217+
},
218+
"children": [
219+
{
220+
"icon": "http://mfr.osf.io/assets/zip/img/folder.png",
221+
"text": "folder-2-1",
222+
"data": {
223+
"size": "",
224+
"date": "2018-04-16 11:56:56"
225+
},
226+
"children": [
227+
{
228+
"icon": "http://mfr.osf.io/assets/zip/img/file-ext-jpg.png",
229+
"text": "image-2.jpg",
230+
"data": {
231+
"size": " 26B",
232+
"date": "2018-03-27 15:42:32"
233+
},
234+
"children": []
235+
}
236+
]
237+
},
238+
{
239+
"icon": "http://mfr.osf.io/assets/zip/img/file-ext-pdf.png",
240+
"text": "text-2.pdf",
241+
"data": {
242+
"size": " 37B",
243+
"date": "2018-03-27 15:42:32"
244+
},
245+
"children": []
246+
}
247+
]
248+
},
249+
{
250+
"icon": "http://mfr.osf.io/assets/zip/img/folder.png",
251+
"text": "folder-3",
252+
"data": {
253+
"size": "",
254+
"date": "2018-04-16 17:07:48"
255+
},
256+
"children": [
257+
{
258+
"icon": "http://mfr.osf.io/assets/zip/img/folder.png",
259+
"text": "folder-3-1",
260+
"data": {
261+
"size": "",
262+
"date": "2018-04-16 11:56:56"
263+
},
264+
"children": [
265+
{
266+
"icon": "http://mfr.osf.io/assets/zip/img/folder.png",
267+
"text": "folder-3-1-1",
268+
"data": {
269+
"size": "",
270+
"date": "2018-04-16 11:57:12"
271+
},
272+
"children": [
273+
{
274+
"icon": "http://mfr.osf.io/assets/zip/img/file-ext-bmp.png",
275+
"text": "image-3.bmp",
276+
"data": {
277+
"size": " 17B",
278+
"date": "2018-03-27 15:42:32"
279+
},
280+
"children": []
281+
}
282+
]
283+
},
284+
{
285+
"icon": "http://mfr.osf.io/assets/zip/img/file-ext-docx.png",
286+
"text": "text-3.docx",
287+
"data": {
288+
"size": " 19B",
289+
"date": "2018-03-27 15:42:32"
290+
},
291+
"children": []
292+
}
293+
]
294+
}
295+
]
296+
}
297+
]
298+
}
299+
]
300+
}
301+
],
152302
"empty_file_tree": [
153303
{
154304
"text": "test.zip",

tests/extensions/zip/test_renderer.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ def empty_file_obj_name_list():
4444

4545
@pytest.fixture
4646
def test_file_obj_list(test_file):
47-
return ZipRenderer.sanitize_obj_list(test_file.filelist)
47+
return ZipRenderer.sanitize_obj_list(test_file.filelist, sort=False)
48+
49+
50+
@pytest.fixture
51+
def test_file_sorted_obj_list(test_file):
52+
return ZipRenderer.sanitize_obj_list(test_file.filelist, sort=True)
4853

4954

5055
@pytest.fixture
@@ -53,9 +58,15 @@ def test_file_obj_tree():
5358
return json.load(fp)['test_file_tree']
5459

5560

61+
@pytest.fixture
62+
def test_file_obj_tree_sorted():
63+
with open(os.path.join(os.path.dirname(__file__), 'fixtures/obj_tree.json'), 'r') as fp:
64+
return json.load(fp)['test_file_tree_sorted']
65+
66+
5667
@pytest.fixture
5768
def empty_file_obj_list(empty_file):
58-
return ZipRenderer.sanitize_obj_list(empty_file.filelist)
69+
return ZipRenderer.sanitize_obj_list(empty_file.filelist, sort=False)
5970

6071

6172
@pytest.fixture
@@ -160,6 +171,11 @@ def test_sanitize_obj_list(self, test_file, test_file_obj_name_list):
160171
obj_name_list = [obj.filename for obj in obj_list if obj]
161172
assert sorted(obj_name_list) == sorted(test_file_obj_name_list)
162173

174+
def test_sanitize_and_sort_obj_list(self, test_file, test_file_obj_name_list):
175+
sorted_obj_list = ZipRenderer.sanitize_obj_list(test_file.filelist, sort=True)
176+
sorted_obj_name_list = [obj.filename for obj in sorted_obj_list if obj]
177+
assert sorted_obj_name_list == sorted(test_file_obj_name_list)
178+
163179
def test_sanitize_obj_list_empty(self, empty_file, empty_file_obj_name_list):
164180
obj_list = ZipRenderer.sanitize_obj_list(empty_file.filelist)
165181
obj_name_list = [obj.filename for obj in obj_list if obj]
@@ -196,11 +212,22 @@ def test_update_node_with_attributes(self, test_file_renderer, obj_zip_info_to_u
196212
assert node_to_update.get('data', {}) == existing_folder_to_update['data']
197213
assert node_to_update.get('icon', {}) == existing_folder_to_update['icon']
198214

199-
def test_obj_list_to_tree(self, test_file_obj_list, test_file_renderer, test_file_obj_tree):
200-
obj_tree = test_file_renderer.obj_list_to_tree(test_file_obj_list)
215+
def test_unsorted_obj_list_to_tree(self, test_file_obj_list, test_file_renderer,
216+
test_file_obj_tree):
217+
obj_tree = test_file_renderer.unsorted_obj_list_to_tree(test_file_obj_list)
201218
assert obj_tree == test_file_obj_tree
202219

203-
def test_obj_list_to_tree_empty(self, empty_file_obj_list, empty_file_renderer,
204-
empty_file_obj_tree):
205-
obj_tree = empty_file_renderer.obj_list_to_tree(empty_file_obj_list)
220+
def test_sorted_obj_list_to_tree(self, test_file_sorted_obj_list, test_file_renderer,
221+
test_file_obj_tree_sorted):
222+
obj_tree = test_file_renderer.sorted_obj_list_to_tree(test_file_sorted_obj_list)
223+
assert obj_tree == test_file_obj_tree_sorted
224+
225+
def test_unsorted_obj_list_to_tree_empty(self, empty_file_obj_list, empty_file_renderer,
226+
empty_file_obj_tree):
227+
obj_tree = empty_file_renderer.unsorted_obj_list_to_tree(empty_file_obj_list)
228+
assert obj_tree == empty_file_obj_tree
229+
230+
def test_sorted_obj_list_to_tree_empty(self, empty_file_obj_list, empty_file_renderer,
231+
empty_file_obj_tree):
232+
obj_tree = empty_file_renderer.sorted_obj_list_to_tree(empty_file_obj_list)
206233
assert obj_tree == empty_file_obj_tree

0 commit comments

Comments
 (0)