From 75a61cbe6e370c98f9f091cef27505f0e4f7b1a6 Mon Sep 17 00:00:00 2001 From: Katy Baulch <46493669+katybaulch@users.noreply.github.com> Date: Thu, 7 Mar 2024 14:07:13 +0000 Subject: [PATCH] Pdct 820/adding a family document which we dont have a url for (#98) * Update Makefile * Only delete language if existing language is not None * Added test for update doc with idempotent language --- Makefile | 10 ++-- app/repository/document.py | 15 +++--- integration_tests/document/test_update.py | 64 +++++++++++++++++++++-- 3 files changed, 74 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 6ee2b1ad..5ffa0cf5 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/app/repository/document.py b/app/repository/document.py index 75476ab5..5425207f 100644 --- a/app/repository/document.py +++ b/app/repository/document.py @@ -232,7 +232,7 @@ 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 @@ -240,7 +240,7 @@ def update(db: Session, import_id: str, document: DocumentWriteDTO) -> bool: .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"] @@ -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( @@ -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) diff --git a/integration_tests/document/test_update.py b/integration_tests/document/test_update.py index db823b14..c3e60a97 100644 --- a/integration_tests/document/test_update.py +++ b/integration_tests/document/test_update.py @@ -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 @@ -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")