-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f894bb
commit f18142d
Showing
3 changed files
with
113 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters