Skip to content

Commit

Permalink
Feature/pdct 1245 Remove default metadata value if metadata was null …
Browse files Browse the repository at this point in the history
…& bump db_client to 3.8.5 (#173)

* Bump db_client to 3.8.5

* Remove default metadata value

* Bump to 2.10.9

* Check we're not updating metadata with None or empty values

* Make sure family can be found for doc in test

* Make metadata key value None

* Make role not nullable for updates
  • Loading branch information
katybaulch authored Jul 9, 2024
1 parent fbead9d commit d191021
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/model/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class DocumentWriteDTO(BaseModel):

# From FamilyDocument
variant_name: Optional[str]
role: Optional[str]
role: str
type: Optional[str]
metadata: Json

Expand Down
6 changes: 1 addition & 5 deletions app/repository/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,7 @@ def _doc_to_dto(doc_query_return: ReadObj) -> DocumentReadDTO:
created=cast(datetime, fdoc.created),
last_modified=cast(datetime, fdoc.last_modified),
slug=str(fdoc.slugs[0].name if len(fdoc.slugs) > 0 else ""),
metadata=(
cast(dict, fdoc.valid_metadata)
if fdoc.valid_metadata is not None
else {"role": []}
),
metadata=cast(dict, fdoc.valid_metadata),
physical_id=cast(int, pdoc.id),
title=str(pdoc.title),
md5_sum=str(pdoc.md5_sum) if pdoc.md5_sum is not None else None,
Expand Down
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.10.8"
version = "2.10.9"
description = ""
authors = ["CPR-dev-team <[email protected]>"]
packages = [{ include = "app" }, { include = "tests" }]
Expand Down Expand Up @@ -29,7 +29,7 @@ boto3 = "^1.28.46"
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", tag = "v3.8.4" }
db-client = { git = "https://github.com/climatepolicyradar/navigator-db-client.git", tag = "v3.8.5" }

[tool.poetry.dev-dependencies]
pre-commit = "^2.17.0"
Expand Down
4 changes: 3 additions & 1 deletion tests/integration_tests/document/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,9 @@ def test_create_document_when_empty_variant(
client: TestClient, data_db: Session, user_header_token
):
setup_db(data_db)
new_document = create_document_create_dto(title="Empty variant", variant_name="")
new_document = create_document_create_dto(
title="Empty variant", family_import_id="A.0.0.2", variant_name=""
)
response = client.post(
"/api/v1/documents",
json=new_document.model_dump(mode="json"),
Expand Down
83 changes: 83 additions & 0 deletions tests/integration_tests/document/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,89 @@ def test_update_document_raises_when_metadata_invalid(
assert pd.source_url == "http://source1/"


def test_update_document_raises_when_metadata_required_field_blank(
client: TestClient, data_db: Session, non_cclw_user_header_token
):
setup_db(data_db)

# Keep all values apart from the metadata the same.
new_document = DocumentWriteDTO(
variant_name="Original Language",
role="MAIN",
type="Law",
metadata={"role": []},
title="big title1",
source_url=cast(AnyHttpUrl, "http://source1/"),
user_language_name="English",
)

response = client.put(
"/api/v1/documents/D.0.0.1",
json=new_document.model_dump(mode="json"),
headers=non_cclw_user_header_token,
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
data = response.json()

key_text = "'role'"
expected_message = (
f"Metadata validation failed: Blank value for metadata key {key_text}"
)
assert data["detail"].startswith(expected_message)
assert len(data["detail"]) == len(expected_message)

fd, pd = _get_doc_tuple(data_db, "D.0.0.1")
assert fd.import_id == "D.0.0.1"
assert fd.variant_name == "Original Language"
assert fd.document_role == "MAIN"
assert fd.document_type == "Law"
assert fd.valid_metadata == {"role": ["MAIN"]}
assert pd.title == "big title1"
assert pd.source_url == "http://source1/"


def test_update_document_raises_when_metadata_required_field_none(
client: TestClient, data_db: Session, non_cclw_user_header_token
):
setup_db(data_db)

# Keep all values apart from the metadata the same.
new_document = DocumentWriteDTO(
variant_name="Original Language",
role="MAIN",
type="Law",
metadata={"role": None},
title="big title1",
source_url=cast(AnyHttpUrl, "http://source1/"),
user_language_name="English",
)

response = client.put(
"/api/v1/documents/D.0.0.1",
json=new_document.model_dump(mode="json"),
headers=non_cclw_user_header_token,
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
data = response.json()

key_text = "'role'"
expected_message = (
"Metadata validation failed: "
f"Invalid value 'None' for metadata key {key_text} expected list."
)
assert data["detail"].startswith(expected_message)
assert len(data["detail"]) == len(expected_message)

fd, pd = _get_doc_tuple(data_db, "D.0.0.1")
assert fd.import_id == "D.0.0.1"
assert fd.variant_name == "Original Language"
assert fd.document_role == "MAIN"
assert fd.document_type == "Law"
assert fd.valid_metadata == {"role": ["MAIN"]}
assert pd.title == "big title1"
assert pd.source_url == "http://source1/"


def test_update_document_remove_variant(
client: TestClient, data_db: Session, non_cclw_user_header_token
):
Expand Down

0 comments on commit d191021

Please sign in to comment.