Skip to content

Commit

Permalink
Extra PEP8 compliance #2714
Browse files Browse the repository at this point in the history
  • Loading branch information
iamleeg committed Jun 21, 2022
1 parent 608df92 commit 6ee7b73
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,20 @@ def get_case(self, id: str):
return f"No case with ID {id}", 404
return jsonify(case), 200

def list_cases(self, page:int=None, limit:int=None):
def list_cases(self, page: int = None, limit: int = None):
"""Implements get /cases."""
page = 1 if page is None else page
limit = 10 if limit is None else limit
validation_error = None
if page <= 0:
validation_error = { "message" : "page must be >0" }
validation_error = {"message": "page must be >0"}
if limit <= 0:
validation_error = { "message" : "limit must be >0" }
validation_error = {"message": "limit must be >0"}
if validation_error is not None:
return jsonify(validation_error), 422
cases = self.store.fetch_cases(page, limit)
count = self.store.count_cases()
response = {
"cases": cases,
"total": count
}
response = {"cases": cases, "total": count}
if count > page * limit:
response["nextPage"] = page + 1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def get_case(id):

@app.route("/api/cases")
def list_cases():
page = request.args.get('page', type=int)
limit = request.args.get('limit', type=int)
page = request.args.get("page", type=int)
limit = request.args.get("limit", type=int)
return case_controller.list_cases(page=page, limit=limit)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ def case_by_id(self, id: str):
return None

def fetch_cases(self, page: int, limit: int):
cases = self.get_case_collection().find({}, skip=(page - 1) * limit, limit = limit)
cases = self.get_case_collection().find(
{}, skip=(page - 1) * limit, limit=limit
)
return [Case.from_json(dumps(c)) for c in cases]

def count_cases(self) -> int:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ def put_case(self, id: str, case: Case):
self.cases[id] = case

def fetch_cases(self, page: int, limit: int):
return list(self.cases.values())[(page - 1) * limit: page * limit]
return list(self.cases.values())[(page - 1) * limit : page * limit]

def count_cases(self):
return len(self.cases)


@pytest.fixture
def case_controller():
with app.app_context():
Expand Down Expand Up @@ -71,6 +72,7 @@ def test_list_cases_should_paginate(case_controller):
assert response.json["nextPage"] == 2
assert response.json["total"] == 15


def test_list_cases_last_page(case_controller):
with open("./tests/data/case.minimal.json", "r") as minimal_file:
case = Case.from_json(minimal_file.read())
Expand All @@ -82,6 +84,7 @@ def test_list_cases_last_page(case_controller):
assert response.json["total"] == 15
assert "nextPage" not in response.json


def test_list_cases_nonexistent_page(case_controller):
with open("./tests/data/case.minimal.json", "r") as minimal_file:
case = Case.from_json(minimal_file.read())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ def test_list_cases_when_none_present_is_empty_list(client_with_patched_mongo):
assert response.status_code == 200
assert response.json["cases"] == []


def test_list_cases_with_pagination_query(client_with_patched_mongo):
db = pymongo.MongoClient("mongodb://localhost:27017/outbreak")
db["outbreak"]["cases"].insert_many([{"confirmation_date": datetime(2020, 12, 24)} for i in range(25)])
db["outbreak"]["cases"].insert_many(
[{"confirmation_date": datetime(2020, 12, 24)} for i in range(25)]
)
response = client_with_patched_mongo.get(f"/api/cases?page=2&limit=10")
assert response.status_code == 200
assert len(response.json["cases"]) == 10
Expand Down

0 comments on commit 6ee7b73

Please sign in to comment.