Skip to content

Commit

Permalink
Pdct 820/adding a family document which we dont have a url for (#98)
Browse files Browse the repository at this point in the history
* Update Makefile

* Only delete language if existing language is not None

* Added test for update doc with idempotent language
  • Loading branch information
katybaulch authored Mar 7, 2024
1 parent 9fdd0d0 commit 75a61cb
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 15 deletions.
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ integration_test:
test: unit_test integration_test

run:
docker-compose -f docker-compose.yml up -d --remove-orphans
docker compose -f docker-compose.yml up -d --remove-orphans

start: build_dev run

restart:
docker stop navigator-admin-backend && docker rm navigator-admin-backend && make start && docker logs -f navigator-admin-backend

start: build run

show_logs:
- docker-compose logs -f
- docker compose logs -f
15 changes: 8 additions & 7 deletions app/repository/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,15 @@ def update(db: Session, import_id: str, document: DocumentWriteDTO) -> bool:
return False

# User Language changed?
pdl = (
existing_language = (
db.query(PhysicalDocumentLanguage)
.filter(
PhysicalDocumentLanguage.document_id == original_fd.physical_document_id
)
.filter(PhysicalDocumentLanguage.source == LanguageSource.USER)
.one_or_none()
)
new_language = _get_new_language(db, new_values, pdl)
new_language = _get_new_language(db, new_values, existing_language)

update_slug = original_pd.title != new_values["title"]

Expand All @@ -261,7 +261,7 @@ def update(db: Session, import_id: str, document: DocumentWriteDTO) -> bool:
]

if new_language is not None:
if pdl is not None:
if existing_language is not None:
command = (
db_update(PhysicalDocumentLanguage)
.where(
Expand All @@ -281,10 +281,11 @@ def update(db: Session, import_id: str, document: DocumentWriteDTO) -> bool:
)
commands.append(command)
else:
command = db_delete(PhysicalDocumentLanguage).where(
PhysicalDocumentLanguage.document_id == original_fd.physical_document_id
)
commands.append(command)
if existing_language is not None:
command = db_delete(PhysicalDocumentLanguage).where(
PhysicalDocumentLanguage.document_id == original_fd.physical_document_id
)
commands.append(command)

for c in commands:
result = db.execute(c)
Expand Down
64 changes: 59 additions & 5 deletions integration_tests/document/test_update.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from typing import Tuple, cast

from fastapi import status
from fastapi.testclient import TestClient
from pydantic import AnyHttpUrl
from sqlalchemy.orm import Session

from db_client.models.document.physical_document import (
LanguageSource,
PhysicalDocument,
PhysicalDocumentLanguage,
)
from db_client.models.law_policy.family import FamilyDocument, Slug
from fastapi import status
from fastapi.testclient import TestClient
from pydantic import AnyHttpUrl
from sqlalchemy.orm import Session

from app.model.document import DocumentWriteDTO
from integration_tests.setup_db import EXPECTED_DOCUMENTS, setup_db
from unit_tests.helpers.document import create_document_write_dto
Expand Down Expand Up @@ -308,3 +308,57 @@ def test_update_document_blank_variant(
assert response.status_code == status.HTTP_400_BAD_REQUEST
data = response.json()
assert data["detail"] == "Variant name is empty"


def test_update_document_idempotent_user_language(
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",
source_url=cast(AnyHttpUrl, "http://update_source"),
user_language_name=None,
)
print(new_document.model_dump(mode="json"))
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"] == "Updated Title"
assert data["source_url"] == "http://update_source/"
assert data["slug"].startswith("updated-title")
assert data["user_language_name"] is None

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 == "Updated Title"
assert pd.source_url == "http://update_source/"

# 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_or_none()
)
assert lang is None

# 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")

0 comments on commit 75a61cb

Please sign in to comment.