From c79317fc09f0f173b64e4754eabacc56c8afe59a Mon Sep 17 00:00:00 2001 From: Graham Lee Date: Tue, 28 Jun 2022 10:24:28 +0100 Subject: [PATCH] Rename confirmation_date to camelCase to match existing data structure #2714 --- .../controller/case_controller.py | 6 +-- .../reusable_data_service/main.py | 5 ++- .../reusable_data_service/model/case.py | 10 ++--- .../tests/data/case.minimal.json | 2 +- .../tests/test_case_controller.py | 16 +++---- .../tests/test_case_end_to_end.py | 44 ++++++++++--------- 6 files changed, 43 insertions(+), 40 deletions(-) diff --git a/data-serving/reusable-data-service/reusable_data_service/controller/case_controller.py b/data-serving/reusable-data-service/reusable_data_service/controller/case_controller.py index 7792e53ee..3053353e5 100644 --- a/data-serving/reusable-data-service/reusable_data_service/controller/case_controller.py +++ b/data-serving/reusable-data-service/reusable_data_service/controller/case_controller.py @@ -94,7 +94,7 @@ def create_case_if_valid(self, maybe_case: dict): return case def check_case_preconditions(self, case: Case): - if case.confirmation_date < self.outbreak_date: + if case.confirmationDate < self.outbreak_date: raise PreconditionError("Confirmation date is before outbreak began") @staticmethod @@ -122,11 +122,11 @@ def individual_filter(term: str) -> Filter: # special case dateconfirmedbefore, dateconfirmedafter if keyword == "dateconfirmedbefore": return PropertyFilter( - "confirmation_date", FilterOperator.LESS_THAN, date.fromisoformat(value) + "confirmationDate", FilterOperator.LESS_THAN, date.fromisoformat(value) ) if keyword == "dateconfirmedafter": return PropertyFilter( - "confirmation_date", + "confirmationDate", FilterOperator.GREATER_THAN, date.fromisoformat(value), ) diff --git a/data-serving/reusable-data-service/reusable_data_service/main.py b/data-serving/reusable-data-service/reusable_data_service/main.py index bd399a44c..d79129233 100644 --- a/data-serving/reusable-data-service/reusable_data_service/main.py +++ b/data-serving/reusable-data-service/reusable_data_service/main.py @@ -17,9 +17,9 @@ def get_case(id): return case_controller.get_case(id) -@app.route("/api/cases", methods = ['POST', 'GET']) +@app.route("/api/cases", methods=["POST", "GET"]) def list_cases(): - if request.method == 'GET': + if request.method == "GET": page = request.args.get("page", type=int) limit = request.args.get("limit", type=int) filter = request.args.get("q", type=str) @@ -34,6 +34,7 @@ def list_cases(): count = 1 return case_controller.create_case(potential_case, num_cases=count) + def set_up_controllers(): global case_controller store_options = {"mongodb": MongoStore.setup} diff --git a/data-serving/reusable-data-service/reusable_data_service/model/case.py b/data-serving/reusable-data-service/reusable_data_service/model/case.py index 447be5e3a..18ac0c7ea 100644 --- a/data-serving/reusable-data-service/reusable_data_service/model/case.py +++ b/data-serving/reusable-data-service/reusable_data_service/model/case.py @@ -18,7 +18,7 @@ class DayZeroCase: to that function).""" _: dataclasses.KW_ONLY - confirmation_date: datetime.date = dataclasses.field(init=False) + confirmationDate: datetime.date = dataclasses.field(init=False) @classmethod def from_json(cls, obj: str) -> type: @@ -55,11 +55,11 @@ def from_dict(cls, dictionary: dict[str, Any]) -> type: def validate(self): """Check whether I am consistent. Raise ValueError if not.""" - if not hasattr(self, "confirmation_date"): + if not hasattr(self, "confirmationDate"): raise ValueError("Confirmation Date is mandatory") - elif self.confirmation_date is None: + elif self.confirmationDate is None: raise ValueError("Confirmation Date must have a value") - + def to_dict(self): """Return myself as a dictionary.""" return dataclasses.asdict(self) @@ -68,7 +68,7 @@ def to_dict(self): def date_fields(cls) -> list[str]: """Record where dates are kept because they sometimes need special treatment. A subclass could override this method to indicate it stores additional date fields.""" - return ["confirmation_date"] + return ["confirmationDate"] # Actually we want to capture extra fields which can be specified dynamically: diff --git a/data-serving/reusable-data-service/tests/data/case.minimal.json b/data-serving/reusable-data-service/tests/data/case.minimal.json index a18817641..cf1ab0634 100644 --- a/data-serving/reusable-data-service/tests/data/case.minimal.json +++ b/data-serving/reusable-data-service/tests/data/case.minimal.json @@ -1,3 +1,3 @@ { - "confirmation_date": "2021-12-31T01:23:45.678Z" + "confirmationDate": "2021-12-31T01:23:45.678Z" } \ No newline at end of file diff --git a/data-serving/reusable-data-service/tests/test_case_controller.py b/data-serving/reusable-data-service/tests/test_case_controller.py index a13f2b959..2dee66065 100644 --- a/data-serving/reusable-data-service/tests/test_case_controller.py +++ b/data-serving/reusable-data-service/tests/test_case_controller.py @@ -112,14 +112,14 @@ def test_create_case_with_missing_properties_400_error(case_controller): def test_create_case_with_invalid_data_422_error(case_controller): (response, status) = case_controller.create_case( - {"confirmation_date": date(2001, 3, 17)} + {"confirmationDate": date(2001, 3, 17)} ) assert status == 422 def test_create_valid_case_adds_to_collection(case_controller): (response, status) = case_controller.create_case( - {"confirmation_date": date(2021, 6, 3)} + {"confirmationDate": date(2021, 6, 3)} ) assert status == 201 assert case_controller.store.count_cases() == 1 @@ -127,16 +127,14 @@ def test_create_valid_case_adds_to_collection(case_controller): def test_create_valid_case_with_negative_count_400_error(case_controller): (response, status) = case_controller.create_case( - {"confirmation_date": date(2021, 6, 3)}, - num_cases = -7 + {"confirmationDate": date(2021, 6, 3)}, num_cases=-7 ) assert status == 400 def test_create_valid_case_with_positive_count_adds_to_collection(case_controller): (response, status) = case_controller.create_case( - {"confirmation_date": date(2021, 6, 3)}, - num_cases = 7 + {"confirmationDate": date(2021, 6, 3)}, num_cases=7 ) assert status == 201 assert case_controller.store.count_cases() == 7 @@ -147,9 +145,11 @@ def test_validate_case_with_invalid_case_is_400_error(case_controller): assert status == 400 -def test_validate_case_with_valid_case_returns_204_and_does_not_add_case(case_controller): +def test_validate_case_with_valid_case_returns_204_and_does_not_add_case( + case_controller, +): (response, status) = case_controller.validate_case_dictionary( - {"confirmation_date": date(2021, 6, 3)} + {"confirmationDate": date(2021, 6, 3)} ) assert status == 204 assert case_controller.store.count_cases() == 0 diff --git a/data-serving/reusable-data-service/tests/test_case_end_to_end.py b/data-serving/reusable-data-service/tests/test_case_end_to_end.py index ba0e47e7e..7b07428f3 100644 --- a/data-serving/reusable-data-service/tests/test_case_end_to_end.py +++ b/data-serving/reusable-data-service/tests/test_case_end_to_end.py @@ -34,14 +34,14 @@ def test_get_case_with_known_id(client_with_patched_mongo): db = pymongo.MongoClient("mongodb://localhost:27017/outbreak") case_id = ( db["outbreak"]["cases"] - .insert_one({"confirmation_date": datetime(2021, 12, 31, 1, 23, 45, 678)}) + .insert_one({"confirmationDate": datetime(2021, 12, 31, 1, 23, 45, 678)}) .inserted_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-31" + assert result["confirmationDate"] == "2021-12-31" def test_get_case_with_poorly_formatted_id(client_with_patched_mongo): @@ -63,7 +63,7 @@ def test_list_cases_when_none_present_is_empty_list(client_with_patched_mongo): 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)] + [{"confirmationDate": 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 @@ -85,7 +85,7 @@ def test_list_cases_with_negative_page_rejected(client_with_patched_mongo): def test_list_cases_filter_confirmation_date_before(client_with_patched_mongo): db = pymongo.MongoClient("mongodb://localhost:27017/outbreak") db["outbreak"]["cases"].insert_many( - [{"confirmation_date": datetime(2022, 5, i)} for i in range(1, 32)] + [{"confirmationDate": datetime(2022, 5, i)} for i in range(1, 32)] ) response = client_with_patched_mongo.get( f"/api/cases?q=dateconfirmedbefore%3a2022-05-10" @@ -93,7 +93,7 @@ def test_list_cases_filter_confirmation_date_before(client_with_patched_mongo): assert response.status_code == 200 assert len(response.json["cases"]) == 9 assert response.json["total"] == 9 - dates = [c["confirmation_date"] for c in response.json["cases"]] + dates = [c["confirmationDate"] for c in response.json["cases"]] assert "2022-05-11" not in dates assert "2022-05-10" not in dates assert "2022-05-09" in dates @@ -102,7 +102,7 @@ def test_list_cases_filter_confirmation_date_before(client_with_patched_mongo): def test_list_cases_filter_confirmation_date_after(client_with_patched_mongo): db = pymongo.MongoClient("mongodb://localhost:27017/outbreak") db["outbreak"]["cases"].insert_many( - [{"confirmation_date": datetime(2022, 5, i)} for i in range(1, 32)] + [{"confirmationDate": datetime(2022, 5, i)} for i in range(1, 32)] ) response = client_with_patched_mongo.get( f"/api/cases?q=dateconfirmedafter%3a2022-05-10" @@ -110,7 +110,7 @@ def test_list_cases_filter_confirmation_date_after(client_with_patched_mongo): assert response.status_code == 200 assert len(response.json["cases"]) == 10 assert response.json["total"] == 21 - dates = [c["confirmation_date"] for c in response.json["cases"]] + dates = [c["confirmationDate"] for c in response.json["cases"]] assert "2022-05-09" not in dates assert "2022-05-10" not in dates assert "2022-05-11" in dates @@ -121,7 +121,7 @@ def test_list_cases_filter_confirmation_date_before_and_after( ): db = pymongo.MongoClient("mongodb://localhost:27017/outbreak") db["outbreak"]["cases"].insert_many( - [{"confirmation_date": datetime(2022, 5, i)} for i in range(1, 32)] + [{"confirmationDate": datetime(2022, 5, i)} for i in range(1, 32)] ) response = client_with_patched_mongo.get( f"/api/cases?q=dateconfirmedafter%3a2022-05-10%20dateconfirmedbefore%3a2022-05-13" @@ -129,7 +129,7 @@ def test_list_cases_filter_confirmation_date_before_and_after( assert response.status_code == 200 assert len(response.json["cases"]) == 2 assert response.json["total"] == 2 - dates = [c["confirmation_date"] for c in response.json["cases"]] + dates = [c["confirmationDate"] for c in response.json["cases"]] assert "2022-05-10" not in dates assert "2022-05-11" in dates assert "2022-05-12" in dates @@ -139,7 +139,7 @@ def test_list_cases_filter_confirmation_date_before_and_after( def test_list_cases_no_matching_results(client_with_patched_mongo): db = pymongo.MongoClient("mongodb://localhost:27017/outbreak") db["outbreak"]["cases"].insert_many( - [{"confirmation_date": datetime(2022, 5, i)} for i in range(1, 32)] + [{"confirmationDate": datetime(2022, 5, i)} for i in range(1, 32)] ) response = client_with_patched_mongo.get( f"/api/cases?q=dateconfirmedafter%3a2023-05-10" @@ -155,30 +155,32 @@ def test_list_cases_with_bad_filter_rejected(client_with_patched_mongo): def test_post_case_list_cases_round_trip(client_with_patched_mongo): - post_response = client_with_patched_mongo.post("/api/cases", json = { - "confirmation_date": "2022-01-23T13:45:01.234Z" - }) + post_response = client_with_patched_mongo.post( + "/api/cases", json={"confirmationDate": "2022-01-23T13:45:01.234Z"} + ) assert post_response.status_code == 201 get_response = client_with_patched_mongo.get("/api/cases") assert get_response.status_code == 200 assert len(get_response.json["cases"]) == 1 - assert get_response.json["cases"][0]["confirmation_date"] == "2022-01-23" + assert get_response.json["cases"][0]["confirmationDate"] == "2022-01-23" def test_post_multiple_case_list_cases_round_trip(client_with_patched_mongo): - post_response = client_with_patched_mongo.post("/api/cases?num_cases=3", json = { - "confirmation_date": "2022-01-23T13:45:01.234Z" - }) + post_response = client_with_patched_mongo.post( + "/api/cases?num_cases=3", json={"confirmationDate": "2022-01-23T13:45:01.234Z"} + ) assert post_response.status_code == 201 get_response = client_with_patched_mongo.get("/api/cases") assert get_response.status_code == 200 assert len(get_response.json["cases"]) == 3 - assert get_response.json["cases"][0]["confirmation_date"] == "2022-01-23" + assert get_response.json["cases"][0]["confirmationDate"] == "2022-01-23" + def test_post_case_validate_only(client_with_patched_mongo): - post_response = client_with_patched_mongo.post("/api/cases?validate_only=true", json = { - "confirmation_date": "2022-01-23T13:45:01.234Z" - }) + post_response = client_with_patched_mongo.post( + "/api/cases?validate_only=true", + json={"confirmationDate": "2022-01-23T13:45:01.234Z"}, + ) assert post_response.status_code == 204 get_response = client_with_patched_mongo.get("/api/cases") assert get_response.status_code == 200