Skip to content

Commit

Permalink
PDCT-954 fix the fields, str conversion and add new test (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
asacristani authored Mar 11, 2024
1 parent 75a61cb commit fceac82
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
6 changes: 3 additions & 3 deletions app/model/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DocumentReadDTO(BaseModel):
title: str
md5_sum: Optional[str]
cdn_object: Optional[str]
source_url: Optional[AnyHttpUrl]
source_url: Optional[AnyHttpUrl] = None
content_type: Optional[str]
user_language_name: Optional[str]
calc_language_name: Optional[str]
Expand All @@ -40,7 +40,7 @@ class DocumentWriteDTO(BaseModel):
role: Optional[str]
type: Optional[str]
title: str
source_url: Optional[AnyHttpUrl]
source_url: Optional[AnyHttpUrl] = None
user_language_name: Optional[str]


Expand All @@ -55,7 +55,7 @@ class DocumentCreateDTO(BaseModel):

# From PhysicalDocument
title: str
source_url: Optional[AnyHttpUrl]
source_url: Optional[AnyHttpUrl] = None
user_language_name: Optional[str]


Expand Down
4 changes: 3 additions & 1 deletion app/repository/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ def update(db: Session, import_id: str, document: DocumentWriteDTO) -> bool:
.where(PhysicalDocument.id == original_pd.id)
.values(
title=new_values["title"],
source_url=str(new_values["source_url"]),
source_url=str(new_values["source_url"])
if new_values["source_url"] is not None
else None,
),
db_update(FamilyDocument)
.where(FamilyDocument.import_id == original_fd.import_id)
Expand Down
53 changes: 53 additions & 0 deletions integration_tests/document/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,59 @@ def test_update_document(client: TestClient, test_db: Session, user_header_token
assert last_slug.startswith("updated-title")


def test_update_document_no_source_url(
client: TestClient, test_db: Session, user_header_token
):
setup_db(test_db)
new_document = DocumentWriteDTO(
variant_name="Translation",
role="SUMMARY",
type="Annex",
title="Updated Title No Source URL",
source_url=None,
user_language_name="Ghotuo",
)
response = client.put(
"/api/v1/documents/D.0.0.2",
json=new_document.model_dump(mode="json"),
headers=user_header_token,
)
assert response.status_code == status.HTTP_200_OK
data = response.json()
assert data["import_id"] == "D.0.0.2"
assert data["variant_name"] == "Translation"
assert data["role"] == "SUMMARY"
assert data["type"] == "Annex"
assert data["title"] == new_document.title
assert data["source_url"] == new_document.source_url
assert data["slug"].startswith("updated-title")
assert data["user_language_name"] == "Ghotuo"

fd, pd = _get_doc_tuple(test_db, "D.0.0.2")
assert fd.import_id == "D.0.0.2"
assert fd.variant_name == "Translation"
assert fd.document_role == "SUMMARY"
assert fd.document_type == "Annex"
assert pd.title == new_document.title
assert pd.source_url == new_document.source_url

# Check the user language in the db
lang = (
test_db.query(PhysicalDocumentLanguage)
.filter(PhysicalDocumentLanguage.document_id == data["physical_id"])
.filter(PhysicalDocumentLanguage.source == LanguageSource.USER)
.one()
)
assert lang.language_id == 1

# Check slug is updated too
slugs = (
test_db.query(Slug).filter(Slug.family_document_import_id == "D.0.0.2").all()
)
last_slug = slugs[-1].name
assert last_slug.startswith("updated-title")


def test_update_document_remove_variant(
client: TestClient, test_db: Session, user_header_token
):
Expand Down

0 comments on commit fceac82

Please sign in to comment.