Skip to content

Commit

Permalink
Convert page numbers to strings when JSON encoding
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alex9smith committed Nov 5, 2024
1 parent 31b685b commit 046ad22
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions backend/models/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion backend/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/models/test_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 046ad22

Please sign in to comment.