# Path: elasticsearch_django/apps.py
# def _on_model_save(sender: type[Model], **kwargs: Any) -> None:
#     """Update document in search index post_save."""
#     instance = kwargs.pop("instance")
#     update_fields = kwargs.pop("update_fields")
#     for index in instance.search_indexes:
#         try:
#             _update_search_index(
#                 instance=instance, index=index, update_fields=update_fields
#             )
#         except Exception:  # noqa: B902
#             logger.exception("Error handling 'on_save' signal for %s", instance)
# 
# Path: elasticsearch_django/decorators.py
# def _strip_on_model_save() -> list[Callable]:
#     """Return list of signal receivers without _on_model_save."""
#     return [r for r in signals.post_save.receivers if r[1]() != _on_model_save]
# 
# @contextmanager
# def disable_search_updates() -> Generator:
#     """
#     Context manager used to temporarily disable auto_sync.
# 
#     This is useful when performing bulk updates on objects - when
#     you may not want to flood the indexing process.
# 
#     >>> with disable_search_updates():
#     ...     for obj in model.objects.all():
#     ...     obj.save()
# 
#     The function works by temporarily removing the apps._on_model_save
#     signal handler from the model.post_save signal receivers, and then
#     restoring them after.
# 
#     """
#     _receivers = signals.post_save.receivers.copy()
#     signals.post_save.receivers = _strip_on_model_save()
#     yield
#     signals.post_save.receivers = _receivers


from django.db.models import signals
from django.test import TestCase
from elasticsearch_django.apps import _on_model_save
from elasticsearch_django.decorators import _strip_on_model_save, disable_search_updates



class DecoratorTests(TestCase):
    def setUp(self):
        signals.post_save.connect(_on_model_save)

    def tearDown(self):
        signals.post_save.disconnect(_on_model_save)

    def test_disable_updates(self):
        """Check the decorator removes _on_model_save from signal receivers."""
        self.assertNotEqual(signals.post_save.receivers, [])

# Path: elasticsearch_django/apps.py
# def _on_model_save(sender: type[Model], **kwargs: Any) -> None:
#     """Update document in search index post_save."""
#     instance = kwargs.pop("instance")
#     update_fields = kwargs.pop("update_fields")
#     for index in instance.search_indexes:
#         try:
#             _update_search_index(
#                 instance=instance, index=index, update_fields=update_fields
#             )
#         except Exception:  # noqa: B902
#             logger.exception("Error handling 'on_save' signal for %s", instance)
# 
# Path: elasticsearch_django/decorators.py
# def _strip_on_model_save() -> list[Callable]:
#     """Return list of signal receivers without _on_model_save."""
#     return [r for r in signals.post_save.receivers if r[1]() != _on_model_save]
# 
# @contextmanager
# def disable_search_updates() -> Generator:
#     """
#     Context manager used to temporarily disable auto_sync.
# 
#     This is useful when performing bulk updates on objects - when
#     you may not want to flood the indexing process.
# 
#     >>> with disable_search_updates():
#     ...     for obj in model.objects.all():
#     ...     obj.save()
# 
#     The function works by temporarily removing the apps._on_model_save
#     signal handler from the model.post_save signal receivers, and then
#     restoring them after.
# 
#     """
#     _receivers = signals.post_save.receivers.copy()
#     signals.post_save.receivers = _strip_on_model_save()
#     yield
#     signals.post_save.receivers = _receivers


# Path: tests/test_decorators.py
from django.db.models import signals
from django.test import TestCase
from elasticsearch_django.apps import _on_model_save
from elasticsearch_django.decorators import _strip_on_model_save, disable_search_updates




class DecoratorTests(TestCase):
    def setUp(self):
        signals.post_save.connect(_on_model_save)

    def tearDown(self):
        signals.post_save.disconnect(_on_model_save)

    def test_disable_updates(self):
        """Check the decorator removes _on_model_save from signal receivers."""
        self.assertNotEqual(signals.post_save.receivers, [])

        self.assertEqual(signals.post_save.receivers[0][1](), _on_model_save)