"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 07.09.23

"""
from django.core.exceptions import ObjectDoesNotExist
from django.utils.timezone import now

from intervention.models import RevocationDocument, Revocation
from konova.tests.test_views import BaseTestCase


class RevocationDocumentTestCase(BaseTestCase):
    def setUp(self) -> None:
        super().setUp()
        self.revocation = Revocation.objects.get_or_create(
            date=now().date(),
            comment="Test",
            legal=self.intervention.legal
        )[0]
        self.doc = self.create_dummy_document(
            RevocationDocument,
            instance=self.revocation
        )

    def test_intervention_property(self):
        self.assertEqual(
            self.doc.intervention,
            self.doc.instance.legal.intervention
        )
        self.assertEqual(
            self.doc.intervention,
            self.intervention
        )

    def test_delete(self):
        revoc_docs, other_intervention_docs = self.intervention.get_documents()
        self.assertIn(self.doc, revoc_docs)

        try:
            self.doc.delete()
            self.doc.refresh_from_db()
            self.fail("Should not be fetchable anymore!")
        except ObjectDoesNotExist:
            pass

        revoc_docs, other_intervention_docs = self.intervention.get_documents()
        self.assertEqual(revoc_docs.count(), 0)
