Skip to content

Commit

Permalink
refactor(maintainability): dry
Browse files Browse the repository at this point in the history
  • Loading branch information
helmut-hoffer-von-ankershoffen committed Dec 28, 2024
1 parent 21ba5ec commit b2f2f2b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 54 deletions.
7 changes: 4 additions & 3 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

_INSTALL_ARGS = "-e .[dev,imaging]"
_INSTALL_NO_EXTRAS_ARGS = "-e .[dev]"
_JUNITXML_ARG = "--junitxml=junit.xml"


@nox.session(python=["3.11", "3.12", "3.13"])
Expand All @@ -17,7 +18,7 @@ def test(session: nox.Session):
session.run(
"pytest",
"--disable-warnings",
"--junitxml=junit.xml",
_JUNITXML_ARG,
"-n",
"auto",
"--dist",
Expand All @@ -29,7 +30,7 @@ def test(session: nox.Session):
"pytest",
"--cov-append",
"--disable-warnings",
"--junitxml=junit.xml",
_JUNITXML_ARG,
"-n",
"auto",
"--dist",
Expand All @@ -52,7 +53,7 @@ def test_no_extras(session: nox.Session):
"pytest",
"--cov-append",
"--disable-warnings",
"--junitxml=junit.xml",
_JUNITXML_ARG,
"-n",
"1",
"-m",
Expand Down
9 changes: 5 additions & 4 deletions tests/confluence/starbridge_confluence_cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
MOCK_GET_SPACE = "atlassian.Confluence.get_space"
MOCK_GET_ALL_PAGES_FROM_SPACE = "atlassian.Confluence.get_all_pages_from_space"
MOCK_GET_PAGE_BY_ID = "atlassian.Confluence.get_page_by_id"
FIXTURE_GET_ALL_SPACES = "tests/fixtures/get_all_spaces.json"


@pytest.fixture
Expand All @@ -22,7 +23,7 @@ def runner():
def test_confluence_health(mock_get_all_spaces, runner):
"""Check health."""
# Mock the response data that would come from get_all_spaces
with Path("tests/fixtures/get_all_spaces.json").open() as f:
with Path(FIXTURE_GET_ALL_SPACES).open() as f:
mock_get_all_spaces.return_value = json.loads(f.read())

result = runner.invoke(cli, ["confluence", "health"])
Expand All @@ -40,7 +41,7 @@ def test_confluence_info(runner):
def test_confluence_mcp_resources(mock_get_all_spaces, runner):
"""Check fetching resources."""
# Mock the response data that would come from get_all_spaces
with Path("tests/fixtures/get_all_spaces.json").open() as f:
with Path(FIXTURE_GET_ALL_SPACES).open() as f:
mock_get_all_spaces.return_value = json.loads(f.read())

result = runner.invoke(cli, ["confluence", "mcp", "resources"])
Expand Down Expand Up @@ -80,7 +81,7 @@ def test_confluence_mcp_prompts(runner):
def test_confluence_mcp_space_summary(mock_get_all_spaces, runner):
"""Check space list."""
# Mock the response data that would come from get_all_spaces
with Path("tests/fixtures/get_all_spaces.json").open() as f:
with Path(FIXTURE_GET_ALL_SPACES).open() as f:
mock_get_all_spaces.return_value = json.loads(f.read())

result = runner.invoke(
Expand All @@ -94,7 +95,7 @@ def test_confluence_mcp_space_summary(mock_get_all_spaces, runner):
def test_confluence_space_list(mock_get_all_spaces, runner):
"""Check space list."""
# Mock the response data that would come from get_all_spaces
with Path("tests/fixtures/get_all_spaces.json").open() as f:
with Path(FIXTURE_GET_ALL_SPACES).open() as f:
mock_get_all_spaces.return_value = json.loads(f.read())

result = runner.invoke(cli, ["confluence", "space", "list"])
Expand Down
7 changes: 4 additions & 3 deletions tests/web/starbridge_web_cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from starbridge.cli import cli

GET_TEST_URL = "https://helmuthva.gitbook.io/starbridge"
GET_LLMS_TXT_URL = "https://docs.anthropic.com"


@pytest.fixture
Expand Down Expand Up @@ -157,7 +158,7 @@ def test_web_cli_get_additional_context_llms_text(runner):
"get",
"--format",
"text",
"https://docs.anthropic.com/",
GET_LLMS_TXT_URL,
],
)
assert "Get Api Key" in json.loads(result.output)["context"]["llms_txt"]
Expand All @@ -176,7 +177,7 @@ def test_web_cli_get_additional_context_llms_full_txt(runner):
"--format",
"text",
"--llms-full-txt",
"https://docs.anthropic.com/",
GET_LLMS_TXT_URL,
],
)
assert "Get Api Key" in json.loads(result.output)["context"]["llms_txt"]
Expand All @@ -195,7 +196,7 @@ def test_web_cli_get_additional_context_not(runner):
"--format",
"text",
"--no-additional-context",
"https://docs.anthropic.com/",
GET_LLMS_TXT_URL,
],
)
assert hasattr(json.loads(result.output), "context") is False
Expand Down
84 changes: 40 additions & 44 deletions tests/web/starbridge_web_util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,85 +3,85 @@

import pytest
from httpx import TimeoutException
from nox import project

from starbridge import __project_name__
from starbridge.web import RobotForbiddenException
from starbridge.web.utils import ensure_allowed_to_crawl, get_additional_context

GET_TEST_URL = "https://helmuthva.gitbook.io/starbridge"
HTTPX_ASYNC_CLIENT_GET = "httpx.AsyncClient.get"
TIMEOUT_MESSAGE = "Connection timed out"
LLMS_TXT_URL = "https://docs.anthropic.com"
LLMS_TXT = "llms_txt"
LLMS_FULL_TXT = "llms-full.txt"
LLMS_DUMY_CONTENT = "llms content"


def test_web_util_ensure_allowed_to_crawl_forbidden_on_timeout():
"""Check web info."""

with pytest.raises(RobotForbiddenException):
with patch("httpx.AsyncClient.get") as mock_get:
mock_get.side_effect = TimeoutException("Connection timed out")
asyncio.run(ensure_allowed_to_crawl("https://www.google.com", "starbridge"))
with patch(HTTPX_ASYNC_CLIENT_GET) as mock_get:
mock_get.side_effect = TimeoutException(TIMEOUT_MESSAGE)
asyncio.run(ensure_allowed_to_crawl(GET_TEST_URL, __project_name__))


def test_web_util_ensure_allowed_to_crawl_forbidden_on_401():
"""Check web info."""

with pytest.raises(RobotForbiddenException):
with patch("httpx.AsyncClient.get") as mock_get:
with patch(HTTPX_ASYNC_CLIENT_GET) as mock_get:
mock_get.return_value.status_code = 401
asyncio.run(ensure_allowed_to_crawl("https://www.google.com", "starbridge"))
asyncio.run(ensure_allowed_to_crawl(GET_TEST_URL, __project_name__))


def test_web_util_ensure_allowed_to_crawl_allowed_on_404():
"""Check web info."""

with patch("httpx.AsyncClient.get") as mock_get:
with patch(HTTPX_ASYNC_CLIENT_GET) as mock_get:
mock_get.return_value.status_code = 404
# No exception should be raised
asyncio.run(ensure_allowed_to_crawl("https://www.google.com", "starbridge"))
asyncio.run(ensure_allowed_to_crawl(GET_TEST_URL, __project_name__))


def test_web_get_additional_context_success():
"""Check web info."""

context = asyncio.run(
get_additional_context("https://docs.anthropic.com", "starbridge")
)
assert "llms_txt" in context
context = asyncio.run(get_additional_context(LLMS_TXT_URL, __project_name__))
assert LLMS_TXT in context


def test_web_get_additional_context_empty_on_404():
"""Check web info."""

with patch("httpx.AsyncClient.get") as mock_get:
with patch(HTTPX_ASYNC_CLIENT_GET) as mock_get:
mock_get.return_value.status_code = 404
# No exception should be raised
context = asyncio.run(
get_additional_context("https://docs.anthropic.com", "starbridge")
)
assert "llms_txt" not in context
context = asyncio.run(get_additional_context(LLMS_TXT_URL, __project_name__))
assert LLMS_TXT not in context


def test_web_get_additional_context_empty_on_timeout():
"""Check web info."""

with patch("httpx.AsyncClient.get") as mock_get:
mock_get.side_effect = TimeoutException("Connection timed out")
with patch(HTTPX_ASYNC_CLIENT_GET) as mock_get:
mock_get.side_effect = TimeoutException(TIMEOUT_MESSAGE)
# No exception should be raised
context = asyncio.run(
get_additional_context("https://docs.anthropic.com", "starbridge")
)
assert "llms_txt" not in context
context = asyncio.run(get_additional_context(LLMS_TXT_URL, __project_name__))
assert LLMS_TXT not in context


def test_web_get_additional_context_empty_on_full_timeout():
"""Check web info."""

with patch("httpx.AsyncClient.get") as mock_get:
mock_get.side_effect = TimeoutException("Connection timed out")
with patch(HTTPX_ASYNC_CLIENT_GET) as mock_get:
mock_get.side_effect = TimeoutException(TIMEOUT_MESSAGE)
# No exception should be raised
context = asyncio.run(
get_additional_context(
"https://docs.anthropic.com", "starbridge", full=True
)
get_additional_context(LLMS_TXT_URL, __project_name__, full=True)
)
assert "llms_txt" not in context
assert LLMS_TXT not in context


def test_web_get_additional_context_fallback_to_non_full():
Expand All @@ -93,19 +93,17 @@ def __init__(self, status_code, text=""):
self.text = text

def mock_get_side_effect(url, **kwargs):
if "llms-full.txt" in url:
if LLMS_FULL_TXT in url:
return MockResponse(404)
return MockResponse(200, "llms content")
return MockResponse(200, LLMS_DUMY_CONTENT)

with patch("httpx.AsyncClient.get") as mock_get:
with patch(HTTPX_ASYNC_CLIENT_GET) as mock_get:
mock_get.side_effect = mock_get_side_effect
context = asyncio.run(
get_additional_context(
"https://docs.anthropic.com", "starbridge", full=True
)
get_additional_context(LLMS_TXT_URL, __project_name__, full=True)
)
assert "llms_txt" in context
assert context["llms_txt"] == "llms content"
assert LLMS_TXT in context
assert context[LLMS_TXT] == LLMS_DUMY_CONTENT


def test_web_get_additional_context_empty_on():
Expand All @@ -117,16 +115,14 @@ def __init__(self, status_code, text=""):
self.text = text

def mock_get_side_effect(url, **kwargs):
if "llms-full.txt" in url:
if LLMS_FULL_TXT in url:
return MockResponse(404)
return MockResponse(200, "llms content")
return MockResponse(200, LLMS_DUMY_CONTENT)

with patch("httpx.AsyncClient.get") as mock_get:
with patch(HTTPX_ASYNC_CLIENT_GET) as mock_get:
mock_get.side_effect = mock_get_side_effect
context = asyncio.run(
get_additional_context(
"https://docs.anthropic.com", "starbridge", full=True
)
get_additional_context(LLMS_TXT_URL, __project_name__, full=True)
)
assert "llms_txt" in context
assert context["llms_txt"] == "llms content"
assert LLMS_TXT in context
assert context[LLMS_TXT] == LLMS_DUMY_CONTENT

0 comments on commit b2f2f2b

Please sign in to comment.