File size: 2,423 Bytes
a9d7074
7c61e48
 
 
a9d7074
 
 
 
 
 
 
 
7c61e48
 
 
 
becf772
7c61e48
 
becf772
7c61e48
becf772
 
 
 
 
7c61e48
 
 
 
 
 
 
 
 
 
 
 
 
 
becf772
7c61e48
 
 
 
becf772
 
 
7c61e48
becf772
7c61e48
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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()