import sys import unittest from unittest.mock import patch # tests/test_api_endpoints.py installs a fake "services.cache_service" module # into sys.modules for its own route tests. If that file gets imported first # in this process (e.g. `pytest tests/`), evict the fake here so this import # triggers a genuine fresh load of the real module instead of reusing it. sys.modules.pop("services.cache_service", None) from core.cache import instant_reply_cache # noqa: E402 from services.cache_service import clear_product_result_cache_for_slug # noqa: E402 class CacheServiceTests(unittest.TestCase): def setUp(self): instant_reply_cache.clear() def tearDown(self): instant_reply_cache.clear() def test_clear_product_result_cache_for_slug_clears_workspace_channel_identifiers(self): instant_reply_cache.set("query:replyflow-test:old-slug-entry", "keep") instant_reply_cache.set("query:replyflow.myshopify.com:stock-question", "delete") instant_reply_cache.set("query:replyflow-instagram:shipping-question", "delete") instant_reply_cache.set("query:other-shop.myshopify.com:stock-question", "keep") with ( patch( "services.cache_service.get_workspace_id_by_slug", return_value="workspace-123", ) as get_workspace_id_by_slug, patch( "services.cache_service.list_channel_identifiers_for_workspace", return_value=[ "replyflow.myshopify.com", "replyflow-instagram", ], ) as list_channel_identifiers_for_workspace, ): deleted = clear_product_result_cache_for_slug("replyflow-test") self.assertEqual(deleted, 2) get_workspace_id_by_slug.assert_called_once_with("replyflow-test") list_channel_identifiers_for_workspace.assert_called_once_with("workspace-123") self.assertEqual(instant_reply_cache.get("query:replyflow-test:old-slug-entry"), "keep") self.assertIsNone(instant_reply_cache.get("query:replyflow.myshopify.com:stock-question")) self.assertIsNone(instant_reply_cache.get("query:replyflow-instagram:shipping-question")) self.assertEqual( instant_reply_cache.get("query:other-shop.myshopify.com:stock-question"), "keep", ) if __name__ == "__main__": unittest.main()