Fix tests

This commit is contained in:
shamoon 2025-02-13 19:48:29 -08:00
parent b3d49dbf12
commit abfddd6931
2 changed files with 62 additions and 71 deletions

View File

@ -1,18 +1,14 @@
import os import os
import tempfile
from pathlib import Path from pathlib import Path
from unittest import mock from unittest import mock
from celery import states
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.test import override_settings from django.test import override_settings
from rest_framework import status from rest_framework import status
from rest_framework.test import APITestCase from rest_framework.test import APITestCase
from documents.classifier import ClassifierModelCorruptError from documents.models import PaperlessTask
from documents.classifier import DocumentClassifier
from documents.classifier import load_classifier
from documents.models import Document
from documents.models import Tag
from paperless import version from paperless import version
@ -193,7 +189,6 @@ class TestSystemStatus(APITestCase):
self.assertEqual(response.data["tasks"]["index_status"], "ERROR") self.assertEqual(response.data["tasks"]["index_status"], "ERROR")
self.assertIsNotNone(response.data["tasks"]["index_error"]) self.assertIsNotNone(response.data["tasks"]["index_error"])
@override_settings(DATA_DIR=Path("/tmp/does_not_exist/data/"))
def test_system_status_classifier_ok(self): def test_system_status_classifier_ok(self):
""" """
GIVEN: GIVEN:
@ -203,85 +198,81 @@ class TestSystemStatus(APITestCase):
THEN: THEN:
- The response contains an OK classifier status - The response contains an OK classifier status
""" """
load_classifier() PaperlessTask.objects.create(
test_classifier = DocumentClassifier() type=PaperlessTask.TaskType.SCHEDULED_TASK,
test_classifier.save() status=states.SUCCESS,
task_name="train_classifier",
)
self.client.force_login(self.user) self.client.force_login(self.user)
response = self.client.get(self.ENDPOINT) response = self.client.get(self.ENDPOINT)
self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["tasks"]["classifier_status"], "OK") self.assertEqual(response.data["tasks"]["classifier_status"], "OK")
self.assertIsNone(response.data["tasks"]["classifier_error"]) self.assertIsNone(response.data["tasks"]["classifier_error"])
def test_system_status_classifier_warning(self): def test_system_status_classifier_error(self):
""" """
GIVEN: GIVEN:
- The classifier does not exist yet - An error occurred while loading the classifier
- > 0 documents and tags with auto matching exist
WHEN:
- The user requests the system status
THEN:
- The response contains an WARNING classifier status
"""
with override_settings(MODEL_FILE=Path("does_not_exist")):
Document.objects.create(
title="Test Document",
)
Tag.objects.create(name="Test Tag", matching_algorithm=Tag.MATCH_AUTO)
self.client.force_login(self.user)
response = self.client.get(self.ENDPOINT)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["tasks"]["classifier_status"], "WARNING")
self.assertIsNotNone(response.data["tasks"]["classifier_error"])
@mock.patch(
"documents.classifier.load_classifier",
side_effect=ClassifierModelCorruptError(),
)
def test_system_status_classifier_error(self, mock_load_classifier):
"""
GIVEN:
- The classifier does exist but is corrupt
- > 0 documents and tags with auto matching exist
WHEN: WHEN:
- The user requests the system status - The user requests the system status
THEN: THEN:
- The response contains an ERROR classifier status - The response contains an ERROR classifier status
""" """
with ( PaperlessTask.objects.create(
tempfile.NamedTemporaryFile( type=PaperlessTask.TaskType.SCHEDULED_TASK,
dir="/tmp", status=states.FAILURE,
delete=False, task_name="train_classifier",
) as does_exist, result="Classifier training failed",
override_settings(MODEL_FILE=Path(does_exist.name)), )
): self.client.force_login(self.user)
Document.objects.create( response = self.client.get(self.ENDPOINT)
title="Test Document", self.assertEqual(response.status_code, status.HTTP_200_OK)
) self.assertEqual(
Tag.objects.create( response.data["tasks"]["classifier_status"],
name="Test Tag", "ERROR",
matching_algorithm=Tag.MATCH_AUTO, )
) self.assertIsNotNone(response.data["tasks"]["classifier_error"])
self.client.force_login(self.user)
response = self.client.get(self.ENDPOINT)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.data["tasks"]["classifier_status"],
"ERROR",
)
self.assertIsNotNone(response.data["tasks"]["classifier_error"])
def test_system_status_classifier_ok_no_objects(self): def test_system_status_sanity_check_ok(self):
""" """
GIVEN: GIVEN:
- The classifier does not exist (and should not) - The sanity check is successful
- No documents nor objects with auto matching exist
WHEN: WHEN:
- The user requests the system status - The user requests the system status
THEN: THEN:
- The response contains an OK classifier status - The response contains an OK sanity check status
""" """
with override_settings(MODEL_FILE=Path("does_not_exist")): PaperlessTask.objects.create(
self.client.force_login(self.user) type=PaperlessTask.TaskType.SCHEDULED_TASK,
response = self.client.get(self.ENDPOINT) status=states.SUCCESS,
self.assertEqual(response.status_code, status.HTTP_200_OK) task_name="check_sanity",
self.assertEqual(response.data["tasks"]["classifier_status"], "OK") )
self.client.force_login(self.user)
response = self.client.get(self.ENDPOINT)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["tasks"]["sanity_check_status"], "OK")
self.assertIsNone(response.data["tasks"]["sanity_check_error"])
def test_system_status_sanity_check_error(self):
"""
GIVEN:
- The sanity check failed
WHEN:
- The user requests the system status
THEN:
- The response contains an ERROR sanity check status
"""
PaperlessTask.objects.create(
type=PaperlessTask.TaskType.SCHEDULED_TASK,
status=states.FAILURE,
task_name="check_sanity",
result="5 issues found.",
)
self.client.force_login(self.user)
response = self.client.get(self.ENDPOINT)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.data["tasks"]["sanity_check_status"],
"ERROR",
)
self.assertIsNotNone(response.data["tasks"]["sanity_check_error"])

View File

@ -2674,7 +2674,7 @@ class SystemStatusView(PassUserMixin):
else "ERROR" else "ERROR"
) )
classifier_error = None classifier_error = None
if last_trained_task.status == states.FAILURE: if last_trained_task and last_trained_task.status == states.FAILURE:
classifier_error = last_trained_task.result classifier_error = last_trained_task.result
classifier_last_trained = ( classifier_last_trained = (
last_trained_task.date_done if last_trained_task else None last_trained_task.date_done if last_trained_task else None
@ -2695,7 +2695,7 @@ class SystemStatusView(PassUserMixin):
else "ERROR" else "ERROR"
) )
sanity_check_error = None sanity_check_error = None
if last_sanity_check.status == states.FAILURE: if last_sanity_check and last_sanity_check.status == states.FAILURE:
sanity_check_error = last_sanity_check.result sanity_check_error = last_sanity_check.result
sanity_check_last_run = ( sanity_check_last_run = (
last_sanity_check.date_done if last_sanity_check else None last_sanity_check.date_done if last_sanity_check else None