Skip to content

Commit

Permalink
fix up doc delete test
Browse files Browse the repository at this point in the history
  • Loading branch information
diversemix committed Sep 26, 2023
1 parent 6f894bb commit f18142d
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 25 deletions.
8 changes: 8 additions & 0 deletions app/repository/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ def delete(db: Session, import_id: str) -> bool:
:return bool: True if deleted False if not.
"""

found = (
db.query(FamilyDocument)
.filter(FamilyDocument.import_id == import_id)
.one_or_none()
)
if found is None:
return False

# TODO: Check the backend - I think when a document is delete the
# actual information in the physical_document is "blanked".

Expand Down
79 changes: 55 additions & 24 deletions integration_tests/document/test_delete.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,99 @@
from fastapi.testclient import TestClient
from fastapi import status
from sqlalchemy.orm import Session
from app.clients.db.models.law_policy.collection import Collection
from app.clients.db.models.law_policy import FamilyDocument
from app.clients.db.models.document import PhysicalDocument
from app.clients.db.models.law_policy.family import DocumentStatus
from integration_tests.setup_db import setup_db
import app.repository.document as document_repo


def test_delete_collection(
client: TestClient, test_db: Session, admin_user_header_token
):
def test_delete_document(client: TestClient, test_db: Session, admin_user_header_token):
setup_db(test_db)
response = client.delete(
"/api/v1/collections/C.0.0.2", headers=admin_user_header_token
"/api/v1/documents/D.0.0.2", headers=admin_user_header_token
)
assert response.status_code == status.HTTP_200_OK
n = test_db.query(Collection).count()
assert n == 1
assert test_db.query(FamilyDocument).count() == 2
assert (
test_db.query(FamilyDocument)
.filter(FamilyDocument.document_status == DocumentStatus.DELETED)
.count()
== 1
)
assert test_db.query(PhysicalDocument).count() == 2


def test_delete_collection_when_not_authenticated(client: TestClient, test_db: Session):
def test_delete_document_when_not_authenticated(
client: TestClient, test_db: Session, mocker
):
setup_db(test_db)
mocker.spy(document_repo, "delete")
response = client.delete(
"/api/v1/collections/C.0.0.2",
"/api/v1/documents/D.0.0.2",
)
assert response.status_code == status.HTTP_401_UNAUTHORIZED
assert test_db.query(FamilyDocument).count() == 2
assert (
test_db.query(FamilyDocument)
.filter(FamilyDocument.document_status == DocumentStatus.DELETED)
.count()
== 0
)
assert test_db.query(PhysicalDocument).count() == 2
assert document_repo.delete.call_count == 0


def test_delete_collection_rollback(
def test_delete_document_rollback(
client: TestClient,
test_db: Session,
rollback_collection_repo,
rollback_document_repo,
admin_user_header_token,
):
setup_db(test_db)
response = client.delete(
"/api/v1/collections/C.0.0.2", headers=admin_user_header_token
"/api/v1/documents/D.0.0.2", headers=admin_user_header_token
)
assert response.status_code == status.HTTP_503_SERVICE_UNAVAILABLE
n = test_db.query(Collection).count()
assert n == 2
assert rollback_collection_repo.delete.call_count == 1
assert test_db.query(FamilyDocument).count() == 2
assert (
test_db.query(FamilyDocument)
.filter(FamilyDocument.document_status == DocumentStatus.DELETED)
.count()
== 0
)
assert test_db.query(PhysicalDocument).count() == 2
assert rollback_document_repo.delete.call_count == 1


def test_delete_collection_when_not_found(
def test_delete_document_when_not_found(
client: TestClient, test_db: Session, admin_user_header_token
):
setup_db(test_db)
response = client.delete(
"/api/v1/collections/C.0.0.22", headers=admin_user_header_token
"/api/v1/documents/D.0.0.22", headers=admin_user_header_token
)
assert response.status_code == status.HTTP_404_NOT_FOUND
data = response.json()
assert data["detail"] == "Collection not deleted: C.0.0.22"
n = test_db.query(Collection).count()
assert n == 2
assert data["detail"] == "Document not deleted: D.0.0.22"
assert test_db.query(FamilyDocument).count() == 2
assert (
test_db.query(FamilyDocument)
.filter(FamilyDocument.document_status == DocumentStatus.DELETED)
.count()
== 0
)
assert test_db.query(PhysicalDocument).count() == 2


def test_delete_collection_when_db_error(
client: TestClient, test_db: Session, bad_collection_repo, admin_user_header_token
def test_delete_document_when_db_error(
client: TestClient, test_db: Session, bad_document_repo, admin_user_header_token
):
setup_db(test_db)
response = client.delete(
"/api/v1/collections/C.0.0.1", headers=admin_user_header_token
"/api/v1/documents/D.0.0.1", headers=admin_user_header_token
)
assert response.status_code == status.HTTP_503_SERVICE_UNAVAILABLE
data = response.json()
assert data["detail"] == "Bad Repo"
assert bad_collection_repo.delete.call_count == 1
assert bad_document_repo.delete.call_count == 1
51 changes: 50 additions & 1 deletion integration_tests/setup_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
from sqlalchemy.orm import Session
from sqlalchemy import text
from app.clients.db.models.app.users import Organisation
from app.clients.db.models.document.physical_document import PhysicalDocument
from app.clients.db.models.law_policy.collection import (
Collection,
CollectionFamily,
CollectionOrganisation,
)
from app.clients.db.models.law_policy.family import (
DocumentStatus,
Family,
FamilyCategory,
FamilyDocument,
FamilyOrganisation,
Slug,
)
Expand Down Expand Up @@ -67,7 +70,7 @@
"events": [],
"published_date": None,
"last_updated_date": None,
"documents": [],
"documents": ["D.0.0.1", "D.0.0.2"],
"collections": ["C.0.0.2"],
},
]
Expand Down Expand Up @@ -104,6 +107,9 @@ def setup_db(test_db: Session):
_setup_collection_data(test_db, org_id)
test_db.commit()

_setup_document_data(test_db, "A.0.0.3")
test_db.commit()


def _setup_organisation(test_db: Session) -> int:
# Now an organisation
Expand Down Expand Up @@ -220,3 +226,46 @@ def _setup_family_data(test_db: Session, org_id: int):
family_import_id=EXPECTED_FAMILIES[index]["import_id"],
)
)


def _setup_document_data(test_db: Session, family_id: str) -> None:
pd1 = PhysicalDocument(
id=None,
title="title1",
md5_sum="sum1",
cdn_object="obj1",
source_url="url1",
content_type="type1",
)
pd2 = PhysicalDocument(
id=None,
title="title2",
md5_sum="sum2",
cdn_object="obj2",
source_url="url2",
content_type="type2",
)
test_db.add(pd1)
test_db.add(pd2)
test_db.flush()

fd1 = FamilyDocument(
family_import_id=family_id,
physical_document_id=pd1.id,
import_id="D.0.0.1",
variant_name="Original Language",
document_status=DocumentStatus.CREATED,
document_type="Law",
document_role="MAIN",
)
fd2 = FamilyDocument(
family_import_id=family_id,
physical_document_id=pd2.id,
import_id="D.0.0.2",
variant_name="Original Language",
document_status=DocumentStatus.CREATED,
document_type="Law",
document_role="MAIN",
)
test_db.add(fd1)
test_db.add(fd2)

0 comments on commit f18142d

Please sign in to comment.