mirror of
https://github.com/langgenius/dify.git
synced 2026-10-04 13:47:55 +08:00
test: construct real repositories and Redis composition fixtures (#43207)
This commit is contained in:
1 parent
6cca398f1c
commit
fa4f49df00
3 files changed
+30
-26
No files matched your search
+16
-6
@@ -86,6 +86,17 @@ def _create_workflow_run(
|
||||
return workflow_run
|
||||
|
||||
|
||||
def _unpersisted_pause(*, workflow_id: str, workflow_run_id: str) -> WorkflowPauseEntity:
|
||||
return _PrivateWorkflowPauseEntity(
|
||||
pause_model=WorkflowPause(
|
||||
workflow_id=workflow_id,
|
||||
workflow_run_id=workflow_run_id,
|
||||
state_object_key="unpersisted-state",
|
||||
),
|
||||
reason_models=[],
|
||||
)
|
||||
|
||||
|
||||
def _cleanup_scope_data(session: Session, scope: _TestScope) -> None:
|
||||
"""Remove test-created DB rows and storage objects for a test scope."""
|
||||
|
||||
@@ -736,8 +747,7 @@ class TestResumeWorkflowPause:
|
||||
test_scope,
|
||||
status=WorkflowExecutionStatus.RUNNING,
|
||||
)
|
||||
pause_entity = Mock(spec=WorkflowPauseEntity)
|
||||
pause_entity.id = str(uuid4())
|
||||
pause_entity = _unpersisted_pause(workflow_id=test_scope.workflow_id, workflow_run_id=workflow_run.id)
|
||||
|
||||
with pytest.raises(_WorkflowRunError, match="WorkflowRun is not in PAUSED status"):
|
||||
repository.resume_workflow_pause(
|
||||
@@ -769,8 +779,9 @@ class TestResumeWorkflowPause:
|
||||
assert pause_model is not None
|
||||
test_scope.state_keys.add(pause_model.state_object_key)
|
||||
|
||||
mismatched_pause_entity = Mock(spec=WorkflowPauseEntity)
|
||||
mismatched_pause_entity.id = str(uuid4())
|
||||
mismatched_pause_entity = _unpersisted_pause(
|
||||
workflow_id=test_scope.workflow_id, workflow_run_id=workflow_run.id
|
||||
)
|
||||
|
||||
with pytest.raises(_WorkflowRunError, match="different id in WorkflowPause and WorkflowPauseEntity"):
|
||||
repository.resume_workflow_pause(
|
||||
@@ -819,8 +830,7 @@ class TestDeleteWorkflowPause:
|
||||
) -> None:
|
||||
"""Raise _WorkflowRunError when deleting a non-existent pause."""
|
||||
|
||||
pause_entity = Mock(spec=WorkflowPauseEntity)
|
||||
pause_entity.id = str(uuid4())
|
||||
pause_entity = _unpersisted_pause(workflow_id=str(uuid4()), workflow_run_id=str(uuid4()))
|
||||
|
||||
with pytest.raises(_WorkflowRunError, match="WorkflowPause not found"):
|
||||
repository.delete_workflow_pause(pause_entity=pause_entity)
|
||||
|
||||
@@ -14,9 +14,9 @@ from sqlalchemy.orm import Session, sessionmaker
|
||||
from core.repositories.factory import (
|
||||
DifyCoreRepositoryFactory,
|
||||
RepositoryImportError,
|
||||
WorkflowExecutionRepository,
|
||||
WorkflowNodeExecutionRepository,
|
||||
)
|
||||
from core.repositories.sqlalchemy_workflow_execution_repository import SQLAlchemyWorkflowExecutionRepository
|
||||
from core.repositories.sqlalchemy_workflow_node_execution_repository import SQLAlchemyWorkflowNodeExecutionRepository
|
||||
from libs.module_loading import import_string
|
||||
from models import Account, EndUser
|
||||
from models.enums import WorkflowRunTriggeredFrom
|
||||
@@ -76,10 +76,8 @@ class TestRepositoryFactory:
|
||||
app_id = "test-app-id"
|
||||
triggered_from = WorkflowRunTriggeredFrom.APP_RUN
|
||||
|
||||
# Create mock repository class and instance
|
||||
mock_repository_class = MagicMock()
|
||||
mock_repository_instance = MagicMock(spec=WorkflowExecutionRepository)
|
||||
mock_repository_class.return_value = mock_repository_instance
|
||||
# Record construction while creating the real SQLAlchemy repository.
|
||||
mock_repository_class = MagicMock(wraps=SQLAlchemyWorkflowExecutionRepository)
|
||||
|
||||
# Mock import_string
|
||||
with patch("core.repositories.factory.import_string", return_value=mock_repository_class, autospec=True):
|
||||
@@ -99,7 +97,7 @@ class TestRepositoryFactory:
|
||||
app_id=app_id,
|
||||
triggered_from=triggered_from,
|
||||
)
|
||||
assert result is mock_repository_instance
|
||||
assert isinstance(result, SQLAlchemyWorkflowExecutionRepository)
|
||||
|
||||
def test_create_workflow_execution_repository_import_error(self, sqlite_session_factory, config_overrides):
|
||||
"""Test WorkflowExecutionRepository creation with import error."""
|
||||
@@ -144,10 +142,8 @@ class TestRepositoryFactory:
|
||||
app_id = "test-app-id"
|
||||
triggered_from = WorkflowNodeExecutionTriggeredFrom.SINGLE_STEP
|
||||
|
||||
# Create mock repository class and instance
|
||||
mock_repository_class = MagicMock()
|
||||
mock_repository_instance = MagicMock(spec=WorkflowNodeExecutionRepository)
|
||||
mock_repository_class.return_value = mock_repository_instance
|
||||
# Record construction while creating the real SQLAlchemy repository.
|
||||
mock_repository_class = MagicMock(wraps=SQLAlchemyWorkflowNodeExecutionRepository)
|
||||
|
||||
# Mock import_string
|
||||
with patch("core.repositories.factory.import_string", return_value=mock_repository_class, autospec=True):
|
||||
@@ -167,7 +163,7 @@ class TestRepositoryFactory:
|
||||
app_id=app_id,
|
||||
triggered_from=triggered_from,
|
||||
)
|
||||
assert result is mock_repository_instance
|
||||
assert isinstance(result, SQLAlchemyWorkflowNodeExecutionRepository)
|
||||
|
||||
def test_create_workflow_node_execution_repository_import_error(self, sqlite_session_factory, config_overrides):
|
||||
"""Test WorkflowNodeExecutionRepository creation with import error."""
|
||||
@@ -218,10 +214,8 @@ class TestRepositoryFactory:
|
||||
app_id = "test-app-id"
|
||||
triggered_from = WorkflowRunTriggeredFrom.APP_RUN
|
||||
|
||||
# Create mock repository class and instance
|
||||
mock_repository_class = MagicMock()
|
||||
mock_repository_instance = MagicMock(spec=WorkflowExecutionRepository)
|
||||
mock_repository_class.return_value = mock_repository_instance
|
||||
# Record construction while creating the real SQLAlchemy repository.
|
||||
mock_repository_class = MagicMock(wraps=SQLAlchemyWorkflowExecutionRepository)
|
||||
|
||||
# Mock import_string
|
||||
with patch("core.repositories.factory.import_string", return_value=mock_repository_class, autospec=True):
|
||||
@@ -241,4 +235,4 @@ class TestRepositoryFactory:
|
||||
app_id=app_id,
|
||||
triggered_from=triggered_from,
|
||||
)
|
||||
assert result is mock_repository_instance
|
||||
assert isinstance(result, SQLAlchemyWorkflowExecutionRepository)
|
||||
@@ -858,7 +858,7 @@ def test_build_application_services_groups_dataset_services_and_reuses_repositor
|
||||
database_client=sqlite_session_factory,
|
||||
deployment_edition=DeploymentEdition.COMMUNITY,
|
||||
initialization_password="",
|
||||
redis=MagicMock(spec=RedisClientWrapper),
|
||||
redis=_redis(),
|
||||
)
|
||||
|
||||
assert isinstance(services.data_sources.bindings, DataSourceBindingApplicationService)
|
||||
@@ -902,7 +902,7 @@ def test_build_application_services_wires_credential_query(
|
||||
database_client=sqlite_session_factory,
|
||||
deployment_edition=DeploymentEdition.COMMUNITY,
|
||||
initialization_password="",
|
||||
redis=MagicMock(spec=RedisClientWrapper),
|
||||
redis=_redis(),
|
||||
)
|
||||
tenant_id, actor_id = str(uuid4()), str(uuid4())
|
||||
with sqlite_session_factory.begin() as session:
|
||||
@@ -1004,7 +1004,7 @@ def test_build_application_services_reuses_installed_app_generation_dependencies
|
||||
database_client=sqlite_session_factory,
|
||||
deployment_edition=DeploymentEdition.COMMUNITY,
|
||||
initialization_password="",
|
||||
redis=MagicMock(spec=RedisClientWrapper),
|
||||
redis=_redis(),
|
||||
)
|
||||
|
||||
assert services.installed_apps.access._installed_apps is services.installed_apps.generation._usage
|
||||
|
||||
Reference in new issue
Block a user