Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

test: use real HTTP clients and creator responses (#43164)

Asuka Minatodb317bf9a15e
diff --git a/api/tests/unit_tests/core/helper/test_creators.py b/api/tests/unit_tests/core/helper/test_creators.py
index 7734146770a..64ff7825431 100644
--- a/api/tests/unit_tests/core/helper/test_creators.py
+++ b/api/tests/unit_tests/core/helper/test_creators.py
@@ -1,6 +1,6 @@
"""Tests for the Creators Platform helper module."""
-from unittest.mock import MagicMock, patch
+from unittest.mock import patch
import httpx
import pytest
@@ -19,10 +19,11 @@ def _patch_creators_url(monkeypatch: pytest.MonkeyPatch):
class TestUploadDSL:
@patch("core.helper.creators.httpx.post")
def test_returns_claim_code(self, mock_post):
- mock_response = MagicMock(spec=httpx.Response)
- mock_response.json.return_value = {"data": {"claim_code": "abc123"}}
- mock_response.raise_for_status = MagicMock()
- mock_post.return_value = mock_response
+ mock_post.return_value = httpx.Response(
+ 200,
+ request=httpx.Request("POST", "https://creators.example.com/api/v1/templates/anonymous-upload"),
+ json={"data": {"claim_code": "abc123"}},
+ )
from core.helper.creators import upload_dsl
@@ -46,10 +47,11 @@ class TestUploadDSL:
@patch("core.helper.creators.httpx.post")
def test_raises_on_missing_claim_code(self, mock_post):
- mock_response = MagicMock(spec=httpx.Response)
- mock_response.json.return_value = {"data": {}}
- mock_response.raise_for_status = MagicMock()
- mock_post.return_value = mock_response
+ mock_post.return_value = httpx.Response(
+ 200,
+ request=httpx.Request("POST", "https://creators.example.com/api/v1/templates/anonymous-upload"),
+ json={"data": {}},
+ )
from core.helper.creators import upload_dsl
@@ -58,10 +60,11 @@ class TestUploadDSL:
@patch("core.helper.creators.httpx.post")
def test_raises_on_non_string_claim_code(self, mock_post):
- mock_response = MagicMock(spec=httpx.Response)
- mock_response.json.return_value = {"data": {"claim_code": 123}}
- mock_response.raise_for_status = MagicMock()
- mock_post.return_value = mock_response
+ mock_post.return_value = httpx.Response(
+ 200,
+ request=httpx.Request("POST", "https://creators.example.com/api/v1/templates/anonymous-upload"),
+ json={"data": {"claim_code": 123}},
+ )
from core.helper.creators import upload_dsl
@@ -70,18 +73,16 @@ class TestUploadDSL:
@patch("core.helper.creators.httpx.post")
def test_raises_on_http_error(self, mock_post):
- mock_response = MagicMock(spec=httpx.Response)
- mock_response.raise_for_status.side_effect = httpx.HTTPStatusError(
- "Server Error",
- request=MagicMock(),
- response=MagicMock(),
+ mock_post.return_value = httpx.Response(
+ 500,
+ request=httpx.Request("POST", "https://creators.example.com/api/v1/templates/anonymous-upload"),
)
- mock_post.return_value = mock_response
from core.helper.creators import upload_dsl
- with pytest.raises(httpx.HTTPStatusError):
+ with pytest.raises(httpx.HTTPStatusError) as caught:
upload_dsl(b"app: demo")
+ assert caught.value.response is mock_post.return_value
class TestGetRedirectUrl:
diff --git a/api/tests/unit_tests/core/helper/test_http_client_pooling.py b/api/tests/unit_tests/core/helper/test_http_client_pooling.py
index c29962f1b1a..4b426c139f4 100644
--- a/api/tests/unit_tests/core/helper/test_http_client_pooling.py
+++ b/api/tests/unit_tests/core/helper/test_http_client_pooling.py
@@ -1,16 +1,28 @@
from __future__ import annotations
-from unittest.mock import MagicMock
+from collections.abc import Iterator
import httpx
+import pytest
from core.helper.http_client_pooling import HttpClientPoolFactory
-def test_get_or_create_reuses_client_for_same_key() -> None:
+def http_clients() -> Iterator[tuple[httpx.Client, httpx.Client]]:
+ def reject_request(request: httpx.Request) -> httpx.Response:
+ raise AssertionError(f"Pooling must not send requests: {request.url}")
+
+ with (
+ httpx.Client(transport=httpx.MockTransport(reject_request)) as first,
+ httpx.Client(transport=httpx.MockTransport(reject_request)) as second,
+ ):
+ yield first, second
+
+
+def test_get_or_create_reuses_client_for_same_key(http_clients: tuple[httpx.Client, httpx.Client]) -> None:
factory = HttpClientPoolFactory()
- first_client = MagicMock(spec=httpx.Client)
- second_client = MagicMock(spec=httpx.Client)
+ first_client, second_client = http_clients
clients = [first_client, second_client]
def _builder() -> httpx.Client:
@@ -20,22 +32,24 @@ def test_get_or_create_reuses_client_for_same_key() -> None:
assert factory.get_or_create("shared", _builder) is first_client
-def test_get_or_create_creates_distinct_clients_for_distinct_keys() -> None:
+def test_get_or_create_creates_distinct_clients_for_distinct_keys(
+ http_clients: tuple[httpx.Client, httpx.Client],
+) -> None:
factory = HttpClientPoolFactory()
- client_a = MagicMock(spec=httpx.Client)
- client_b = MagicMock(spec=httpx.Client)
+ client_a, client_b = http_clients
assert factory.get_or_create("a", lambda: client_a) is client_a
assert factory.get_or_create("b", lambda: client_b) is client_b
-def test_close_all_closes_pooled_clients_and_allows_recreate() -> None:
+def test_close_all_closes_pooled_clients_and_allows_recreate(http_clients: tuple[httpx.Client, httpx.Client]) -> None:
factory = HttpClientPoolFactory()
- first_client = MagicMock(spec=httpx.Client)
- replacement_client = MagicMock(spec=httpx.Client)
+ first_client, replacement_client = http_clients
assert factory.get_or_create("x", lambda: first_client) is first_client
+ assert not first_client.is_closed
factory.close_all()
- first_client.close.assert_called_once()
+ assert first_client.is_closed
+ assert not replacement_client.is_closed
assert factory.get_or_create("x", lambda: replacement_client) is replacement_client