mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-04 06:40:29 +08:00
Refactor: reformat all code for lefthook using ruff and gofmt (#16585)
This commit is contained in:
@@ -65,9 +65,7 @@ def test_max_tokens_falls_back_to_factory_when_model_extra_empty(monkeypatch):
|
||||
],
|
||||
)
|
||||
|
||||
config = tms.get_model_config_from_provider_instance(
|
||||
"tenant-1", "chat", "gpt-test@default@OpenAI"
|
||||
)
|
||||
config = tms.get_model_config_from_provider_instance("tenant-1", "chat", "gpt-test@default@OpenAI")
|
||||
|
||||
assert config["max_tokens"] == 128000
|
||||
|
||||
@@ -115,8 +113,6 @@ def test_max_tokens_prefers_model_extra_over_factory(monkeypatch):
|
||||
],
|
||||
)
|
||||
|
||||
config = tms.get_model_config_from_provider_instance(
|
||||
"tenant-1", "chat", "gpt-test@default@OpenAI"
|
||||
)
|
||||
config = tms.get_model_config_from_provider_instance("tenant-1", "chat", "gpt-test@default@OpenAI")
|
||||
|
||||
assert config["max_tokens"] == 32000
|
||||
|
||||
@@ -30,6 +30,7 @@ warnings.filterwarnings(
|
||||
def _install_cv2_stub_if_unavailable():
|
||||
try:
|
||||
import cv2 # noqa: F401
|
||||
|
||||
return
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@@ -48,6 +48,7 @@ warnings.filterwarnings(
|
||||
def _install_cv2_stub_if_unavailable():
|
||||
try:
|
||||
import cv2 # noqa: F401
|
||||
|
||||
return
|
||||
except Exception:
|
||||
pass
|
||||
@@ -197,6 +198,7 @@ class _FakeLangfuseClient:
|
||||
def _collect(async_gen):
|
||||
async def _run():
|
||||
return [ev async for ev in async_gen]
|
||||
|
||||
return asyncio.run(_run())
|
||||
|
||||
|
||||
@@ -204,6 +206,7 @@ def _collect(async_gen):
|
||||
# Tests for async_ask (production code path)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.p2
|
||||
def test_async_ask_final_event_carries_decorated_answer(monkeypatch):
|
||||
"""
|
||||
@@ -218,23 +221,21 @@ def test_async_ask_final_event_carries_decorated_answer(monkeypatch):
|
||||
chat_mdl = _StreamingChatModel(llm_answer)
|
||||
retriever = _StubRetriever()
|
||||
|
||||
monkeypatch.setattr(dialog_service.KnowledgebaseService, "get_by_ids", lambda _ids: [_KB])
|
||||
monkeypatch.setattr(
|
||||
dialog_service.KnowledgebaseService, "get_by_ids", lambda _ids: [_KB]
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
dialog_service, "get_model_config_from_provider_instance",
|
||||
dialog_service,
|
||||
"get_model_config_from_provider_instance",
|
||||
lambda _tid, _type, _name: _LLM_CONFIG,
|
||||
)
|
||||
monkeypatch.setattr(dialog_service, "LLMBundle", lambda _tid, _cfg: chat_mdl)
|
||||
monkeypatch.setattr(dialog_service.settings, "retriever", retriever, raising=False)
|
||||
monkeypatch.setattr(dialog_service.settings, "kg_retriever", retriever, raising=False)
|
||||
monkeypatch.setattr(
|
||||
dialog_service.DocMetadataService, "get_flatted_meta_by_kbs", lambda _ids: {}
|
||||
)
|
||||
monkeypatch.setattr(dialog_service.DocMetadataService, "get_flatted_meta_by_kbs", lambda _ids: {})
|
||||
monkeypatch.setattr(dialog_service, "label_question", lambda _q, _kbs: "")
|
||||
# kb_prompt calls DocumentService.get_by_ids which needs a live DB; stub it out.
|
||||
monkeypatch.setattr(
|
||||
dialog_service, "kb_prompt",
|
||||
dialog_service,
|
||||
"kb_prompt",
|
||||
lambda _kbinfos, _max_tokens, **_kw: ["RAGFlow is a RAG engine."],
|
||||
)
|
||||
|
||||
@@ -249,9 +250,7 @@ def test_async_ask_final_event_carries_decorated_answer(monkeypatch):
|
||||
assert events, "async_ask must yield at least one event"
|
||||
|
||||
final_events = [e for e in events if e.get("final") is True]
|
||||
assert len(final_events) == 1, (
|
||||
f"Expected exactly one final event, got {len(final_events)}: {final_events}"
|
||||
)
|
||||
assert len(final_events) == 1, f"Expected exactly one final event, got {len(final_events)}: {final_events}"
|
||||
final = final_events[0]
|
||||
|
||||
assert "answer" in final
|
||||
@@ -267,22 +266,20 @@ def test_async_ask_delta_events_carry_incremental_text_only(monkeypatch):
|
||||
chat_mdl = _StreamingChatModel("Incremental text for delta test.")
|
||||
retriever = _StubRetriever()
|
||||
|
||||
monkeypatch.setattr(dialog_service.KnowledgebaseService, "get_by_ids", lambda _ids: [_KB])
|
||||
monkeypatch.setattr(
|
||||
dialog_service.KnowledgebaseService, "get_by_ids", lambda _ids: [_KB]
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
dialog_service, "get_model_config_from_provider_instance",
|
||||
dialog_service,
|
||||
"get_model_config_from_provider_instance",
|
||||
lambda _tid, _type, _name: _LLM_CONFIG,
|
||||
)
|
||||
monkeypatch.setattr(dialog_service, "LLMBundle", lambda _tid, _cfg: chat_mdl)
|
||||
monkeypatch.setattr(dialog_service.settings, "retriever", retriever, raising=False)
|
||||
monkeypatch.setattr(dialog_service.settings, "kg_retriever", retriever, raising=False)
|
||||
monkeypatch.setattr(
|
||||
dialog_service.DocMetadataService, "get_flatted_meta_by_kbs", lambda _ids: {}
|
||||
)
|
||||
monkeypatch.setattr(dialog_service.DocMetadataService, "get_flatted_meta_by_kbs", lambda _ids: {})
|
||||
monkeypatch.setattr(dialog_service, "label_question", lambda _q, _kbs: "")
|
||||
monkeypatch.setattr(
|
||||
dialog_service, "kb_prompt",
|
||||
dialog_service,
|
||||
"kb_prompt",
|
||||
lambda _kbinfos, _max_tokens, **_kw: ["RAGFlow is a RAG engine."],
|
||||
)
|
||||
|
||||
@@ -295,15 +292,13 @@ def test_async_ask_delta_events_carry_incremental_text_only(monkeypatch):
|
||||
)
|
||||
|
||||
delta_events = [e for e in events if not e.get("final")]
|
||||
final_events = [e for e in events if e.get("final") is True]
|
||||
final_events = [e for e in events if e.get("final") is True]
|
||||
|
||||
assert len(final_events) == 1, f"Expected exactly one final event, got {len(final_events)}"
|
||||
for ev in delta_events:
|
||||
assert ev["reference"] == {}, f"Delta event must have empty reference, got: {ev['reference']}"
|
||||
|
||||
assert "chunks" in final_events[0]["reference"], (
|
||||
"Final event reference must contain chunk data from decorate_answer()"
|
||||
)
|
||||
assert "chunks" in final_events[0]["reference"], "Final event reference must contain chunk data from decorate_answer()"
|
||||
|
||||
|
||||
@pytest.mark.p2
|
||||
@@ -311,9 +306,7 @@ def test_async_ask_empty_kb_ids_yields_error_final_event(monkeypatch):
|
||||
"""
|
||||
When kb_ids is empty, async_ask() must not crash with IndexError on kbs[0].
|
||||
"""
|
||||
monkeypatch.setattr(
|
||||
dialog_service.KnowledgebaseService, "get_by_ids", lambda _ids: []
|
||||
)
|
||||
monkeypatch.setattr(dialog_service.KnowledgebaseService, "get_by_ids", lambda _ids: [])
|
||||
|
||||
events = _collect(
|
||||
dialog_service.async_ask(
|
||||
@@ -357,6 +350,7 @@ def test_async_ask_stale_kb_ids_yields_error_final_event(monkeypatch):
|
||||
# Tests for async_chat (production code path)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _make_dialog(chat_mdl_stub):
|
||||
"""Build a minimal dialog SimpleNamespace for async_chat()."""
|
||||
return SimpleNamespace(
|
||||
@@ -405,33 +399,30 @@ def test_async_chat_final_event_carries_decorated_answer(monkeypatch):
|
||||
retriever = _StubRetriever()
|
||||
|
||||
# Stub out the heavy service/model calls
|
||||
monkeypatch.setattr(dialog_service, "get_model_type_by_name", lambda _tid, _llm_id: ["chat"])
|
||||
monkeypatch.setattr(
|
||||
dialog_service, "get_model_type_by_name",
|
||||
lambda _tid, _llm_id: ["chat"]
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
dialog_service, "get_model_config_from_provider_instance",
|
||||
dialog_service,
|
||||
"get_model_config_from_provider_instance",
|
||||
lambda _tid, _type, _llm_id: _LLM_CONFIG,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
dialog_service.TenantLangfuseService, "filter_by_tenant",
|
||||
dialog_service.TenantLangfuseService,
|
||||
"filter_by_tenant",
|
||||
lambda tenant_id: None,
|
||||
)
|
||||
# get_models returns (kbs, embd_mdl, rerank_mdl, chat_mdl, tts_mdl)
|
||||
monkeypatch.setattr(
|
||||
dialog_service, "get_models",
|
||||
dialog_service,
|
||||
"get_models",
|
||||
lambda _dialog, **_kwargs: ([_KB], chat_mdl, None, chat_mdl, None),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
dialog_service.KnowledgebaseService, "get_field_map", lambda _kb_ids: {}
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
dialog_service.KnowledgebaseService, "get_by_ids", lambda _ids: [_KB]
|
||||
)
|
||||
monkeypatch.setattr(dialog_service.KnowledgebaseService, "get_field_map", lambda _kb_ids: {})
|
||||
monkeypatch.setattr(dialog_service.KnowledgebaseService, "get_by_ids", lambda _ids: [_KB])
|
||||
monkeypatch.setattr(dialog_service.settings, "retriever", retriever, raising=False)
|
||||
monkeypatch.setattr(dialog_service, "label_question", lambda _q, _kbs: "")
|
||||
monkeypatch.setattr(
|
||||
dialog_service, "kb_prompt",
|
||||
dialog_service,
|
||||
"kb_prompt",
|
||||
lambda _kbinfos, _max_tokens, **_kw: ["RAGFlow is a RAG engine."],
|
||||
)
|
||||
|
||||
@@ -441,9 +432,7 @@ def test_async_chat_final_event_carries_decorated_answer(monkeypatch):
|
||||
events = _collect(dialog_service.async_chat(dialog, messages, stream=True, quote=True))
|
||||
|
||||
final_events = [e for e in events if e.get("final") is True]
|
||||
assert len(final_events) == 1, (
|
||||
f"Expected exactly one final event, got {len(final_events)}: {final_events}"
|
||||
)
|
||||
assert len(final_events) == 1, f"Expected exactly one final event, got {len(final_events)}: {final_events}"
|
||||
final = final_events[0]
|
||||
|
||||
assert "answer" in final
|
||||
@@ -462,16 +451,15 @@ def test_async_chat_langfuse_uses_start_observation(monkeypatch):
|
||||
chat_mdl = _StreamingChatModel(llm_answer)
|
||||
retriever = _StubRetriever()
|
||||
|
||||
monkeypatch.setattr(dialog_service, "get_model_type_by_name", lambda _tid, _llm_id: ["chat"])
|
||||
monkeypatch.setattr(
|
||||
dialog_service, "get_model_type_by_name",
|
||||
lambda _tid, _llm_id: ["chat"]
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
dialog_service, "get_model_config_from_provider_instance",
|
||||
dialog_service,
|
||||
"get_model_config_from_provider_instance",
|
||||
lambda _tid, _type, _llm_id: _LLM_CONFIG,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
dialog_service.TenantLangfuseService, "filter_by_tenant",
|
||||
dialog_service.TenantLangfuseService,
|
||||
"filter_by_tenant",
|
||||
lambda tenant_id: SimpleNamespace(
|
||||
public_key="public",
|
||||
secret_key="secret",
|
||||
@@ -486,12 +474,8 @@ def test_async_chat_langfuse_uses_start_observation(monkeypatch):
|
||||
"get_models",
|
||||
lambda _dialog, **_kwargs: ([_KB], chat_mdl, None, chat_mdl, None),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
dialog_service.KnowledgebaseService, "get_field_map", lambda _kb_ids: {}
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
dialog_service.KnowledgebaseService, "get_by_ids", lambda _ids: [_KB]
|
||||
)
|
||||
monkeypatch.setattr(dialog_service.KnowledgebaseService, "get_field_map", lambda _kb_ids: {})
|
||||
monkeypatch.setattr(dialog_service.KnowledgebaseService, "get_by_ids", lambda _ids: [_KB])
|
||||
monkeypatch.setattr(dialog_service.settings, "retriever", retriever, raising=False)
|
||||
monkeypatch.setattr(dialog_service, "label_question", lambda _q, _kbs: "")
|
||||
monkeypatch.setattr(
|
||||
@@ -530,17 +514,15 @@ def test_async_chat_langfuse_observation_includes_session_id(monkeypatch):
|
||||
chat_mdl = _StreamingChatModel("Session traces should be grouped.")
|
||||
retriever = _StubRetriever()
|
||||
|
||||
monkeypatch.setattr(
|
||||
dialog_service, "get_model_type_by_name",
|
||||
lambda _tid, _llm_id: ["chat"]
|
||||
)
|
||||
monkeypatch.setattr(dialog_service, "get_model_type_by_name", lambda _tid, _llm_id: ["chat"])
|
||||
monkeypatch.setattr(
|
||||
dialog_service,
|
||||
"get_model_config_from_provider_instance",
|
||||
lambda _tid, _type, _llm_id: _LLM_CONFIG,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
dialog_service.TenantLangfuseService, "filter_by_tenant",
|
||||
dialog_service.TenantLangfuseService,
|
||||
"filter_by_tenant",
|
||||
lambda tenant_id: SimpleNamespace(
|
||||
public_key="public",
|
||||
secret_key="secret",
|
||||
@@ -554,12 +536,8 @@ def test_async_chat_langfuse_observation_includes_session_id(monkeypatch):
|
||||
"get_models",
|
||||
lambda _dialog, **_kwargs: ([_KB], chat_mdl, None, chat_mdl, None),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
dialog_service.KnowledgebaseService, "get_field_map", lambda _kb_ids: {}
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
dialog_service.KnowledgebaseService, "get_by_ids", lambda _ids: [_KB]
|
||||
)
|
||||
monkeypatch.setattr(dialog_service.KnowledgebaseService, "get_field_map", lambda _kb_ids: {})
|
||||
monkeypatch.setattr(dialog_service.KnowledgebaseService, "get_by_ids", lambda _ids: [_KB])
|
||||
monkeypatch.setattr(dialog_service.settings, "retriever", retriever, raising=False)
|
||||
monkeypatch.setattr(dialog_service, "label_question", lambda _q, _kbs: "")
|
||||
monkeypatch.setattr(
|
||||
@@ -635,16 +613,15 @@ def test_async_chat_continues_when_langfuse_observation_start_fails(monkeypatch)
|
||||
chat_mdl = _StreamingChatModel(llm_answer)
|
||||
retriever = _StubRetriever()
|
||||
|
||||
monkeypatch.setattr(dialog_service, "get_model_type_by_name", lambda _tid, _llm_id: ["chat"])
|
||||
monkeypatch.setattr(
|
||||
dialog_service, "get_model_type_by_name",
|
||||
lambda _tid, _llm_id: ["chat"]
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
dialog_service, "get_model_config_from_provider_instance",
|
||||
dialog_service,
|
||||
"get_model_config_from_provider_instance",
|
||||
lambda _tid, _type, _llm_id: _LLM_CONFIG,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
dialog_service.TenantLangfuseService, "filter_by_tenant",
|
||||
dialog_service.TenantLangfuseService,
|
||||
"filter_by_tenant",
|
||||
lambda tenant_id: SimpleNamespace(
|
||||
public_key="public",
|
||||
secret_key="secret",
|
||||
@@ -659,12 +636,8 @@ def test_async_chat_continues_when_langfuse_observation_start_fails(monkeypatch)
|
||||
"get_models",
|
||||
lambda _dialog, **_kwargs: ([_KB], chat_mdl, None, chat_mdl, None),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
dialog_service.KnowledgebaseService, "get_field_map", lambda _kb_ids: {}
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
dialog_service.KnowledgebaseService, "get_by_ids", lambda _ids: [_KB]
|
||||
)
|
||||
monkeypatch.setattr(dialog_service.KnowledgebaseService, "get_field_map", lambda _kb_ids: {})
|
||||
monkeypatch.setattr(dialog_service.KnowledgebaseService, "get_by_ids", lambda _ids: [_KB])
|
||||
monkeypatch.setattr(dialog_service.settings, "retriever", retriever, raising=False)
|
||||
monkeypatch.setattr(dialog_service, "label_question", lambda _q, _kbs: "")
|
||||
monkeypatch.setattr(
|
||||
|
||||
@@ -316,10 +316,7 @@ def test_async_chat_uses_all_docs_when_no_doc_ids_selected(monkeypatch):
|
||||
)
|
||||
|
||||
monkeypatch.setattr(dialog_service.settings, "retriever", retriever, raising=False)
|
||||
monkeypatch.setattr(
|
||||
dialog_service, "get_model_type_by_name",
|
||||
lambda _tid, _llm_id: ["chat"]
|
||||
)
|
||||
monkeypatch.setattr(dialog_service, "get_model_type_by_name", lambda _tid, _llm_id: ["chat"])
|
||||
monkeypatch.setattr(
|
||||
dialog_service,
|
||||
"get_model_config_from_provider_instance",
|
||||
|
||||
@@ -31,6 +31,7 @@ warnings.filterwarnings(
|
||||
def _install_cv2_stub_if_unavailable():
|
||||
try:
|
||||
import cv2 # noqa: F401
|
||||
|
||||
return
|
||||
except Exception:
|
||||
pass
|
||||
@@ -70,6 +71,7 @@ from common.constants import TaskStatus # noqa: E402
|
||||
# Helpers to access the original function bypassing @DB.connection_context()
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _unwrapped_get_parsing_status():
|
||||
"""Return the original (un-decorated) get_parsing_status_by_kb_ids function.
|
||||
|
||||
@@ -84,6 +86,7 @@ def _unwrapped_get_parsing_status():
|
||||
# Fake ORM helpers – mimic the minimal peewee query chain used by the function
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class _FieldStub:
|
||||
"""Minimal stand-in for a peewee model field used in select/where/group_by."""
|
||||
|
||||
@@ -130,6 +133,7 @@ def _make_fake_model(rows):
|
||||
# Pytest fixture – patch DocumentService.model per test
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def call_with_rows(monkeypatch):
|
||||
"""Return a helper that runs get_parsing_status_by_kb_ids with fake DB rows."""
|
||||
@@ -146,14 +150,11 @@ def call_with_rows(monkeypatch):
|
||||
# Tests
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
_ALL_STATUS_FIELDS = frozenset(
|
||||
["unstart_count", "running_count", "cancel_count", "done_count", "fail_count"]
|
||||
)
|
||||
_ALL_STATUS_FIELDS = frozenset(["unstart_count", "running_count", "cancel_count", "done_count", "fail_count"])
|
||||
|
||||
|
||||
@pytest.mark.p2
|
||||
class TestGetParsingStatusByKbIds:
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Edge-case: empty input list – must short-circuit before any DB call
|
||||
# ------------------------------------------------------------------
|
||||
@@ -224,16 +225,13 @@ class TestGetParsingStatusByKbIds:
|
||||
|
||||
def test_unknown_run_value_ignored(self, call_with_rows):
|
||||
rows = [
|
||||
{"kb_id": "kb-1", "run": "9", "cnt": 99}, # "9" is not a TaskStatus
|
||||
{"kb_id": "kb-1", "run": "9", "cnt": 99}, # "9" is not a TaskStatus
|
||||
{"kb_id": "kb-1", "run": TaskStatus.DONE.value, "cnt": 4},
|
||||
]
|
||||
result = call_with_rows(rows=rows, kb_ids=["kb-1"])
|
||||
|
||||
assert result["kb-1"]["done_count"] == 4
|
||||
assert all(
|
||||
result["kb-1"][f] == 0
|
||||
for f in _ALL_STATUS_FIELDS - {"done_count"}
|
||||
)
|
||||
assert all(result["kb-1"][f] == 0 for f in _ALL_STATUS_FIELDS - {"done_count"})
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# A row whose kb_id was NOT requested must not appear in the output
|
||||
@@ -298,9 +296,7 @@ class TestGetParsingStatusByKbIds:
|
||||
rows = [
|
||||
{"kb_id": "kb-with-data", "run": TaskStatus.DONE.value, "cnt": 1},
|
||||
]
|
||||
result = call_with_rows(
|
||||
rows=rows, kb_ids=["kb-with-data", "kb-empty-1", "kb-empty-2"]
|
||||
)
|
||||
result = call_with_rows(rows=rows, kb_ids=["kb-with-data", "kb-empty-1", "kb-empty-2"])
|
||||
|
||||
assert set(result.keys()) == {"kb-with-data", "kb-empty-1", "kb-empty-2"}
|
||||
assert result["kb-empty-1"] == {f: 0 for f in _ALL_STATUS_FIELDS}
|
||||
@@ -320,7 +316,4 @@ class TestGetParsingStatusByKbIds:
|
||||
assert result["kb-1"]["done_count"] == 2
|
||||
# SCHEDULE is not a tracked bucket
|
||||
assert "schedule_count" not in result["kb-1"]
|
||||
assert all(
|
||||
result["kb-1"][f] == 0
|
||||
for f in _ALL_STATUS_FIELDS - {"done_count"}
|
||||
)
|
||||
assert all(result["kb-1"][f] == 0 for f in _ALL_STATUS_FIELDS - {"done_count"})
|
||||
|
||||
@@ -127,6 +127,7 @@ def test_upload_document_skips_cross_kb_document_id_collision(monkeypatch):
|
||||
# Helpers shared by TestValidateUrlForCrawl
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _addrinfo(ip_str: str) -> list:
|
||||
"""Build a minimal getaddrinfo-style result for a single address string."""
|
||||
family = socket.AF_INET6 if ":" in ip_str else socket.AF_INET
|
||||
@@ -137,6 +138,7 @@ def _addrinfo(ip_str: str) -> list:
|
||||
# _validate_url_for_crawl SSRF-guard tests
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.p2
|
||||
class TestValidateUrlForCrawl:
|
||||
"""Focused regression suite for the SSRF guard on the URL-crawl path.
|
||||
@@ -268,10 +270,7 @@ class TestValidateUrlForCrawl:
|
||||
monkeypatch.setattr(
|
||||
socket,
|
||||
"getaddrinfo",
|
||||
lambda h, p: (
|
||||
_addrinfo("93.184.216.34")
|
||||
+ _addrinfo("2606:2800:220:1:248:1893:25c8:1946")
|
||||
),
|
||||
lambda h, p: _addrinfo("93.184.216.34") + _addrinfo("2606:2800:220:1:248:1893:25c8:1946"),
|
||||
)
|
||||
hostname, resolved_ip = FileService._validate_url_for_crawl("https://example.com/")
|
||||
assert hostname == "example.com"
|
||||
|
||||
@@ -20,22 +20,23 @@ class TestOceanBaseDatabase:
|
||||
|
||||
def test_oceanbase_in_pooled_database_enum(self):
|
||||
"""Test that OCEANBASE is in PooledDatabase enum."""
|
||||
assert hasattr(PooledDatabase, 'OCEANBASE')
|
||||
assert hasattr(PooledDatabase, "OCEANBASE")
|
||||
assert PooledDatabase.OCEANBASE.value == RetryingPooledOceanBaseDatabase
|
||||
|
||||
def test_oceanbase_in_database_lock_enum(self):
|
||||
"""Test that OCEANBASE is in DatabaseLock enum."""
|
||||
assert hasattr(DatabaseLock, 'OCEANBASE')
|
||||
assert hasattr(DatabaseLock, "OCEANBASE")
|
||||
|
||||
def test_oceanbase_in_text_field_type_enum(self):
|
||||
"""Test that OCEANBASE is in TextFieldType enum."""
|
||||
assert hasattr(TextFieldType, 'OCEANBASE')
|
||||
assert hasattr(TextFieldType, "OCEANBASE")
|
||||
# OceanBase should use LONGTEXT like MySQL
|
||||
assert TextFieldType.OCEANBASE.value == "LONGTEXT"
|
||||
|
||||
def test_oceanbase_database_inherits_mysql(self):
|
||||
"""Test that OceanBase database inherits from PooledMySQLDatabase."""
|
||||
from playhouse.pool import PooledMySQLDatabase
|
||||
|
||||
assert issubclass(RetryingPooledOceanBaseDatabase, PooledMySQLDatabase)
|
||||
|
||||
def test_oceanbase_database_init(self):
|
||||
@@ -64,13 +65,13 @@ class TestOceanBaseDatabase:
|
||||
|
||||
def test_pooled_database_enum_values(self):
|
||||
"""Test PooledDatabase enum has all expected values."""
|
||||
expected = {'MYSQL', 'OCEANBASE', 'POSTGRES'}
|
||||
expected = {"MYSQL", "OCEANBASE", "POSTGRES"}
|
||||
actual = {e.name for e in PooledDatabase}
|
||||
assert expected.issubset(actual), f"Missing: {expected - actual}"
|
||||
|
||||
def test_database_lock_enum_values(self):
|
||||
"""Test DatabaseLock enum has all expected values."""
|
||||
expected = {'MYSQL', 'OCEANBASE', 'POSTGRES'}
|
||||
expected = {"MYSQL", "OCEANBASE", "POSTGRES"}
|
||||
actual = set(DatabaseLock.__members__.keys())
|
||||
assert expected.issubset(actual), f"Missing: {expected - actual}"
|
||||
|
||||
@@ -81,45 +82,49 @@ class TestOceanBaseConfiguration:
|
||||
def test_settings_default_to_mysql(self):
|
||||
"""Test that default DB_TYPE is mysql."""
|
||||
import os
|
||||
|
||||
# Save original value
|
||||
original = os.environ.get('DB_TYPE')
|
||||
|
||||
original = os.environ.get("DB_TYPE")
|
||||
|
||||
try:
|
||||
# Remove DB_TYPE to test default
|
||||
if 'DB_TYPE' in os.environ:
|
||||
del os.environ['DB_TYPE']
|
||||
|
||||
if "DB_TYPE" in os.environ:
|
||||
del os.environ["DB_TYPE"]
|
||||
|
||||
# Reload settings
|
||||
from common import settings
|
||||
|
||||
settings.DATABASE_TYPE = os.getenv("DB_TYPE", "mysql")
|
||||
|
||||
|
||||
assert settings.DATABASE_TYPE == "mysql"
|
||||
finally:
|
||||
# Restore original value
|
||||
if original:
|
||||
os.environ['DB_TYPE'] = original
|
||||
os.environ["DB_TYPE"] = original
|
||||
|
||||
def test_settings_can_use_oceanbase(self):
|
||||
"""Test that DB_TYPE can be set to oceanbase."""
|
||||
import os
|
||||
|
||||
# Save original value
|
||||
original = os.environ.get('DB_TYPE')
|
||||
|
||||
original = os.environ.get("DB_TYPE")
|
||||
|
||||
try:
|
||||
os.environ['DB_TYPE'] = 'oceanbase'
|
||||
|
||||
os.environ["DB_TYPE"] = "oceanbase"
|
||||
|
||||
# Reload settings
|
||||
from common import settings
|
||||
|
||||
settings.DATABASE_TYPE = os.getenv("DB_TYPE", "mysql")
|
||||
|
||||
|
||||
assert settings.DATABASE_TYPE == "oceanbase"
|
||||
finally:
|
||||
# Restore original value
|
||||
if original:
|
||||
os.environ['DB_TYPE'] = original
|
||||
os.environ["DB_TYPE"] = original
|
||||
else:
|
||||
if 'DB_TYPE' in os.environ:
|
||||
del os.environ['DB_TYPE']
|
||||
if "DB_TYPE" in os.environ:
|
||||
del os.environ["DB_TYPE"]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user