Skip to content

Commit cecd022

Browse files
authored
Merge pull request #1119 from ethho/dev-tests-plat-148
PLAT-148: Migrate test_attach.py
2 parents 453e516 + bad3e9e commit cecd022

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

tests/conftest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import tempfile
1515
from datajoint import errors
1616
from datajoint.errors import ADAPTED_TYPE_SWITCH, FILEPATH_FEATURE_SWITCH
17-
from datajoint.errors import ADAPTED_TYPE_SWITCH, FILEPATH_FEATURE_SWITCH
1817
from . import (
1918
PREFIX,
2019
CONN_INFO,

tests/test_attach.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import pytest
2+
import tempfile
3+
from pathlib import Path
4+
import os
5+
from .schema_external import Attach
6+
7+
8+
def test_attach_attributes(schema_ext, minio_client):
9+
"""Test saving files in attachments"""
10+
# create a mock file
11+
table = Attach()
12+
source_folder = tempfile.mkdtemp()
13+
for i in range(2):
14+
attach1 = Path(source_folder, "attach1.img")
15+
data1 = os.urandom(100)
16+
with attach1.open("wb") as f:
17+
f.write(data1)
18+
attach2 = Path(source_folder, "attach2.txt")
19+
data2 = os.urandom(200)
20+
with attach2.open("wb") as f:
21+
f.write(data2)
22+
table.insert1(dict(attach=i, img=attach1, txt=attach2))
23+
24+
download_folder = Path(tempfile.mkdtemp())
25+
keys, path1, path2 = table.fetch(
26+
"KEY", "img", "txt", download_path=download_folder, order_by="KEY"
27+
)
28+
29+
# verify that different attachment are renamed if their filenames collide
30+
assert path1[0] != path2[0]
31+
assert path1[0] != path1[1]
32+
assert Path(path1[0]).parent == download_folder
33+
with Path(path1[-1]).open("rb") as f:
34+
check1 = f.read()
35+
with Path(path2[-1]).open("rb") as f:
36+
check2 = f.read()
37+
assert data1 == check1
38+
assert data2 == check2
39+
40+
# verify that existing files are not duplicated if their filename matches issue #592
41+
p1, p2 = (Attach & keys[0]).fetch1("img", "txt", download_path=download_folder)
42+
assert p1 == path1[0]
43+
assert p2 == path2[0]
44+
45+
46+
def test_return_string(schema_ext, minio_client):
47+
"""Test returning string on fetch"""
48+
# create a mock file
49+
table = Attach()
50+
source_folder = tempfile.mkdtemp()
51+
52+
attach1 = Path(source_folder, "attach1.img")
53+
data1 = os.urandom(100)
54+
with attach1.open("wb") as f:
55+
f.write(data1)
56+
attach2 = Path(source_folder, "attach2.txt")
57+
data2 = os.urandom(200)
58+
with attach2.open("wb") as f:
59+
f.write(data2)
60+
table.insert1(dict(attach=2, img=attach1, txt=attach2))
61+
62+
download_folder = Path(tempfile.mkdtemp())
63+
keys, path1, path2 = table.fetch(
64+
"KEY", "img", "txt", download_path=download_folder, order_by="KEY"
65+
)
66+
67+
assert isinstance(path1[0], str)

0 commit comments

Comments
 (0)