3
3
from nose .tools import assert_true , assert_equal
4
4
from datajoint .external import ExternalTable
5
5
from datajoint .blob import pack , unpack
6
-
7
-
8
6
import datajoint as dj
9
7
from .schema_external import stores_config , SimpleRemote , Simple , schema
8
+ import os
10
9
11
- import json
12
- import os
13
- from os import stat
14
- import pwd
15
- from pwd import getpwuid
16
10
current_location_s3 = dj .config ['stores' ]['share' ]['location' ]
17
11
current_location_local = dj .config ['stores' ]['local' ]['location' ]
18
12
@@ -26,6 +20,27 @@ def tearDown(self):
26
20
dj .config ['stores' ]['local' ]['location' ] = current_location_local
27
21
28
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
+
29
44
30
45
def test_s3_leading_slash (index = 100 , store = 'share' ):
31
46
"""
@@ -86,95 +101,26 @@ def test_s3_leading_slash(index=100, store='share'):
86
101
87
102
dj .config ['stores' ][store ]['location' ] = oldConfig
88
103
104
+
89
105
def test_file_leading_slash ():
90
106
"""
91
107
file external storage configured with leading slash
92
108
"""
93
109
test_s3_leading_slash (index = 200 , store = 'local' )
94
110
95
- def test_remove_fail ():
96
- #https://github.com/datajoint/datajoint-python/issues/953
97
-
98
- #print(json.dumps(dj.config['stores'], indent=4))
99
- #print(dj.config['stores']['local']['location'])
100
111
101
- # oldConfig = dj.config['stores']
102
- # dj.config['stores'] = stores_config
103
-
104
- dirName = dj .config ['stores' ]['local' ]['location' ]
105
-
106
- #print('directory: ' + dirName)
107
-
108
-
109
-
110
- data = dict (simple = 2 , item = [1 , 2 , 3 ])
112
+ def test_remove_fail ():
113
+ # https://github.com/datajoint/datajoint-python/issues/953
114
+ data = dict (simple = 2 , item = [1 , 2 , 3 ])
111
115
Simple .insert1 (data )
112
-
113
- #print('location')
114
- # print('\n IN TEST: BEFORE DELETE: list of dir stores, local, location')
115
- print ('stores location -----------\n ' )
116
- print (dj .config ['stores' ]['local' ]['location' ])
117
- print ('local location -----------\n ' )
118
- print (schema .external ['local' ])
119
- print ('----------------------------' )
120
-
121
116
path1 = dj .config ['stores' ]['local' ]['location' ] + '/djtest_extern/4/c/'
122
-
123
- argDir = dj .config ['stores' ]['local' ]['location' ] + '/djtest_extern/4/c/'
124
-
125
- print (f'argDir----------\n { argDir } \n ' )
126
-
127
- path2 = os .listdir (argDir )
128
-
129
- # print(path1 + path2[0])
130
-
131
- old_name = path1 + path2 [0 ]
132
-
133
- new_name = "/tmp/newfile"
134
-
135
- os .rename (old_name , new_name )
136
-
137
- # print(f'\n IN TEST: is the new file name a file? {os.path.isfile(new_name)}')
138
- # print(f'\n IN TEST: is the old file name a file? {os.path.isfile(old_name)}')
139
-
140
- # print(os.listdir(dj.config['stores']['local']['location'] + '/djtest_extern/4/c/'))
141
-
142
- # st = stat(path1 + path2[0])
143
- # print(bool(st.st_mode & stat.S_IXUSR))
144
-
145
- #print(getpwuid(stat(path3).st_uid).pw_name)
146
-
147
- # print(f' IN TEST: simple table before delete {Simple()}')
117
+ currentMode = int (oct (os .stat (path1 ).st_mode ), 8 )
118
+ os .chmod (path1 , 0o40555 )
148
119
(Simple & 'simple=2' ).delete ()
149
- # print(f' IN TEST: simple table after delete {Simple()}')
150
- # print(' IN TEST: -------------showing external store before delete with flag---------')
151
- # print(schema.external['local'])
152
120
listOfErrors = schema .external ['local' ].delete (delete_external_files = True )
153
- # print(f' IN TEST: list of errors: {listOfErrors}')
154
- # print(' IN TEST: list of dir stores, local, location')
155
- # print(os.listdir(dj.config['stores']['local']['location'] + '/djtest_extern/4/c'))
156
- # print(' IN TEST: -------------showing external store after delete with flag---------')
157
- # print(schema.external['local'])
158
-
159
- # print(f'\n IN TEST: is this the UID or HASH? {listOfErrors[0][0]}')
160
-
161
- # LENGTH_OF_QUERY = len(schema.external['local'] & dict(hash = listOfErrors[0][0]))
162
-
163
- # print(f'\n IN TEST: WHAT IS THE LENGTH OF THIS? {LENGTH_OF_QUERY}')
164
-
165
121
assert len (listOfErrors ) == 1 , 'unexpected number of errors'
166
- assert len (schema .external ['local' ] & dict (hash = listOfErrors [0 ][0 ])) == 1 , 'unexpected number of rows in external table'
167
-
168
- #---------------------CLEAN UP--------------------
169
- os .rename (new_name , old_name ) #switching from the new name back to the old name
170
-
171
- # print(f'this is the old_name after the asserts {old_name}')
172
-
173
- # print(f'\n IN TEST: is the new file name a file? {os.path.isfile(new_name)}')
174
- # print(f'\n IN TEST: is the old file name a file? {os.path.isfile(old_name)}')
175
-
176
- listOfErrors = schema .external ['local' ].delete (delete_external_files = True )
177
-
178
- print (len (listOfErrors ))
179
-
180
- # dj.config['stores'] = oldConfig
122
+ assert len (schema .external ['local' ] & dict (hash = listOfErrors [0 ][0 ])) == 1 , 'unexpec' + \
123
+ 'number of rows in external table'
124
+ # ---------------------CLEAN UP--------------------
125
+ os .chmod (path1 , currentMode )
126
+ listOfErrors = schema .external ['local' ].delete (delete_external_files = True )
0 commit comments