Skip to content

Commit

Permalink
list cases (works when empty) #2714
Browse files Browse the repository at this point in the history
  • Loading branch information
iamleeg committed Jun 20, 2022
1 parent f2879cd commit 6b405d3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ def get_case(self, id: str):
if case is None:
return f"No case with ID {id}", 404
return jsonify(case), 200

def list_cases(self):
"""Implements get /cases."""
cases = self._store.all_cases()
return jsonify(cases), 200
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
case_controller = None # Will be set up in main()


@app.route("/api/case/<id>")
@app.route("/api/cases/<id>")
def get_case(id):
return case_controller.get_case(id)


@app.route("/api/cases")
def list_cases():
return case_controller.list_cases()


def set_up_controllers():
global case_controller
store_options = {"mongodb": MongoStore.setup}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def case_by_id(self, id: str):
except InvalidId:
return None

def all_cases(self):
cases = self.get_case_collection().find({})
return loads(dumps(cases))

@staticmethod
def setup():
"""Configure a store instance from the environment."""
Expand Down
11 changes: 11 additions & 0 deletions data-serving/reusable-data-service/tests/test_case_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ def case_by_id(self, id: str):

def put_case(self, id: str, case: Case):
self.cases[id] = case

def all_cases(self):
return list(self.cases.values())


@pytest.fixture
Expand All @@ -39,3 +42,11 @@ def test_one_absent_item_should_return_400_not_found(app_context):
(response, status) = controller.get_case("foo")
assert status == 404
assert response == "No case with ID foo"


def test_list_cases_should_return_200_OK(app_context):
store = MemoryStore()
controller = CaseController(store)
(response, status) = controller.list_cases()
assert status == 200
assert response.json == []
12 changes: 9 additions & 3 deletions data-serving/reusable-data-service/tests/test_case_end_to_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,24 @@ def test_get_case_with_known_id(client_with_patched_mongo):
.insert_one({"confirmation_date": "2021-12-31T01:23:45.678Z"})
.inserted_id
)
response = client_with_patched_mongo.get(f"/api/case/{str(case_id)}")
response = client_with_patched_mongo.get(f"/api/cases/{str(case_id)}")
result = response.get_json()
assert response.status_code == 200
assert result is not None
assert result["confirmation_date"] == "2021-12-31T01:23:45.678Z"


def test_get_case_with_poorly_formatted_id(client_with_patched_mongo):
response = client_with_patched_mongo.get(f"/api/case/not_a_case_id")
response = client_with_patched_mongo.get(f"/api/cases/not_a_case_id")
assert response.status_code == 404


def test_get_case_with_valid_absent_id(client_with_patched_mongo):
response = client_with_patched_mongo.get(f"/api/case/01234567890123456789abcd")
response = client_with_patched_mongo.get(f"/api/cases/01234567890123456789abcd")
assert response.status_code == 404


def test_list_cases_when_none_present_is_empty_list(client_with_patched_mongo):
response = client_with_patched_mongo.get(f"/api/cases")
assert response.status_code == 200
assert response.json == []

0 comments on commit 6b405d3

Please sign in to comment.