Skip to content

Commit 26f6265

Browse files
fix #876
1 parent 5469c5c commit 26f6265

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

datajoint/fetch.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ def __call__(self, *attrs, offset=None, limit=None, order_by=None, format=None,
207207
except Exception as e:
208208
raise e
209209
for name in heading:
210+
# unpack blobs and externals
210211
ret[name] = list(map(partial(get, heading[name]), ret[name]))
211212
if format == "frame":
212213
ret = pandas.DataFrame(ret).set_index(heading.primary_key)
@@ -251,7 +252,7 @@ def __call__(self, *attrs, squeeze=False, download_path='.'):
251252
else: # fetch some attributes, return as tuple
252253
attributes = [a for a in attrs if not is_key(a)]
253254
result = self._expression.proj(*attributes).fetch(
254-
squeeze=squeeze, download_path=download_path)
255+
squeeze=squeeze, download_path=download_path, format="array")
255256
if len(result) != 1:
256257
raise DataJointError(
257258
'fetch1 should only return one tuple. %d tuples found' % len(result))

tests/test_fetch.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def test_limit_warning(self):
187187

188188
def test_len(self):
189189
"""Tests __len__"""
190-
assert_true(len(self.lang.fetch()) == len(self.lang), '__len__ is not behaving properly')
190+
assert_equal(len(self.lang.fetch()), len(self.lang), '__len__ is not behaving properly')
191191

192192
@raises(dj.DataJointError)
193193
def test_fetch1_step2(self):
@@ -222,31 +222,33 @@ def test_nullable_numbers(self):
222222

223223
def test_fetch_format(self):
224224
"""test fetch_format='frame'"""
225-
dj.config['fetch_format'] = 'frame'
226-
# test if lists are both dicts
227-
list1 = sorted(self.subject.proj().fetch(as_dict=True), key=itemgetter('subject_id'))
228-
list2 = sorted(self.subject.fetch(dj.key), key=itemgetter('subject_id'))
229-
for l1, l2 in zip(list1, list2):
230-
assert_dict_equal(l1, l2, 'Primary key is not returned correctly')
231-
232-
# tests if pandas dataframe
233-
tmp = self.subject.fetch(order_by='subject_id')
234-
assert_true(isinstance(tmp, pandas.DataFrame))
235-
tmp = tmp.to_records()
236-
237-
subject_notes, key, real_id = self.subject.fetch('subject_notes', dj.key, 'real_id')
238-
239-
np.testing.assert_array_equal(sorted(subject_notes), sorted(tmp['subject_notes']))
240-
np.testing.assert_array_equal(sorted(real_id), sorted(tmp['real_id']))
241-
list1 = sorted(key, key=itemgetter('subject_id'))
242-
for l1, l2 in zip(list1, list2):
243-
assert_dict_equal(l1, l2, 'Primary key is not returned correctly')
244-
245-
# test pandas with fetch1
246-
# k = (self.subject & 'subject_id=10').fetch1('KEY')
247-
248-
# revert configuration of fetch format
249-
dj.config['fetch_format'] = 'array'
225+
with dj.config(fetch_format='frame'):
226+
# test if lists are both dicts
227+
list1 = sorted(self.subject.proj().fetch(as_dict=True), key=itemgetter('subject_id'))
228+
list2 = sorted(self.subject.fetch(dj.key), key=itemgetter('subject_id'))
229+
for l1, l2 in zip(list1, list2):
230+
assert_dict_equal(l1, l2, 'Primary key is not returned correctly')
231+
232+
# tests if pandas dataframe
233+
tmp = self.subject.fetch(order_by='subject_id')
234+
assert_true(isinstance(tmp, pandas.DataFrame))
235+
tmp = tmp.to_records()
236+
237+
subject_notes, key, real_id = self.subject.fetch('subject_notes', dj.key, 'real_id')
238+
239+
np.testing.assert_array_equal(sorted(subject_notes), sorted(tmp['subject_notes']))
240+
np.testing.assert_array_equal(sorted(real_id), sorted(tmp['real_id']))
241+
list1 = sorted(key, key=itemgetter('subject_id'))
242+
for l1, l2 in zip(list1, list2):
243+
assert_dict_equal(l1, l2, 'Primary key is not returned correctly')
244+
245+
def test_key_fetch1(self):
246+
"""test KEY fetch1 - issue #976"""
247+
with dj.config(fetch_format="array"):
248+
k1 = (self.subject & 'subject_id=10').fetch1('KEY')
249+
with dj.config(fetch_format="frame"):
250+
k2 = (self.subject & 'subject_id=10').fetch1('KEY')
251+
assert_equal(k1, k2)
250252

251253
def test_same_secondary_attribute(self):
252254
print(schema.Child * schema.Parent().proj())

0 commit comments

Comments
 (0)