Add API endpoint to fetch an entity (#25)
* Add API endpoint to fetch an entity * Move logic to outside of the API
This commit is contained in:
parent
077daf622f
commit
3a458da2cb
@ -64,7 +64,7 @@ def to_json(api_call):
|
||||
"Content-Type": "application/json"}
|
||||
except AlmanachEntityNotFoundException as e:
|
||||
logging.warning(e.message)
|
||||
return encode({"error": "Entity not found for updating closed"}), 400, {"Content-Type": "application/json"}
|
||||
return encode({"error": "Entity not found"}), 404, {"Content-Type": "application/json"}
|
||||
|
||||
except Exception as e:
|
||||
logging.exception(e)
|
||||
@ -289,6 +289,13 @@ def entity_exists(entity_id):
|
||||
return response
|
||||
|
||||
|
||||
@api.route("/entity/<entity_id>", methods=["GET"])
|
||||
@authenticated
|
||||
@to_json
|
||||
def get_entity(entity_id):
|
||||
return controller.get_all_entities_by_id(entity_id)
|
||||
|
||||
|
||||
@api.route("/volume_types", methods=["GET"])
|
||||
@authenticated
|
||||
@to_json
|
||||
|
@ -90,6 +90,11 @@ class DatabaseAdapter(object):
|
||||
entities = self._get_entities_from_db(args)
|
||||
return [build_entity_from_dict(entity) for entity in entities]
|
||||
|
||||
@database
|
||||
def get_all_entities_by_id(self, entity_id):
|
||||
entities = self.db.entity.find({"entity_id": entity_id}, {"_id": 0})
|
||||
return [build_entity_from_dict(entity) for entity in entities]
|
||||
|
||||
@database
|
||||
def list_entities_by_id(self, entity_id, start, end):
|
||||
entities = self.db.entity.find({"entity_id": entity_id,
|
||||
|
@ -125,6 +125,11 @@ class Controller(object):
|
||||
def entity_exists(self, entity_id):
|
||||
return self.database_adapter.count_entity_entries(entity_id=entity_id) >= 1
|
||||
|
||||
def get_all_entities_by_id(self, entity_id):
|
||||
if not self.entity_exists(entity_id=entity_id):
|
||||
raise AlmanachEntityNotFoundException("Entity not found")
|
||||
return self.database_adapter.get_all_entities_by_id(entity_id=entity_id)
|
||||
|
||||
def attach_volume(self, volume_id, date, attachments):
|
||||
date = self._validate_and_parse_date(date)
|
||||
logging.info("volume %s attached to %s on %s" % (volume_id, attachments, date))
|
||||
|
@ -11,9 +11,25 @@ class ApiInstanceEntityTest(BaseApiTestCase):
|
||||
|
||||
assert_that(response.status_code, equal_to(200))
|
||||
|
||||
def test_head_entity_by_id__without_entity_return_404(self):
|
||||
def test_head_entity_by_id_without_entity_return_404(self):
|
||||
instance_id = "some_uuid"
|
||||
response = self.almanachHelper.head(url="{url}/entity/{instance_id}",
|
||||
instance_id=instance_id)
|
||||
|
||||
assert_that(response.status_code, equal_to(404))
|
||||
|
||||
def test_get_entity_by_id_with_entity_return_200(self):
|
||||
instance_id = self._create_instance_entity()
|
||||
response = self.almanachHelper.get(url="{url}/entity/{instance_id}", instance_id=instance_id)
|
||||
|
||||
result = response.json()
|
||||
assert_that(response.status_code, equal_to(200))
|
||||
assert_that(len(result), equal_to(1))
|
||||
assert_that(result[0]["entity_id"], equal_to(instance_id))
|
||||
|
||||
def test_get_entity_by_id_with_entity_return_404(self):
|
||||
response = self.almanachHelper.get(url="{url}/entity/{instance_id}", instance_id="foobar")
|
||||
|
||||
result = response.json()
|
||||
assert_that(result, equal_to({"error": "Entity not found"}))
|
||||
assert_that(response.status_code, equal_to(404))
|
||||
|
@ -112,6 +112,17 @@ class DatabaseAdapterTest(unittest.TestCase):
|
||||
self.assertEqual(1, self.adapter.count_entity_entries("id1"))
|
||||
self.assertEqual(2, self.adapter.count_entity_entries("id2"))
|
||||
|
||||
def test_get_entity(self):
|
||||
fake_entity = a(instance().with_id("id1").with_start(2014, 1, 1, 8, 0, 0).with_no_end())
|
||||
self.db.entity.insert(todict(fake_entity))
|
||||
|
||||
entries = self.adapter.get_all_entities_by_id(entity_id="id1")
|
||||
self.assertEqual(1, len(entries))
|
||||
self.assertEqual("id1", entries[0].entity_id)
|
||||
|
||||
entries = self.adapter.get_all_entities_by_id(entity_id="id2")
|
||||
self.assertEqual(0, len(entries))
|
||||
|
||||
def test_list_instances(self):
|
||||
fake_instances = [
|
||||
a(instance().with_id("id1").with_start(2014, 1, 1, 7, 0, 0).with_end(
|
||||
|
@ -741,6 +741,36 @@ class ControllerTest(unittest.TestCase):
|
||||
|
||||
assert_that(False, equal_to(self.controller.entity_exists(entity_id)))
|
||||
|
||||
def test_get_all_entities_by_id(self):
|
||||
entity_id = "some_entity_id"
|
||||
fake_entity = a(instance().with_id(entity_id))
|
||||
result = [fake_entity]
|
||||
|
||||
(flexmock(self.database_adapter)
|
||||
.should_receive("count_entity_entries")
|
||||
.with_args(entity_id=entity_id)
|
||||
.and_return(1))
|
||||
|
||||
(flexmock(self.database_adapter)
|
||||
.should_receive("get_all_entities_by_id")
|
||||
.with_args(entity_id=entity_id)
|
||||
.and_return(result))
|
||||
|
||||
assert_that(result, equal_to(self.controller.get_all_entities_by_id(entity_id)))
|
||||
|
||||
def test_get_all_entities_by_id_when_entity_not_found(self):
|
||||
entity_id = "some_entity_id"
|
||||
|
||||
(flexmock(self.database_adapter)
|
||||
.should_receive("count_entity_entries")
|
||||
.with_args(entity_id=entity_id)
|
||||
.and_return(0))
|
||||
|
||||
assert_that(
|
||||
calling(self.controller.get_all_entities_by_id).with_args(entity_id),
|
||||
raises(AlmanachEntityNotFoundException)
|
||||
)
|
||||
|
||||
def test_rename_volume(self):
|
||||
fake_volume = a(volume().with_display_name('old_volume_name'))
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user