class TestDocumentAdmin(DirectoriesMixin, TestCase): def get_document_from_index(self, doc): ix = index.open_index() with ix.searcher() as searcher: return searcher.document(id=doc.id) def setUp(self) -> None: super(TestDocumentAdmin, self).setUp() self.doc_admin = DocumentAdmin(model=Document, admin_site=AdminSite()) def test_save_model(self): doc = Document.objects.create(title="test") doc.title = "new title" self.doc_admin.save_model(None, doc, None, None) self.assertEqual(Document.objects.get(id=doc.id).title, "new title") self.assertEqual(self.get_document_from_index(doc)['id'], doc.id) def test_delete_model(self): doc = Document.objects.create(title="test") index.add_or_update_document(doc) self.assertIsNotNone(self.get_document_from_index(doc)) self.doc_admin.delete_model(None, doc) self.assertRaises(Document.DoesNotExist, Document.objects.get, id=doc.id) self.assertIsNone(self.get_document_from_index(doc)) def test_delete_queryset(self): docs = [] for i in range(42): doc = Document.objects.create( title="Many documents with the same title", checksum=f"{i:02}") docs.append(doc) index.add_or_update_document(doc) self.assertEqual(Document.objects.count(), 42) for doc in docs: self.assertIsNotNone(self.get_document_from_index(doc)) self.doc_admin.delete_queryset(None, Document.objects.all()) self.assertEqual(Document.objects.count(), 0) for doc in docs: self.assertIsNone(self.get_document_from_index(doc)) def test_created(self): doc = Document.objects.create(title="test", created=timezone.datetime(2020, 4, 12)) self.assertEqual(self.doc_admin.created_(doc), "2020-04-12")
class TestDocumentAdmin(TestCase): def setUp(self) -> None: self.doc_admin = DocumentAdmin(model=Document, admin_site=AdminSite()) @mock.patch("documents.admin.index.add_or_update_document") def test_save_model(self, m): doc = Document.objects.create(title="test") doc.title = "new title" self.doc_admin.save_model(None, doc, None, None) self.assertEqual(Document.objects.get(id=doc.id).title, "new title") m.assert_called_once() def test_tags(self): doc = Document.objects.create(title="test") doc.tags.create(name="t1") doc.tags.create(name="t2") self.assertEqual(self.doc_admin.tags_(doc), "<span >t1, </span><span >t2, </span>") def test_tags_empty(self): doc = Document.objects.create(title="test") self.assertEqual(self.doc_admin.tags_(doc), "") @mock.patch("documents.admin.index.remove_document") def test_delete_model(self, m): doc = Document.objects.create(title="test") self.doc_admin.delete_model(None, doc) self.assertRaises(Document.DoesNotExist, Document.objects.get, id=doc.id) m.assert_called_once() @mock.patch("documents.admin.index.remove_document") def test_delete_queryset(self, m): for i in range(42): Document.objects.create(title="Many documents with the same title", checksum=f"{i:02}") self.assertEqual(Document.objects.count(), 42) self.doc_admin.delete_queryset(None, Document.objects.all()) self.assertEqual(m.call_count, 42) self.assertEqual(Document.objects.count(), 0) def test_created(self): doc = Document.objects.create(title="test", created=timezone.datetime(2020, 4, 12)) self.assertEqual(self.doc_admin.created_(doc), "2020-04-12")