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.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
test: construct configured SQLAlchemy repositories (#43384)
Asuka Minatoa5eea69efcf8
diff --git a/api/tests/unit_tests/core/repositories/test_factory.py b/api/tests/unit_tests/core/repositories/test_factory.pyindex f795a965b03..31f2fea727d 100644--- a/api/tests/unit_tests/core/repositories/test_factory.py+++ b/api/tests/unit_tests/core/repositories/test_factory.py@@ -5,8 +5,6 @@ This module tests the factory pattern implementation for creating repository insbased on configuration, including error handling."""-from unittest.mock import MagicMock, patch-import pytestfrom sqlalchemy.engine import Enginefrom sqlalchemy.orm import Session, sessionmaker@@ -19,7 +17,7 @@ from core.repositories.sqlalchemy_workflow_execution_repository import SQLAlchemfrom core.repositories.sqlalchemy_workflow_node_execution_repository import SQLAlchemyWorkflowNodeExecutionRepositoryfrom libs.module_loading import import_stringfrom models import Account, EndUser-from models.enums import WorkflowRunTriggeredFrom+from models.enums import CreatorUserRole, WorkflowRunTriggeredFromfrom models.workflow import WorkflowNodeExecutionTriggeredFromRESOURCE_TENANT_ID = "resource-tenant-id"@@ -40,16 +38,20 @@ class TestRepositoryFactory:@pytest.fixture(autouse=True)def _repository_config(self, config_overrides) -> None:config_overrides(- CORE_WORKFLOW_EXECUTION_REPOSITORY="unittest.mock.MagicMock",- CORE_WORKFLOW_NODE_EXECUTION_REPOSITORY="unittest.mock.MagicMock",+ CORE_WORKFLOW_EXECUTION_REPOSITORY=(+ "core.repositories.sqlalchemy_workflow_execution_repository.SQLAlchemyWorkflowExecutionRepository"+ ),+ CORE_WORKFLOW_NODE_EXECUTION_REPOSITORY=(+ "core.repositories.sqlalchemy_workflow_node_execution_repository.SQLAlchemyWorkflowNodeExecutionRepository"+ ),)def test_import_string_success(self):"""Test successful class import."""# Test importing a real class- class_path = "unittest.mock.MagicMock"+ class_path = "core.repositories.sqlalchemy_workflow_execution_repository.SQLAlchemyWorkflowExecutionRepository"result = import_string(class_path)- assert result is MagicMock+ assert result is SQLAlchemyWorkflowExecutionRepositorydef test_import_string_invalid_path(self):"""Test import with invalid module path."""@@ -60,7 +62,7 @@ class TestRepositoryFactory:def test_import_string_invalid_class_name(self):"""Test import with invalid class name."""with pytest.raises(ImportError) as exc_info:- import_string("unittest.mock.NonExistentClass")+ import_string("core.repositories.factory.NonExistentClass")assert "does not define" in str(exc_info.value)def test_import_string_malformed_path(self):@@ -73,31 +75,25 @@ class TestRepositoryFactory:"""Test successful WorkflowExecutionRepository creation."""# Create non-database dependenciesmock_user = Account(name="Test Account", email="[email protected]")+ mock_user.id = "account-id"app_id = "test-app-id"triggered_from = WorkflowRunTriggeredFrom.APP_RUN- # 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):- result = DifyCoreRepositoryFactory.create_workflow_execution_repository(- session_factory=sqlite_session_factory,- tenant_id=RESOURCE_TENANT_ID,- user=mock_user,- app_id=app_id,- triggered_from=triggered_from,- )+ result = DifyCoreRepositoryFactory.create_workflow_execution_repository(+ session_factory=sqlite_session_factory,+ tenant_id=RESOURCE_TENANT_ID,+ user=mock_user,+ app_id=app_id,+ triggered_from=triggered_from,+ )- # Verify the repository was created with correct parameters- mock_repository_class.assert_called_once_with(- session_factory=sqlite_session_factory,- tenant_id=RESOURCE_TENANT_ID,- user=mock_user,- app_id=app_id,- triggered_from=triggered_from,- )- assert isinstance(result, SQLAlchemyWorkflowExecutionRepository)+ assert isinstance(result, SQLAlchemyWorkflowExecutionRepository)+ assert result._session_factory is sqlite_session_factory+ assert result._tenant_id == RESOURCE_TENANT_ID+ assert result._creator_user_id == mock_user.id+ assert result._creator_user_role == CreatorUserRole.ACCOUNT+ assert result._app_id == app_id+ assert result._triggered_from == triggered_fromdef test_create_workflow_execution_repository_import_error(self, sqlite_session_factory, config_overrides):"""Test WorkflowExecutionRepository creation with import error."""@@ -119,51 +115,41 @@ class TestRepositoryFactory:"""Test WorkflowExecutionRepository creation with instantiation error."""mock_user = Account(name="Test Account", email="[email protected]")- # Create a mock repository class that raises exception on instantiation- mock_repository_class = MagicMock()- mock_repository_class.side_effect = Exception("Instantiation failed")-- # Mock import_string to return a failing class- with patch("core.repositories.factory.import_string", return_value=mock_repository_class, autospec=True):- with pytest.raises(RepositoryImportError) as exc_info:- DifyCoreRepositoryFactory.create_workflow_execution_repository(- session_factory=sqlite_session_factory,- tenant_id=RESOURCE_TENANT_ID,- user=mock_user,- app_id="test-app-id",- triggered_from=WorkflowRunTriggeredFrom.APP_RUN,- )- assert "Failed to create WorkflowExecutionRepository" in str(exc_info.value)+ with pytest.raises(RepositoryImportError) as exc_info:+ DifyCoreRepositoryFactory.create_workflow_execution_repository(+ session_factory=sqlite_session_factory,+ tenant_id="",+ user=mock_user,+ app_id="test-app-id",+ triggered_from=WorkflowRunTriggeredFrom.APP_RUN,+ )+ assert "Failed to create WorkflowExecutionRepository" in str(exc_info.value)+ assert isinstance(exc_info.value.__cause__, ValueError)+ assert str(exc_info.value.__cause__) == "tenant_id is required"def test_create_workflow_node_execution_repository_success(self, sqlite_session_factory):"""Test successful WorkflowNodeExecutionRepository creation."""# Create non-database dependencies- mock_user = EndUser()+ mock_user = EndUser(id="end-user-id")app_id = "test-app-id"triggered_from = WorkflowNodeExecutionTriggeredFrom.SINGLE_STEP- # 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):- result = DifyCoreRepositoryFactory.create_workflow_node_execution_repository(- session_factory=sqlite_session_factory,- tenant_id=RESOURCE_TENANT_ID,- user=mock_user,- app_id=app_id,- triggered_from=triggered_from,- )+ result = DifyCoreRepositoryFactory.create_workflow_node_execution_repository(+ session_factory=sqlite_session_factory,+ tenant_id=RESOURCE_TENANT_ID,+ user=mock_user,+ app_id=app_id,+ triggered_from=triggered_from,+ )- # Verify the repository was created with correct parameters- mock_repository_class.assert_called_once_with(- session_factory=sqlite_session_factory,- tenant_id=RESOURCE_TENANT_ID,- user=mock_user,- app_id=app_id,- triggered_from=triggered_from,- )- assert isinstance(result, SQLAlchemyWorkflowNodeExecutionRepository)+ assert isinstance(result, SQLAlchemyWorkflowNodeExecutionRepository)+ assert result._session_factory is sqlite_session_factory+ assert result._tenant_id == RESOURCE_TENANT_ID+ assert result._user is mock_user+ assert result._creator_user_id == mock_user.id+ assert result._creator_user_role == CreatorUserRole.END_USER+ assert result._app_id == app_id+ assert result._triggered_from == triggered_fromdef test_create_workflow_node_execution_repository_import_error(self, sqlite_session_factory, config_overrides):"""Test WorkflowNodeExecutionRepository creation with import error."""@@ -185,21 +171,17 @@ class TestRepositoryFactory:"""Test WorkflowNodeExecutionRepository creation with instantiation error."""mock_user = EndUser()- # Create a mock repository class that raises exception on instantiation- mock_repository_class = MagicMock()- mock_repository_class.side_effect = Exception("Instantiation failed")-- # Mock import_string to return a failing class- with patch("core.repositories.factory.import_string", return_value=mock_repository_class, autospec=True):- with pytest.raises(RepositoryImportError) as exc_info:- DifyCoreRepositoryFactory.create_workflow_node_execution_repository(- session_factory=sqlite_session_factory,- tenant_id=RESOURCE_TENANT_ID,- user=mock_user,- app_id="test-app-id",- triggered_from=WorkflowNodeExecutionTriggeredFrom.SINGLE_STEP,- )- assert "Failed to create WorkflowNodeExecutionRepository" in str(exc_info.value)+ with pytest.raises(RepositoryImportError) as exc_info:+ DifyCoreRepositoryFactory.create_workflow_node_execution_repository(+ session_factory=sqlite_session_factory,+ tenant_id="",+ user=mock_user,+ app_id="test-app-id",+ triggered_from=WorkflowNodeExecutionTriggeredFrom.SINGLE_STEP,+ )+ assert "Failed to create WorkflowNodeExecutionRepository" in str(exc_info.value)+ assert isinstance(exc_info.value.__cause__, ValueError)+ assert str(exc_info.value.__cause__) == "tenant_id is required"def test_repository_import_error_exception(self):"""Test RepositoryImportError exception handling."""@@ -211,28 +193,23 @@ class TestRepositoryFactory:"""Test repository creation with Engine instead of sessionmaker."""# Pass the real Engine directly instead of wrapping it in sessionmakermock_user = Account(name="Test Account", email="[email protected]")+ mock_user.id = "account-id"app_id = "test-app-id"triggered_from = WorkflowRunTriggeredFrom.APP_RUN- # 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):- result = DifyCoreRepositoryFactory.create_workflow_execution_repository(- session_factory=sqlite_engine,- tenant_id=RESOURCE_TENANT_ID,- user=mock_user,- app_id=app_id,- triggered_from=triggered_from,- )+ result = DifyCoreRepositoryFactory.create_workflow_execution_repository(+ session_factory=sqlite_engine,+ tenant_id=RESOURCE_TENANT_ID,+ user=mock_user,+ app_id=app_id,+ triggered_from=triggered_from,+ )- # Verify the repository was created with correct parameters- mock_repository_class.assert_called_once_with(- session_factory=sqlite_engine,- tenant_id=RESOURCE_TENANT_ID,- user=mock_user,- app_id=app_id,- triggered_from=triggered_from,- )- assert isinstance(result, SQLAlchemyWorkflowExecutionRepository)+ assert isinstance(result, SQLAlchemyWorkflowExecutionRepository)+ with result._session_factory() as session:+ assert session.get_bind() is sqlite_engine+ assert result._tenant_id == RESOURCE_TENANT_ID+ assert result._creator_user_id == mock_user.id+ assert result._creator_user_role == CreatorUserRole.ACCOUNT+ assert result._app_id == app_id+ assert result._triggered_from == triggered_from