def test_collection(self): resp = { 'items': [{ 'id': 'abc123' }, { 'id': 'xyz456' }], 'meta': { 'limit': 0, 'offset': 0, 'total_count': 2, 'future_attr': 'foo' } } o = convert_to_analyzere_object(resp, Resource) assert len(o) == 2 assert o.meta.limit == 0 assert o.meta.offset == 0 assert o.meta.total_count == 2 assert o.meta.future_attr == 'foo' assert type(o[0]) == Resource # TODO: When no class is provided, the items should probably be # AnalyzeReObjects o = convert_to_analyzere_object(resp) assert type(o[0]) == EmbeddedResource
def test_nested_resource_does_not_require_id(self): class SomeEmbeddedResource(NestedResource): pass response_dict = {} o = convert_to_analyzere_object(response_dict, SomeEmbeddedResource) assert type(o) == SomeEmbeddedResource
def test_simple(self): d = datetime.today() resp = { 'num': 1, 'str': 'foo', 'date': d, 'list': [1, 2, 3], 'dict': { 'foo': 'bar' }, 'ref': { 'href': 'https://api/layers/abc123' }, } o = convert_to_analyzere_object(resp) assert o.num == 1 assert o.str == 'foo' assert o.date == d assert o.list == [1, 2, 3] assert o.dict == EmbeddedResource(foo='bar') assert type(o.dict) == EmbeddedResource # Shallow comparison for reference so it doesn't evaluate assert type(o.ref) == Reference assert o.ref._id == 'abc123'
def test_nested(self): d = datetime.today() resp = { 'num': { 'num': 1 }, 'str': { 'str': 'foo' }, 'date': { 'date': d }, 'list': { 'list': [1, 2, 3] }, 'dict': { 'dict': { 'foo': 'bar' } }, 'ref': { 'ref': { 'href': 'https://api/layers/abc123' } } } o = convert_to_analyzere_object(resp) assert o.num.num == 1 assert o.str.str == 'foo' assert o.date.date == d assert o.list.list == [1, 2, 3] assert o.dict.dict == EmbeddedResource(foo='bar') assert type(o.ref.ref) == Reference assert o.ref.ref._id == 'abc123'
def initial_metrics(self): """ The name of this method is chosen to avoid overlap with the initial_portfolio_metrics property of the OptimizationView """ path = '{}/initial_portfolio_metrics'.format(self._get_path(self.id)) resp = request('get', path) return convert_to_analyzere_object(resp)
def result(self): warnings.warn( "result() is deprecated, use candidates() instead to page over results", DeprecationWarning ) path = '{}/result'.format(self._get_path(self.id)) resp = request('get', path) return convert_to_analyzere_object(resp)
def test_object_type(self): o = convert_to_analyzere_object({'id': 'abc123'}) assert type(o) == EmbeddedResource o = convert_to_analyzere_object({'id': 'abc123'}, cls=Resource) assert type(o) == Resource o = convert_to_analyzere_object({'foo': 'bar'}, cls=Resource) assert type(o) == EmbeddedResource resp = {'id': 'abc123', 'embedded': {'foo': 'bar'}} o = convert_to_analyzere_object(resp, cls=Resource) assert type(o.embedded) == EmbeddedResource resp = {'id': 'abc123', 'embedded': {'id': 'xyz456', 'foo': 'bar'}} o = convert_to_analyzere_object(resp, cls=Resource) assert type(o) == Resource assert type(o.embedded) == EmbeddedResource
def test_non_embedded_resource_requires_id(self): # without an ID, an EmbeddedResource with no specific type is the expected conversion # if the class passed was not a subclass of EmbeddedResource class SomeResource(Resource): pass response_dict = {} o = convert_to_analyzere_object(response_dict, SomeResource) assert type(o) == EmbeddedResource
def currencies(self): path = '{}/currencies'.format(self._get_path(self.id)) resp = request('get', path) # response will be an embedded object with currencies list # each element of the currencies list is again an embedded object with the structure like: # { # "code": "CAD" # } return convert_to_analyzere_object(resp)
def sensitivity_analysis(self, candidates=[]): # candidates can be only non negative integers candidates = list(filter(lambda x: isinstance(x, int) and x >= 0, candidates)) if len(candidates) == 0: path = '{}/sensitivity_analysis'.format(self._get_path(self.id)) else: path = '{}/sensitivity_analysis?candidates={}'.format(self._get_path(self.id), ','.join(str(c) for c in candidates)) resp = request('get', path) return convert_to_analyzere_object(resp)
def candidate_parameters(self, index=None): if index is None: path = '{}/candidate_parameters'.format(self._get_path(self.id)) else: try: index = int(index) except ValueError: raise Exception('index argument provided to OptimizationView.candidate_parameters() must be an integer') path = '{}/candidate_parameters/{}'.format(self._get_path(self.id), index) resp = request('get', path) return convert_to_analyzere_object(resp, Candidate, optimization_view_id=self.id)
def test_collection(self): resp = { 'items': [{'id': 'abc123'}, {'id': 'xyz456'}], 'meta': { 'limit': 0, 'offset': 0, 'total_count': 2, 'future_attr': 'foo' } } o = convert_to_analyzere_object(resp, Resource) assert len(o) == 2 assert o.meta.limit == 0 assert o.meta.offset == 0 assert o.meta.total_count == 2 assert o.meta.future_attr == 'foo' assert type(o[0]) == Resource # TODO: When no class is provided, the items should probably be # AnalyzeReObjects o = convert_to_analyzere_object(resp) assert type(o[0]) == EmbeddedResource
def test_nested(self): d = datetime.today() resp = { 'num': {'num': 1}, 'str': {'str': 'foo'}, 'date': {'date': d}, 'list': {'list': [1, 2, 3]}, 'dict': {'dict': {'foo': 'bar'}}, 'ref': {'ref': {'href': 'https://api/layers/abc123'}} } o = convert_to_analyzere_object(resp) assert o.num.num == 1 assert o.str.str == 'foo' assert o.date.date == d assert o.list.list == [1, 2, 3] assert o.dict.dict == EmbeddedResource(foo='bar') assert type(o.ref.ref) == Reference assert o.ref.ref._id == 'abc123'
def test_simple(self): d = datetime.today() resp = { 'num': 1, 'str': 'foo', 'date': d, 'list': [1, 2, 3], 'dict': {'foo': 'bar'}, 'ref': {'href': 'https://api/layers/abc123'}, } o = convert_to_analyzere_object(resp) assert o.num == 1 assert o.str == 'foo' assert o.date == d assert o.list == [1, 2, 3] assert o.dict == EmbeddedResource(foo='bar') assert type(o.dict) == EmbeddedResource # Shallow comparison for reference so it doesn't evaluate assert type(o.ref) == Reference assert o.ref._id == 'abc123'
def candidate_metrics(self): path = '{}/candidate_metrics'.format(self._get_path(self.id)) resp = request('get', path) return convert_to_analyzere_object(resp)
def test_type_renamed(self): resp = {'_type': {'foo'}} o = convert_to_analyzere_object(resp) assert not hasattr(o, '_type') assert hasattr(o, 'type')
def test_private_not_stripped(self): # TODO: Maybe underscore prefixed attributes from the server # (besides _type) should be ignored? o = convert_to_analyzere_object({'_private': 'foo'}) assert o._private == 'foo'
def profile(self): path = '%s/profile' % self._get_path(self.id) resp = request('get', path) return convert_to_analyzere_object(resp)
def portfolio_view(self): path = '{}/candidates/{}/portfolio_view'.format(OptimizationView._get_path(self.optimization_view_id), self.index) resp = request('get', path) return convert_to_analyzere_object(resp, PortfolioView)