from django.test import TestCase
from wagtail.documents.models import Document

from books.migrate_document_model import migrate_documents_to_custom_document_model
from books.models.custom_document import CustomDocument

TITLE = 'T9 M2 A2: Quellencheck'


class NewContentBlockMutationTest(TestCase):
    def setUp(self):
        old_document, created = Document.objects.get_or_create(title=TITLE, file='whatever_is_green.pdf')
        migrate_documents_to_custom_document_model()

    def test_migrate_creates_new_document(self):
        self.assertEqual(Document.objects.all().count(), 1)
        self.assertEqual(Document.objects.filter(title=TITLE).count(), 1)
        self.assertEqual(CustomDocument.objects.filter(title=TITLE).count(), 1)

    def test_migrate_display_title_migration(self):
        new_document = CustomDocument.objects.get(title=TITLE)
        self.assertEqual(new_document.display_text, TITLE)

    def test_migration_is_indempodent(self):
        migrate_documents_to_custom_document_model()
        migrate_documents_to_custom_document_model()
        self.assertEqual(Document.objects.all().count(), 1)
        self.assertEqual(Document.objects.filter(title=TITLE).count(), 1)
        self.assertEqual(CustomDocument.objects.filter(title=TITLE).count(), 1)
