Skip to content

Commit

Permalink
add cache lists tests for caching package
Browse files Browse the repository at this point in the history
  • Loading branch information
User committed Apr 25, 2024
1 parent 691a13a commit 8bbd7d2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Empty file added tests/test_caching/__init__.py
Empty file.
47 changes: 47 additions & 0 deletions tests/test_caching/test_http_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import pytest

from motleycrew.caсhing.http_cache import RequestsHttpCaching, CacheException
from motleycrew.caсhing import http_cache


@pytest.fixture
def requests_cash():
return RequestsHttpCaching()


@pytest.mark.parametrize(
"url, expected_result",
[
("https://api.lunary.ai/v1/runs/ingest", False),
("https://api.openai.com/v1/chat/completions", True),
("https://duckduckgo.com/", False),
("https://links.duckduckgo.com/d.j", False),
],
)
def test_white_list(requests_cash, url, expected_result):
http_cache.CACHE_WHITELIST = ["*//api.openai.com/v1/*"]
http_cache.CACHE_BLACKLIST = []
assert requests_cash.should_cache(url) == expected_result


@pytest.mark.parametrize(
"url, expected_result",
[
("https://api.lunary.ai/v1/runs/ingest", False),
("https://api.openai.com/v1/chat/completions", True),
("https://duckduckgo.com/", True),
("https://links.duckduckgo.com/d.j", False),
],
)
def test_black_list(requests_cash, url, expected_result):
http_cache.CACHE_WHITELIST = []
http_cache.CACHE_BLACKLIST = ["*//api.lunary.ai/v1/*", "*//links.duckduckgo.com/*"]
assert requests_cash.should_cache(url) == expected_result


def test_raise_cache_lists(requests_cash):
http_cache.CACHE_WHITELIST = ["*//links.duckduckgo.com/*"]
http_cache.CACHE_BLACKLIST = ["*//api.lunary.ai/v1/*"]
url = "https://api.openai.com/v1/chat/completions"
with pytest.raises(CacheException):
requests_cash.should_cache(url)

0 comments on commit 8bbd7d2

Please sign in to comment.