From 046ad223742283a18e6fd113d9ddf4bedf54608a Mon Sep 17 00:00:00 2001 From: Alex Whitehead-Smith Date: Tue, 5 Nov 2024 11:39:10 +0000 Subject: [PATCH] Convert page numbers to strings when JSON encoding JSON doesn't have an int data type - it's either floats or strings. We're never doing any maths with page numbers so strings make more sense. --- backend/models/recipe.py | 4 ++-- backend/tests/fixtures.py | 2 +- backend/tests/models/test_recipe.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/models/recipe.py b/backend/models/recipe.py index 7f561ed..bb3e8e6 100644 --- a/backend/models/recipe.py +++ b/backend/models/recipe.py @@ -34,7 +34,7 @@ class Book(Source): type: SourceType = SourceType.BOOK def to_dict(self) -> Dict[str, str]: - return {"type": self.type.value, "title": self.title, "page": self.page} + return {"type": self.type.value, "title": self.title, "page": str(self.page)} class Recipe: @@ -87,7 +87,7 @@ def _source_from_dict(cls, input: Dict[str, Any]) -> Source: elif input["type"] == "website": return Website(address=input["address"]) elif input["type"] == "book": - return Book(title=input["title"], page=input["page"]) + return Book(title=input["title"], page=int(input["page"])) else: raise ValueError("Provided type is not recognised") diff --git a/backend/tests/fixtures.py b/backend/tests/fixtures.py index 7236f13..9110bd7 100644 --- a/backend/tests/fixtures.py +++ b/backend/tests/fixtures.py @@ -9,7 +9,7 @@ API_GATEWAY_PROXY_EVENT_V2 = APIGatewayProxyEventV2({"body": json.dumps({})}) SOURCE_WEBSITE_DICT = {"type": "website", "address": "address"} -SOURCE_BOOK_DICT = {"type": "book", "page": 1, "title": "title"} +SOURCE_BOOK_DICT = {"type": "book", "page": "1", "title": "title"} RECIPE_DICT = { "id": "id", diff --git a/backend/tests/models/test_recipe.py b/backend/tests/models/test_recipe.py index a7df0b3..0dfe019 100644 --- a/backend/tests/models/test_recipe.py +++ b/backend/tests/models/test_recipe.py @@ -66,7 +66,7 @@ def test_book_source_from_dict(self): assert isinstance(source, Book) assert source.type == SourceType.BOOK assert source.title == SOURCE_BOOK_DICT["title"] - assert source.page == SOURCE_BOOK_DICT["page"] + assert source.page == int(SOURCE_BOOK_DICT["page"]) def test_source_from_dict_raises_when_type_missing(self): bad_source = deepcopy(SOURCE_WEBSITE_DICT)