2222
2323from core import factories , models
2424from core .services .search_indexers import SearchIndexer
25+ from core .tasks .search import document_indexer_task
2526
2627pytestmark = pytest .mark .django_db
2728
@@ -1473,10 +1474,10 @@ def test_models_documents_post_save_indexer(mock_push, indexer_settings):
14731474 key = itemgetter ("id" ),
14741475 )
14751476
1476- # The debounce counters should be reset
1477- assert cache .get (f"doc-indexer-debounce -{ doc1 .pk } " ) == 0
1478- assert cache .get (f"doc-indexer-debounce -{ doc2 .pk } " ) == 0
1479- assert cache .get (f"doc-indexer-debounce -{ doc3 .pk } " ) == 0
1477+ # The throttle counters should be reset
1478+ assert cache .get (f"doc-indexer-throttle -{ doc1 .pk } " ) is None
1479+ assert cache .get (f"doc-indexer-throttle -{ doc2 .pk } " ) is None
1480+ assert cache .get (f"doc-indexer-throttle -{ doc3 .pk } " ) is None
14801481
14811482
14821483@mock .patch .object (SearchIndexer , "push" )
@@ -1486,10 +1487,31 @@ def test_models_documents_post_save_indexer_not_configured(mock_push, indexer_se
14861487 indexer_settings .SEARCH_INDEXER_COUNTDOWN = 0
14871488 indexer_settings .SEARCH_INDEXER_CLASS = None
14881489
1490+ user = factories .UserFactory ()
1491+
1492+ with transaction .atomic ():
1493+ doc = factories .DocumentFactory ()
1494+ factories .UserDocumentAccessFactory (document = doc , user = user )
1495+
1496+ assert mock_push .assert_not_called
1497+
1498+
1499+ @mock .patch .object (SearchIndexer , "push" )
1500+ @pytest .mark .django_db (transaction = True )
1501+ def test_models_documents_post_save_indexer_wrongly_configured (
1502+ mock_push , indexer_settings
1503+ ):
1504+ """Task should not start an indexation when disabled"""
1505+ indexer_settings .SEARCH_INDEXER_COUNTDOWN = 0
1506+ indexer_settings .SEARCH_INDEXER_URL = None
1507+
1508+ user = factories .UserFactory ()
1509+
14891510 with transaction .atomic ():
1490- factories .DocumentFactory ()
1511+ doc = factories .DocumentFactory ()
1512+ factories .UserDocumentAccessFactory (document = doc , user = user )
14911513
1492- assert mock_push .call_args_list == []
1514+ assert mock_push .assert_not_called
14931515
14941516
14951517@mock .patch .object (SearchIndexer , "push" )
@@ -1526,10 +1548,10 @@ def test_models_documents_post_save_indexer_with_accesses(mock_push, indexer_set
15261548 key = itemgetter ("id" ),
15271549 )
15281550
1529- # The debounce counters should be reset
1530- assert cache .get (f"doc-indexer-debounce -{ doc1 .pk } " ) == 0
1531- assert cache .get (f"doc-indexer-debounce -{ doc2 .pk } " ) == 0
1532- assert cache .get (f"doc-indexer-debounce -{ doc3 .pk } " ) == 0
1551+ # The throttle counters should be reset
1552+ assert cache .get (f"doc-indexer-throttle -{ doc1 .pk } " ) is None
1553+ assert cache .get (f"doc-indexer-throttle -{ doc2 .pk } " ) is None
1554+ assert cache .get (f"doc-indexer-throttle -{ doc3 .pk } " ) is None
15331555
15341556
15351557@mock .patch .object (SearchIndexer , "push" )
@@ -1588,10 +1610,34 @@ def test_models_documents_post_save_indexer_deleted(mock_push, indexer_settings)
15881610 key = itemgetter ("id" ),
15891611 )
15901612
1591- # The debounce counters should be reset
1592- assert cache .get (f"doc-indexer-debounce-{ doc .pk } " ) == 0
1593- assert cache .get (f"doc-indexer-debounce-{ doc_deleted .pk } " ) == 0
1594- assert cache .get (f"doc-indexer-debounce-{ doc_ancestor_deleted .pk } " ) == 0
1613+ # The throttle counters should be reset
1614+ assert cache .get (f"doc-indexer-throttle-{ doc .pk } " ) is None
1615+ assert cache .get (f"doc-indexer-throttle-{ doc_deleted .pk } " ) is None
1616+ assert cache .get (f"doc-indexer-throttle-{ doc_ancestor_deleted .pk } " ) is None
1617+
1618+
1619+ @pytest .mark .django_db (transaction = True )
1620+ def test_models_documents_indexer_hard_deleted (indexer_settings ):
1621+ """Indexation task on hard deleted document"""
1622+ indexer_settings .SEARCH_INDEXER_COUNTDOWN = 0
1623+
1624+ user = factories .UserFactory ()
1625+
1626+ with transaction .atomic ():
1627+ doc = factories .DocumentFactory (
1628+ link_reach = models .LinkReachChoices .AUTHENTICATED
1629+ )
1630+ factories .UserDocumentAccessFactory (document = doc , user = user )
1631+
1632+ doc_id = doc .pk
1633+ doc .delete ()
1634+
1635+ # Call task on deleted document.
1636+ document_indexer_task .apply (args = [doc_id ])
1637+
1638+ with mock .patch .object (SearchIndexer , "push" ) as mock_push :
1639+ # Hard delete document are not re-indexed.
1640+ assert mock_push .assert_not_called
15951641
15961642
15971643@mock .patch .object (SearchIndexer , "push" )
@@ -1664,7 +1710,7 @@ def test_models_documents_post_save_indexer_restored(mock_push, indexer_settings
16641710
16651711
16661712@pytest .mark .django_db (transaction = True )
1667- def test_models_documents_post_save_indexer_debounce (indexer_settings ):
1713+ def test_models_documents_post_save_indexer_throttle (indexer_settings ):
16681714 """Test indexation task skipping on document update"""
16691715 indexer_settings .SEARCH_INDEXER_COUNTDOWN = 0
16701716
@@ -1681,19 +1727,19 @@ def test_models_documents_post_save_indexer_debounce(indexer_settings):
16811727 }
16821728
16831729 with mock .patch .object (SearchIndexer , "push" ) as mock_push :
1684- # Simulate 1 waiting task
1685- cache .set (f"doc-indexer-debounce -{ doc .pk } " , 1 )
1730+ # Simulate 1 running task
1731+ cache .set (f"doc-indexer-throttle -{ doc .pk } " , 1 )
16861732
16871733 # save doc to trigger the indexer, but nothing should be done since
1688- # the counter is over 0
1734+ # the flag is up
16891735 with transaction .atomic ():
16901736 doc .save ()
16911737
16921738 assert [call .args [0 ] for call in mock_push .call_args_list ] == []
16931739
16941740 with mock .patch .object (SearchIndexer , "push" ) as mock_push :
16951741 # No waiting task
1696- cache .set (f"doc-indexer-debounce -{ doc .pk } " , 0 )
1742+ cache .delete (f"doc-indexer-throttle -{ doc .pk } " )
16971743
16981744 with transaction .atomic ():
16991745 doc = models .Document .objects .get (pk = doc .pk )
0 commit comments