Skip to content

Commit

Permalink
Add ResourceList to fix API list access (#40)
Browse files Browse the repository at this point in the history
* Explicitly disallow unsupported comparisons between API models
* Added tests for ResourceLists, and comparison between models
  • Loading branch information
polyatail committed Nov 16, 2018
1 parent a4b9eb4 commit 66fd248
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 1 deletion.
97 changes: 96 additions & 1 deletion onecodex/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,89 @@
DEFAULT_PAGE_SIZE = 200


class ResourceList:
"""
In OneCodexBase, when attributes are lists, actions performed on the returned lists are not
passed through to the underlying resource list. This class passes those actions through, and
will generally act like a list.
"""

def _update(self):
self._res_list = [self.oc_model(x) for x in self._resource]

def __init__(self, _resource, oc_model):
# turn potion Resource objects into OneCodex objects
self._resource = _resource
self.oc_model = oc_model
self._update()

def __lt__(self, other):
raise NotImplementedError

def __le__(self, other):
raise NotImplementedError

def __gt__(self, other):
raise NotImplementedError

def __ge__(self, other):
raise NotImplementedError

def __eq__(self, other):
# two ResourceLists are equal if they refer to the same underlying Resource
return id(self._resource) == id(other._resource)

def __repr__(self):
self._update()
return self._res_list.__repr__()

def __len__(self):
self._update()
return len(self._res_list)

def __getitem__(self, x):
self._update()
return self._res_list[x]

def __setitem__(self, k, v):
if isinstance(v, self.oc_model) and \
isinstance(v._resource, Resource):
self._resource[k] = v._resource
else:
raise

def __delitem__(self, x):
del self._resource[x]

def __iter__(self):
self._update()
return self._res_list.__iter__()

def __reversed__(self):
self._update()
return reversed(self._res_list)

def append(self, x):
if isinstance(x, self.oc_model) and \
isinstance(x._resource, Resource):
self._resource.append(x._resource)
else:
raise

def extend(self, iterable):
for x in iterable:
self.append(x)

def pop(self):
self._update()
self._resource.pop()
return self._res_list.pop()

def clear(self):
self._resource.clear()
self._res_list.clear()


class OneCodexBase(object):
"""
A parent object for all the One Codex objects that wraps the Potion-Client API and makes
Expand All @@ -40,6 +123,18 @@ def __init__(self, _resource=None, **kwargs):
kwargs[key] = val._resource
self._resource = self.__class__._resource(**kwargs)

def __lt__(self, other):
raise NotImplementedError

def __le__(self, other):
raise NotImplementedError

def __gt__(self, other):
raise NotImplementedError

def __ge__(self, other):
raise NotImplementedError

def __repr__(self):
return '<{} {}>'.format(self.__class__.__name__, self.id)

Expand Down Expand Up @@ -76,7 +171,7 @@ def __getattr__(self, key):
elif isinstance(value, list) and len(value) > 0 and isinstance(value[0], Resource):
# convert lists of potion resources into wrapped ones
resource_path = value[0]._uri.rsplit('/', 1)[0]
return [_model_lookup[resource_path](_resource=o) for o in value]
return ResourceList(value, _model_lookup[resource_path])
else:
if key == 'id':
# undo the bad coercion from potion_client/resource.py#L111
Expand Down
64 changes: 64 additions & 0 deletions tests/test_api_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,70 @@ def test_sample_get(ocx, api_data):
assert 'isolate' in [t.name for t in tags]


def test_resourcelist(ocx, api_data):
sample = ocx.Samples.get('761bc54b97f64980')
tags1 = onecodex.models.ResourceList(sample.tags._resource,
onecodex.models.misc.Tags)
tags2 = onecodex.models.ResourceList(sample.tags._resource,
onecodex.models.misc.Tags)

assert isinstance(sample.tags, onecodex.models.ResourceList)

# test manipulation of tags lists
tag_to_pop = sample.tags[-1]
popped_tag = sample.tags.pop()
assert id(tag_to_pop._resource) == id(popped_tag._resource)

# we can set tags list in-place
sample.tags[0] = popped_tag
assert id(sample.tags[0]._resource) == id(popped_tag._resource)

# changes in one instance of a ResourceList affect other instances
assert id(tags1) != id(tags2) != id(sample.tags)
assert id(tags1._resource) == id(tags2._resource) == id(sample.tags._resource)

assert len(tags1) == len(tags2) == len(sample.tags)

for i in range(len(tags1)):
assert tags1[i] == tags2[i] == sample.tags[i]


def test_comparisons(ocx, api_data):
sample1 = ocx.Samples.get('761bc54b97f64980')
sample2 = ocx.Samples.get('761bc54b97f64980')

# should be different objects
assert id(sample1) != id(sample2)

# but they should reference the same Resource
assert id(sample1._resource) == id(sample2._resource)

# and so they should be equal
assert sample1 == sample2

# but any non-equivalence comparisons should throw errors
with pytest.raises(NotImplementedError):
sample1 > sample2
with pytest.raises(NotImplementedError):
sample1 >= sample2
with pytest.raises(NotImplementedError):
sample1 < sample2
with pytest.raises(NotImplementedError):
sample1 <= sample2

# and this should be true of ResourceLists too
assert sample1.tags == sample2.tags

with pytest.raises(NotImplementedError):
sample1.tags > sample2.tags
with pytest.raises(NotImplementedError):
sample1.tags >= sample2.tags
with pytest.raises(NotImplementedError):
sample1.tags < sample2.tags
with pytest.raises(NotImplementedError):
sample1.tags <= sample2.tags


def test_dir_method(ocx, api_data):
sample = ocx.Samples.get('761bc54b97f64980')

Expand Down

0 comments on commit 66fd248

Please sign in to comment.