Skip to content

Commit

Permalink
Feature/pdct 1790 convert all family metadata list values to strings (#…
Browse files Browse the repository at this point in the history
…267)

* Transform metadata to list of strings

* Bump to 2.17.24

* Expect metadata to be str arrays

* Revert

* Bump db client to 3.8.27

* Bump db client to 3.8.27

* Update poetry.lock

* Add missing env variables to example
  • Loading branch information
katybaulch authored Dec 19, 2024
1 parent c8df79c commit 7fd48e0
Show file tree
Hide file tree
Showing 9 changed files with 455 additions and 417 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# DB
POSTGRES_USER=navigator
POSTGRES_PASSWORD=password

# This value is used for encrypting passwords
# You can get this from the AWS
# If you're using `make dev` you will need to use the staging value
Expand Down
8 changes: 3 additions & 5 deletions app/model/family.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

from pydantic import BaseModel

from app.model.general import Json


class FamilyReadDTO(BaseModel):
"""A JSON representation of a family."""
Expand All @@ -15,7 +13,7 @@ class FamilyReadDTO(BaseModel):
geography: str
category: str
status: str
metadata: Json
metadata: dict[str, list[str]]
slug: str
events: list[str]
published_date: Optional[datetime]
Expand Down Expand Up @@ -43,7 +41,7 @@ class FamilyWriteDTO(BaseModel):
summary: str
geography: str
category: str
metadata: Json
metadata: dict[str, list[str]]
collections: list[str]


Expand All @@ -62,6 +60,6 @@ class FamilyCreateDTO(BaseModel):
summary: str
geography: Union[str, list[str]]
category: str
metadata: Json
metadata: dict[str, list[str]]
collections: list[str]
corpus_import_id: str
790 changes: 408 additions & 382 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "admin_backend"
version = "2.17.23"
version = "2.17.24"
description = ""
authors = ["CPR-dev-team <[email protected]>"]
packages = [{ include = "app" }, { include = "tests" }]
Expand Down Expand Up @@ -29,7 +29,7 @@ boto3 = "^1.34.151"
moto = "^4.2.2"
types-sqlalchemy = "^1.4.53.38"
urllib3 = "^1.26.17"
db-client = { git = "https://github.com/climatepolicyradar/navigator-db-client.git", rev = "v3.8.26" }
db-client = { git = "https://github.com/climatepolicyradar/navigator-db-client.git", tag = "v3.8.27" }
navigator-notify = { git = "https://github.com/climatepolicyradar/navigator-notify.git", tag = "v0.0.2-beta" }
bcrypt = "4.0.1"
fastapi-cli = "^0.0.5"
Expand Down
6 changes: 3 additions & 3 deletions tests/integration_tests/family/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def test_create_family_when_invalid_metadata_cclw(
new_family = create_family_create_dto(
title="Title",
summary="test test test",
metadata={"color": ["pink"], "size": [0]},
metadata={"color": ["pink"], "size": ["0"]},
)
response = client.post(
"/api/v1/families",
Expand Down Expand Up @@ -244,7 +244,7 @@ def test_create_family_when_invalid_metadata_unfccc(
new_family = create_family_create_dto(
title="Title",
summary="test test test",
metadata={"color": ["pink"], "size": [0]},
metadata={"color": ["pink"], "size": ["0"]},
corpus_import_id="UNFCCC.corpus.i00000001.n0000",
)
response = client.post(
Expand Down Expand Up @@ -273,7 +273,7 @@ def test_create_family_when_org_mismatch(
new_family = create_family_create_dto(
title="Title",
summary="test test test",
metadata={"author": "CPR", "author_type": ["Party"]},
metadata={"author": ["CPR"], "author_type": ["Party"]},
corpus_import_id="UNFCCC.corpus.i00000001.n0000",
)
response = client.post(
Expand Down
4 changes: 2 additions & 2 deletions tests/integration_tests/family/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_search_geographies(
"geography": "ALB",
"category": "UNFCCC",
"status": "Created",
"metadata": {"author": "CPR", "author_type": "Party"},
"metadata": {"author": ["CPR"], "author_type": ["Party"]},
"organisation": "UNFCCC",
"corpus_import_id": "UNFCCC.corpus.i00000001.n0000",
"corpus_title": "UNFCCC Submissions",
Expand All @@ -40,7 +40,7 @@ def test_search_geographies(
"geography": "ZMB",
"category": "UNFCCC",
"status": "Created",
"metadata": {"author": "CPR", "author_type": "Party"},
"metadata": {"author": ["CPR"], "author_type": ["Party"]},
"organisation": "UNFCCC",
"corpus_import_id": "UNFCCC.corpus.i00000001.n0000",
"corpus_title": "UNFCCC Submissions",
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests/setup_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class DBEntry(TypedDict):
"geography": "AFG",
"category": "UNFCCC",
"status": "Created",
"metadata": {"author": "CPR", "author_type": "Party"},
"metadata": {"author": ["CPR"], "author_type": ["Party"]},
"organisation": "UNFCCC",
"corpus_import_id": "UNFCCC.corpus.i00000001.n0000",
"corpus_title": "UNFCCC Submissions",
Expand Down
26 changes: 17 additions & 9 deletions tests/unit_tests/service/family/test_create_family_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def test_create(
admin_user_context,
):
new_family = create_family_create_dto(
collections=["x.y.z.1", "x.y.z.2"], metadata={"size": [100], "color": ["blue"]}
collections=["x.y.z.1", "x.y.z.2"],
metadata={"size": ["100"], "color": ["blue"]},
)
family = family_service.create(new_family, admin_user_context)
assert family is not None
Expand All @@ -42,7 +43,8 @@ def test_create_repo_fails(
admin_user_context,
):
new_family = create_family_create_dto(
collections=["x.y.z.1", "x.y.z.2"], metadata={"size": [100], "color": ["blue"]}
collections=["x.y.z.1", "x.y.z.2"],
metadata={"size": ["100"], "color": ["blue"]},
)
family_repo_mock.throw_repository_error = True

Expand Down Expand Up @@ -70,7 +72,8 @@ def test_create_raises_when_category_invalid(
admin_user_context,
):
new_family = create_family_create_dto(
collections=["x.y.z.1", "x.y.z.2"], metadata={"size": [100], "color": ["blue"]}
collections=["x.y.z.1", "x.y.z.2"],
metadata={"size": ["100"], "color": ["blue"]},
)
new_family.category = "invalid"
with pytest.raises(ValidationError) as e:
Expand Down Expand Up @@ -126,7 +129,7 @@ def test_create_raises_when_missing_taxonomy(
collections = ["x.y.z.1", "x.y.z.2"]
db_client_metadata_mock.bad_taxonomy = True
new_family = create_family_create_dto(
collections=collections, metadata={"size": [100], "color": ["blue"]}
collections=collections, metadata={"size": ["100"], "color": ["blue"]}
)
with pytest.raises(ValidationError) as e:
family_service.create(new_family, admin_user_context)
Expand All @@ -153,7 +156,8 @@ def test_create_raises_when_collection_org_different_to_usr_org(
admin_user_context,
):
new_family = create_family_create_dto(
collections=["x.y.z.1", "x.y.z.2"], metadata={"size": [100], "color": ["blue"]}
collections=["x.y.z.1", "x.y.z.2"],
metadata={"size": ["100"], "color": ["blue"]},
)
collection_repo_mock.alternative_org = True
with pytest.raises(AuthorisationError) as e:
Expand All @@ -180,7 +184,8 @@ def test_create_raises_when_corpus_missing(
admin_user_context,
):
new_family = create_family_create_dto(
collections=["x.y.z.1", "x.y.z.2"], metadata={"size": [100], "color": ["blue"]}
collections=["x.y.z.1", "x.y.z.2"],
metadata={"size": ["100"], "color": ["blue"]},
)
corpus_repo_mock.valid = False
with pytest.raises(ValidationError) as e:
Expand All @@ -206,7 +211,8 @@ def test_create_when_no_org_associated_with_entity(
admin_user_context,
):
new_family = create_family_create_dto(
collections=["x.y.z.1", "x.y.z.2"], metadata={"size": [100], "color": ["blue"]}
collections=["x.y.z.1", "x.y.z.2"],
metadata={"size": ["100"], "color": ["blue"]},
)
corpus_repo_mock.error = True
with pytest.raises(ValidationError) as e:
Expand All @@ -233,7 +239,8 @@ def test_create_raises_when_corpus_org_different_to_usr_org(
another_admin_user_context,
):
new_family = create_family_create_dto(
collections=["x.y.z.1", "x.y.z.2"], metadata={"size": [100], "color": ["blue"]}
collections=["x.y.z.1", "x.y.z.2"],
metadata={"size": ["100"], "color": ["blue"]},
)
with pytest.raises(AuthorisationError) as e:
family_service.create(new_family, another_admin_user_context)
Expand All @@ -259,7 +266,8 @@ def test_create_success_when_corpus_org_different_to_usr_org_super(
super_user_context,
):
new_family = create_family_create_dto(
collections=["x.y.z.1", "x.y.z.2"], metadata={"size": [100], "color": ["blue"]}
collections=["x.y.z.1", "x.y.z.2"],
metadata={"size": ["100"], "color": ["blue"]},
)
family = family_service.create(new_family, super_user_context)
assert family is not None
Expand Down
28 changes: 15 additions & 13 deletions tests/unit_tests/service/family/test_update_family_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_update(
updated_family = create_family_write_dto(
title="UPDATED TITLE",
collections=["x.y.z.2", "x.y.z.3"],
metadata={"size": [100], "color": ["blue"]},
metadata={"size": ["100"], "color": ["blue"]},
)
result = family_service.update("a.b.c.d", admin_user_context, updated_family)
assert result is not None
Expand Down Expand Up @@ -55,7 +55,7 @@ def test_update_when_family_missing(
assert family_repo_mock.get.call_count == 0

updated_family = create_family_write_dto(
metadata={"size": [100], "color": ["blue"]}
metadata={"size": ["100"], "color": ["blue"]}
)
family_repo_mock.return_empty = True
result = family_service.update("a.b.c.d", admin_user_context, updated_family)
Expand Down Expand Up @@ -85,7 +85,7 @@ def test_update_raises_when_family_id_invalid(
assert family_repo_mock.get.call_count == 0

updated_family = create_family_write_dto(
metadata={"size": [100], "color": ["blue"]}
metadata={"size": ["100"], "color": ["blue"]}
)
family.import_id = "invalid"
with pytest.raises(ValidationError) as e:
Expand Down Expand Up @@ -116,7 +116,7 @@ def test_update_raises_when_category_invalid(
assert family_repo_mock.get.call_count == 0

updated_family = create_family_write_dto(
category="invalid", metadata={"size": [100], "color": ["blue"]}
category="invalid", metadata={"size": ["100"], "color": ["blue"]}
)

with pytest.raises(ValidationError) as e:
Expand Down Expand Up @@ -147,7 +147,7 @@ def test_update_raises_when_organisation_invalid(
assert family_repo_mock.get.call_count == 0

updated_family = create_family_write_dto(
metadata={"size": [100], "color": ["blue"]}
metadata={"size": ["100"], "color": ["blue"]}
)

corpus_repo_mock.error = True
Expand Down Expand Up @@ -180,7 +180,7 @@ def test_update_family_raises_when_geography_invalid(
assert family_repo_mock.get.call_count == 0

updated_family = create_family_write_dto(
metadata={"size": [100], "color": ["blue"]}
metadata={"size": ["100"], "color": ["blue"]}
)

geography_repo_mock.error = True
Expand Down Expand Up @@ -209,7 +209,7 @@ def test_update_family_raises_when_metadata_invalid(
family = family_service.get("a.b.c.d")
assert family is not None # needed to placate pyright

updated_family = create_family_write_dto(metadata={"invalid": True})
updated_family = create_family_write_dto(metadata={"invalid": ["True"]})

with pytest.raises(ValidationError) as e:
family_service.update("a.b.c.d", admin_user_context, updated_family)
Expand Down Expand Up @@ -244,7 +244,7 @@ def test_update_family_raises_when_missing_taxonomy(
assert family is not None # needed to placate pyright

db_client_metadata_mock.bad_taxonomy = True
updated_family = create_family_write_dto(metadata={"invalid": True})
updated_family = create_family_write_dto(metadata={"invalid": ["True"]})

with pytest.raises(ValidationError) as e:
family_service.update("a.b.c.d", admin_user_context, updated_family)
Expand Down Expand Up @@ -274,7 +274,7 @@ def test_update_family_raises_when_collection_id_invalid(

updated_family = create_family_write_dto(
collections=["x.y.z.2", "col3", "col4"],
metadata={"size": [100], "color": ["blue"]},
metadata={"size": ["100"], "color": ["blue"]},
)

with pytest.raises(ValidationError) as e:
Expand Down Expand Up @@ -304,7 +304,7 @@ def test_update_family_raises_when_collection_missing(
assert family is not None # needed to placate pyright

updated_family = create_family_write_dto(
metadata={"size": [100], "color": ["blue"]}
metadata={"size": ["100"], "color": ["blue"]}
)

collection_repo_mock.missing = True
Expand Down Expand Up @@ -335,7 +335,8 @@ def test_update_family_raises_when_collection_org_different_to_usr_org(
assert family is not None # needed to placate pyright

updated_family = create_family_write_dto(
collections=["x.y.z.2", "x.y.z.3"], metadata={"size": [100], "color": ["blue"]}
collections=["x.y.z.2", "x.y.z.3"],
metadata={"size": ["100"], "color": ["blue"]},
)

collection_repo_mock.alternative_org = True
Expand Down Expand Up @@ -365,7 +366,8 @@ def test_update_raises_when_family_organisation_mismatch_with_user_org(
assert family is not None # needed to placate pyright

updated_family = create_family_write_dto(
collections=["x.y.z.2", "x.y.z.3"], metadata={"size": [100], "color": ["blue"]}
collections=["x.y.z.2", "x.y.z.3"],
metadata={"size": ["100"], "color": ["blue"]},
)

with pytest.raises(AuthorisationError) as e:
Expand Down Expand Up @@ -403,7 +405,7 @@ def test_update_success_when_family_organisation_mismatch_with_user_org(
updated_family = create_family_write_dto(
title="UPDATED TITLE",
collections=["x.y.z.2", "x.y.z.3"],
metadata={"size": [100], "color": ["blue"]},
metadata={"size": ["100"], "color": ["blue"]},
)
result = family_service.update("a.b.c.d", super_user_context, updated_family)
assert result is not None
Expand Down

0 comments on commit 7fd48e0

Please sign in to comment.