|
| 1 | +""" |
| 2 | +Unit test for `index` command. |
| 3 | +""" |
| 4 | + |
| 5 | +from unittest import mock |
| 6 | + |
| 7 | +from django.core.management import call_command |
| 8 | +from django.db import transaction |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from core import factories |
| 13 | +from core.services.search_indexers import FindDocumentIndexer |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.django_db |
| 17 | +def test_index(): |
| 18 | + """Test the command `index` that run the Find app indexer for all the available documents.""" |
| 19 | + user = factories.UserFactory() |
| 20 | + indexer = FindDocumentIndexer() |
| 21 | + |
| 22 | + with transaction.atomic(): |
| 23 | + doc = factories.DocumentFactory() |
| 24 | + empty_doc = factories.DocumentFactory(title=None, content='') |
| 25 | + no_title_doc = factories.DocumentFactory(title=None) |
| 26 | + |
| 27 | + factories.UserDocumentAccessFactory(document=doc, user=user) |
| 28 | + factories.UserDocumentAccessFactory(document=empty_doc, user=user) |
| 29 | + factories.UserDocumentAccessFactory(document=no_title_doc, user=user) |
| 30 | + |
| 31 | + accesses = { |
| 32 | + str(doc.path): {"users": [user.sub]}, |
| 33 | + str(empty_doc.path): {"users": [user.sub]}, |
| 34 | + str(no_title_doc.path): {"users": [user.sub]}, |
| 35 | + } |
| 36 | + |
| 37 | + def sortkey(d): |
| 38 | + return d["id"] |
| 39 | + |
| 40 | + with mock.patch.object(FindDocumentIndexer, "push") as mock_push: |
| 41 | + call_command("index") |
| 42 | + |
| 43 | + push_call_args = [call.args[0] for call in mock_push.call_args_list] |
| 44 | + |
| 45 | + assert len(push_call_args) == 1 # called once but with a batch of docs |
| 46 | + assert sorted(push_call_args[0], key=sortkey) == sorted([ |
| 47 | + indexer.serialize_document(doc, accesses), |
| 48 | + indexer.serialize_document(no_title_doc, accesses), |
| 49 | + ], key=sortkey) |
0 commit comments