Skip to content

Commit 6046b81

Browse files
committed
cp to tests
1 parent 10185a2 commit 6046b81

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

tests/test_external.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import numpy as np
2+
from numpy.testing import assert_array_equal
3+
from nose.tools import assert_true, assert_equal
4+
from datajoint.external import ExternalTable
5+
from datajoint.blob import pack, unpack
6+
import datajoint as dj
7+
from .schema_external import stores_config, SimpleRemote, Simple, schema
8+
import os
9+
10+
current_location_s3 = dj.config["stores"]["share"]["location"]
11+
current_location_local = dj.config["stores"]["local"]["location"]
12+
13+
14+
def setUp(self):
15+
dj.config["stores"] = stores_config
16+
17+
18+
def tearDown(self):
19+
dj.config["stores"]["share"]["location"] = current_location_s3
20+
dj.config["stores"]["local"]["location"] = current_location_local
21+
22+
23+
def test_external_put():
24+
"""
25+
external storage put and get and remove
26+
"""
27+
ext = ExternalTable(schema.connection, store="raw", database=schema.database)
28+
initial_length = len(ext)
29+
input_ = np.random.randn(3, 7, 8)
30+
count = 7
31+
extra = 3
32+
for i in range(count):
33+
hash1 = ext.put(pack(input_))
34+
for i in range(extra):
35+
hash2 = ext.put(pack(np.random.randn(4, 3, 2)))
36+
37+
fetched_hashes = ext.fetch("hash")
38+
assert_true(all(hash in fetched_hashes for hash in (hash1, hash2)))
39+
assert_equal(len(ext), initial_length + 1 + extra)
40+
41+
output_ = unpack(ext.get(hash1))
42+
assert_array_equal(input_, output_)
43+
44+
45+
def test_s3_leading_slash(index=100, store="share"):
46+
"""
47+
s3 external storage configured with leading slash
48+
"""
49+
50+
oldConfig = dj.config["stores"][store]["location"]
51+
52+
value = np.array([1, 2, 3])
53+
54+
id = index
55+
dj.config["stores"][store]["location"] = "leading/slash/test"
56+
SimpleRemote.insert([{"simple": id, "item": value}])
57+
assert_true(
58+
np.array_equal(value, (SimpleRemote & "simple={}".format(id)).fetch1("item"))
59+
)
60+
61+
id = index + 1
62+
dj.config["stores"][store]["location"] = "/leading/slash/test"
63+
SimpleRemote.insert([{"simple": id, "item": value}])
64+
assert_true(
65+
np.array_equal(value, (SimpleRemote & "simple={}".format(id)).fetch1("item"))
66+
)
67+
68+
id = index + 2
69+
dj.config["stores"][store]["location"] = "leading\\slash\\test"
70+
SimpleRemote.insert([{"simple": id, "item": value}])
71+
assert_true(
72+
np.array_equal(value, (SimpleRemote & "simple={}".format(id)).fetch1("item"))
73+
)
74+
75+
id = index + 3
76+
dj.config["stores"][store]["location"] = "f:\\leading\\slash\\test"
77+
SimpleRemote.insert([{"simple": id, "item": value}])
78+
assert_true(
79+
np.array_equal(value, (SimpleRemote & "simple={}".format(id)).fetch1("item"))
80+
)
81+
82+
id = index + 4
83+
dj.config["stores"][store]["location"] = "f:\\leading/slash\\test"
84+
SimpleRemote.insert([{"simple": id, "item": value}])
85+
assert_true(
86+
np.array_equal(value, (SimpleRemote & "simple={}".format(id)).fetch1("item"))
87+
)
88+
89+
id = index + 5
90+
dj.config["stores"][store]["location"] = "/"
91+
SimpleRemote.insert([{"simple": id, "item": value}])
92+
assert_true(
93+
np.array_equal(value, (SimpleRemote & "simple={}".format(id)).fetch1("item"))
94+
)
95+
96+
id = index + 6
97+
dj.config["stores"][store]["location"] = "C:\\"
98+
SimpleRemote.insert([{"simple": id, "item": value}])
99+
assert_true(
100+
np.array_equal(value, (SimpleRemote & "simple={}".format(id)).fetch1("item"))
101+
)
102+
103+
id = index + 7
104+
dj.config["stores"][store]["location"] = ""
105+
SimpleRemote.insert([{"simple": id, "item": value}])
106+
assert_true(
107+
np.array_equal(value, (SimpleRemote & "simple={}".format(id)).fetch1("item"))
108+
)
109+
110+
dj.config["stores"][store]["location"] = oldConfig
111+
112+
113+
def test_file_leading_slash():
114+
"""
115+
file external storage configured with leading slash
116+
"""
117+
test_s3_leading_slash(index=200, store="local")
118+
119+
120+
def test_remove_fail():
121+
# https://github.com/datajoint/datajoint-python/issues/953
122+
data = dict(simple=2, item=[1, 2, 3])
123+
Simple.insert1(data)
124+
path1 = dj.config["stores"]["local"]["location"] + "/djtest_extern/4/c/"
125+
currentMode = int(oct(os.stat(path1).st_mode), 8)
126+
os.chmod(path1, 0o40555)
127+
(Simple & "simple=2").delete()
128+
listOfErrors = schema.external["local"].delete(delete_external_files=True)
129+
assert len(listOfErrors) == 1, "unexpected number of errors"
130+
assert (
131+
len(schema.external["local"] & dict(hash=listOfErrors[0][0])) == 1
132+
), "unexpected number of rows in external table"
133+
# ---------------------CLEAN UP--------------------
134+
os.chmod(path1, currentMode)
135+
listOfErrors = schema.external["local"].delete(delete_external_files=True)

0 commit comments

Comments
 (0)