Skip to content

Commit 5ae0cb1

Browse files
committed
Add DataDict to Mults
1 parent deb2685 commit 5ae0cb1

2 files changed

Lines changed: 12 additions & 46 deletions

File tree

opus/mults.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
11
# -*- coding: utf-8 -*-
22

3-
class Mults(object):
3+
from .data import DataDict
4+
5+
class Mults(DataDict):
46
def __init__(self, json):
7+
DataDict.__init__(self)
58
self._field = json['field']
6-
self._data = json['mults']
9+
self.load(json['mults'])
710

811
def __repr__(self):
9-
return 'OPUS API Mults for field: {}'.format(self._field)
10-
11-
def __getitem__(self, attr):
12-
return self._data[attr]
13-
14-
def __len__(self):
15-
return len(self._data)
16-
17-
def __iter__(self):
18-
return iter(self._data)
19-
20-
def keys(self):
21-
return self._data.keys()
22-
23-
def items(self):
24-
return self._data.items()
25-
26-
def values(self):
27-
return self._data.values()
12+
return 'OPUS API Multiple choice for field: `{}`\n'.format(self._field) + \
13+
'\n'.join(' - {} : {}'.format(key, value) for key, value in self.items())

tests/test_mults.py

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,8 @@ def mults():
1111
json = JSON.loads(open('tests/api/meta/mults/target.json', 'r').read())
1212
return Mults(json)
1313

14-
def test_mults_meta(mults):
15-
assert repr(mults) == 'OPUS API Mults for field: target'
16-
17-
def test_mults_getitem(mults):
18-
assert mults['RHEA'] == 20974
19-
20-
def test_mults_iter(mults):
21-
for key in mults:
22-
assert isinstance(key, six.text_type)
23-
24-
def test_mults_keys(mults):
25-
for key in mults.keys():
26-
assert isinstance(key, six.text_type)
27-
28-
def test_mults_items(mults):
29-
for key, value in mults.items():
30-
assert mults[key] == value
31-
32-
def test_mults_values(mults):
33-
for value in mults.values():
34-
assert isinstance(value, int)
35-
36-
def test_mults_getitem_err(mults):
37-
with pytest.raises(KeyError):
38-
mults['ABC']
14+
def test_mults(mults):
15+
r = repr(mults)
16+
assert 'OPUS API Multiple choice for field: `target`' in r
17+
assert 'TITAN' in r
18+
assert len(mults) == 63

0 commit comments

Comments
 (0)