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: run draft variable coverage on SQLite (#40716)

Asuka Minato98b7e9219185
diff --git a/api/tests/integration_tests/services/test_workflow_draft_variable_service.py b/api/tests/unit_tests/services/workflow/test_workflow_draft_variable_service_sqlite.py
similarity index 78%
rename from api/tests/integration_tests/services/test_workflow_draft_variable_service.py
rename to api/tests/unit_tests/services/workflow/test_workflow_draft_variable_service_sqlite.py
index 37bcdce6334..2f66d12e6e2 100644
--- a/api/tests/integration_tests/services/test_workflow_draft_variable_service.py
+++ b/api/tests/unit_tests/services/workflow/test_workflow_draft_variable_service_sqlite.py
@@ -1,15 +1,18 @@
+"""SQLite-backed workflow draft-variable service coverage."""
+
import json
import unittest
import uuid
+from pathlib import Path
from typing import override
import pytest
-from sqlalchemy import delete, func, select
+from sqlalchemy import Engine, delete, func, select
from sqlalchemy.orm import Session
from core.workflow.variable_prefixes import CONVERSATION_VARIABLE_NODE_ID, SYSTEM_VARIABLE_NODE_ID
-from extensions.ext_database import db
from extensions.ext_storage import storage
+from extensions.storage.opendal_storage import OpenDALStorage
from extensions.storage.storage_type import StorageType
from factories.variable_factory import build_segment
from graphon.nodes import BuiltinNodeTypes
@@ -17,6 +20,8 @@ from graphon.variables.segments import StringSegment
from graphon.variables.types import SegmentType
from graphon.variables.variables import StringVariable
from libs import datetime_utils
+from models import Account, Tenant, TenantAccountJoin
+from models.account import TenantAccountRole
from models.enums import CreatorUserRole
from models.model import UploadFile
from models.workflow import Workflow, WorkflowDraftVariable, WorkflowDraftVariableFile, WorkflowNodeExecutionModel
@@ -28,7 +33,30 @@ from services.workflow_draft_variable_service import (
)
[email protected]("flask_req_ctx")
[email protected](autouse=True)
+def _bind_storage(
+ monkeypatch: pytest.MonkeyPatch,
+ tmp_path: Path,
+) -> None:
+ monkeypatch.setattr(
+ storage,
+ "storage_runner",
+ OpenDALStorage(scheme="fs", root=str(tmp_path / "storage")),
+ raising=False,
+ )
+
+
+def _persist_account(*, engine: Engine, tenant_id: str) -> Account:
+ tenant = Tenant(name="draft-variable-tenant")
+ tenant.id = tenant_id
+ account = Account(email=f"draft-{uuid.uuid4()}@example.com", name="Draft Variable User")
+ membership = TenantAccountJoin(tenant_id=tenant.id, account_id=account.id, role=TenantAccountRole.OWNER)
+ with Session(bind=engine, expire_on_commit=False) as session:
+ session.add_all([tenant, account, membership])
+ session.commit()
+ return account
+
+
class TestWorkflowDraftVariableService(unittest.TestCase):
_test_app_id: str
_session: Session
@@ -37,11 +65,14 @@ class TestWorkflowDraftVariableService(unittest.TestCase):
_node2_id = "test_node_2"
_node_exec_id = str(uuid.uuid4())
+ @pytest.fixture(autouse=True)
+ def _bind_session(self, sqlite_session: Session) -> None:
+ self._session = sqlite_session
+
@override
- def setUp(self):
+ def setUp(self) -> None:
self._test_app_id = str(uuid.uuid4())
self._test_user_id = str(uuid.uuid4())
- self._session: Session = db.session()
sys_var = WorkflowDraftVariable.new_sys_variable(
app_id=self._test_app_id,
user_id=self._test_user_id,
@@ -93,8 +124,8 @@ class TestWorkflowDraftVariableService(unittest.TestCase):
]
)
- db.session.add_all(_variables)
- db.session.flush()
+ self._session.add_all(_variables)
+ self._session.flush()
self._variable_ids = [v.id for v in _variables]
self._node1_str_var_id = node1_var.id
self._sys_var_id = sys_var.id
@@ -105,10 +136,10 @@ class TestWorkflowDraftVariableService(unittest.TestCase):
return WorkflowDraftVariableService(session=self._session)
@override
- def tearDown(self):
+ def tearDown(self) -> None:
self._session.rollback()
- def test_list_variables(self):
+ def test_list_variables(self) -> None:
srv = self._get_test_srv()
var_list = srv.list_variables_without_values(self._test_app_id, page=1, limit=2, user_id=self._test_user_id)
assert var_list.total == 5
@@ -123,7 +154,7 @@ class TestWorkflowDraftVariableService(unittest.TestCase):
assert page2_var_ids.isdisjoint(page1_var_ids)
assert page2_var_ids.issubset(self._variable_ids)
- def test_get_node_variable(self):
+ def test_get_node_variable(self) -> None:
srv = self._get_test_srv()
node_var = srv.get_node_variable(self._test_app_id, self._node1_id, "str_var", user_id=self._test_user_id)
assert node_var is not None
@@ -131,7 +162,7 @@ class TestWorkflowDraftVariableService(unittest.TestCase):
assert node_var.name == "str_var"
assert node_var.get_value() == build_segment("str_value")
- def test_get_system_variable(self):
+ def test_get_system_variable(self) -> None:
srv = self._get_test_srv()
sys_var = srv.get_system_variable(self._test_app_id, "sys_var", user_id=self._test_user_id)
assert sys_var is not None
@@ -139,7 +170,7 @@ class TestWorkflowDraftVariableService(unittest.TestCase):
assert sys_var.name == "sys_var"
assert sys_var.get_value() == build_segment("sys_value")
- def test_get_conversation_variable(self):
+ def test_get_conversation_variable(self) -> None:
srv = self._get_test_srv()
conv_var = srv.get_conversation_variable(self._test_app_id, "conv_var", user_id=self._test_user_id)
assert conv_var is not None
@@ -147,7 +178,7 @@ class TestWorkflowDraftVariableService(unittest.TestCase):
assert conv_var.name == "conv_var"
assert conv_var.get_value() == build_segment("conv_value")
- def test_delete_node_variables(self):
+ def test_delete_node_variables(self) -> None:
srv = self._get_test_srv()
srv.delete_node_variables(self._test_app_id, self._node2_id, user_id=self._test_user_id)
node2_var_count = self._session.scalar(
@@ -161,7 +192,7 @@ class TestWorkflowDraftVariableService(unittest.TestCase):
)
assert node2_var_count == 0
- def test_delete_variable(self):
+ def test_delete_variable(self) -> None:
srv = self._get_test_srv()
node_1_var = self._session.scalars(
select(WorkflowDraftVariable).where(WorkflowDraftVariable.id == self._node1_str_var_id)
@@ -174,13 +205,13 @@ class TestWorkflowDraftVariableService(unittest.TestCase):
)
assert exists is False
- def test__list_node_variables(self):
+ def test__list_node_variables(self) -> None:
srv = self._get_test_srv()
node_vars = srv._list_node_variables(self._test_app_id, self._node2_id, user_id=self._test_user_id)
assert len(node_vars.variables) == 2
assert {v.id for v in node_vars.variables} == set(self._node2_var_ids)
- def test_get_draft_variables_by_selectors(self):
+ def test_get_draft_variables_by_selectors(self) -> None:
srv = self._get_test_srv()
selectors = [
[self._node1_id, "str_var"],
@@ -192,32 +223,20 @@ class TestWorkflowDraftVariableService(unittest.TestCase):
assert {v.id for v in variables} == {self._node1_str_var_id} | set(self._node2_var_ids)
[email protected]("flask_req_ctx")
class TestDraftVariableLoader(unittest.TestCase):
_test_app_id: str
_test_tenant_id: str
+ _engine: Engine
_node1_id = "test_loader_node_1"
_node_exec_id = str(uuid.uuid4())
- # @pytest.fixture
- # def test_app_id(self):
- # return str(uuid.uuid4())
-
- # @pytest.fixture
- # def test_tenant_id(self):
- # return str(uuid.uuid4())
+ @pytest.fixture(autouse=True)
+ def _bind_engine(self, sqlite_engine: Engine) -> None:
+ self._engine = sqlite_engine
- # @pytest.fixture
- # def session(self):
- # with Session(bind=db.engine, expire_on_commit=False) as session:
- # yield session
-
- # @pytest.fixture
- # def node_var(self, session):
- # pass
@override
- def setUp(self):
+ def setUp(self) -> None:
self._test_app_id = str(uuid.uuid4())
self._test_tenant_id = str(uuid.uuid4())
self._test_user_id = str(uuid.uuid4())
@@ -249,7 +268,7 @@ class TestDraftVariableLoader(unittest.TestCase):
conv_var,
]
- with Session(bind=db.engine, expire_on_commit=False) as session:
+ with Session(bind=self._engine, expire_on_commit=False) as session:
session.add_all(_variables)
session.flush()
session.commit()
@@ -259,14 +278,14 @@ class TestDraftVariableLoader(unittest.TestCase):
self._conv_var_id = conv_var.id
@override
- def tearDown(self):
- with Session(bind=db.engine, expire_on_commit=False) as session:
+ def tearDown(self) -> None:
+ with Session(bind=self._engine, expire_on_commit=False) as session:
session.execute(delete(WorkflowDraftVariable).where(WorkflowDraftVariable.app_id == self._test_app_id))
session.commit()
- def test_variable_loader_with_empty_selector(self):
+ def test_variable_loader_with_empty_selector(self) -> None:
var_loader = DraftVarLoader(
- engine=db.engine,
+ engine=self._engine,
app_id=self._test_app_id,
tenant_id=self._test_tenant_id,
user_id=self._test_user_id,
@@ -274,9 +293,9 @@ class TestDraftVariableLoader(unittest.TestCase):
variables = var_loader.load_variables([])
assert len(variables) == 0
- def test_variable_loader_with_non_empty_selector(self):
+ def test_variable_loader_with_non_empty_selector(self) -> None:
var_loader = DraftVarLoader(
- engine=db.engine,
+ engine=self._engine,
app_id=self._test_app_id,
tenant_id=self._test_tenant_id,
user_id=self._test_user_id,
@@ -296,9 +315,9 @@ class TestDraftVariableLoader(unittest.TestCase):
node1_var = next(v for v in variables if v.selector[0] == self._node1_id)
assert node1_var.id == self._node_var_id
- @pytest.mark.usefixtures("setup_account")
- def test_load_offloaded_variable_string_type_integration(self, setup_account):
+ def test_load_offloaded_variable_string_type_integration(self) -> None:
"""Test _load_offloaded_variable with string type using DraftVariableSaver for data creation."""
+ account = _persist_account(engine=self._engine, tenant_id=self._test_tenant_id)
# Create a large string that will be offloaded
test_content = "x" * 15000 # Create a string larger than LARGE_VARIABLE_THRESHOLD (10KB)
@@ -307,7 +326,7 @@ class TestDraftVariableLoader(unittest.TestCase):
node_execution_id = str(uuid.uuid4())
try:
- with Session(bind=db.engine, expire_on_commit=False) as session:
+ with Session(bind=self._engine, expire_on_commit=False) as session:
# Use DraftVariableSaver to create offloaded variable (this mimics production)
saver = DraftVariableSaver(
session=session,
@@ -316,7 +335,7 @@ class TestDraftVariableLoader(unittest.TestCase):
node_id="test_offload_node",
node_type=BuiltinNodeTypes.LLM, # Use a real node type
node_execution_id=node_execution_id,
- user=setup_account,
+ user=account,
)
# Save the variable - this will trigger offloading due to large size
@@ -325,10 +344,10 @@ class TestDraftVariableLoader(unittest.TestCase):
# Now test loading using DraftVarLoader
var_loader = DraftVarLoader(
- engine=db.engine,
+ engine=self._engine,
app_id=self._test_app_id,
tenant_id=self._test_tenant_id,
- user_id=setup_account.id,
+ user_id=account.id,
)
# Load the variable using the standard workflow
@@ -339,17 +358,16 @@ class TestDraftVariableLoader(unittest.TestCase):
loaded_variable = variables[0]
assert loaded_variable.name == "offloaded_string_var"
assert loaded_variable.selector == ["test_offload_node", "offloaded_string_var"]
- assert isinstance(loaded_variable.value, StringSegment)
- assert loaded_variable.value.value == test_content
+ assert loaded_variable.value == test_content
finally:
# Clean up - delete all draft variables for this app
- with Session(bind=db.engine) as session:
+ with Session(bind=self._engine) as session:
service = WorkflowDraftVariableService(session)
service.delete_app_workflow_variables(self._test_app_id)
session.commit()
- def test_load_offloaded_variable_object_type_integration(self):
+ def test_load_offloaded_variable_object_type_integration(self) -> None:
"""Test _load_offloaded_variable with object type using real storage and service."""
# Create a test object
@@ -385,11 +403,12 @@ class TestDraftVariableLoader(unittest.TestCase):
app_id=self._test_app_id,
user_id=str(uuid.uuid4()),
size=len(content_bytes),
+ length=len(test_object),
created_at=datetime_utils.naive_utc_now(),
)
try:
- with Session(bind=db.engine, expire_on_commit=False) as session:
+ with Session(bind=self._engine, expire_on_commit=False) as session:
# Add upload file and variable file first to get their IDs
session.add_all([upload_file, variable_file])
session.flush() # This generates the IDs
@@ -424,7 +443,7 @@ class TestDraftVariableLoader(unittest.TestCase):
# Create DraftVarLoader and test loading
var_loader = DraftVarLoader(
- engine=db.engine,
+ engine=self._engine,
app_id=self._test_app_id,
tenant_id=self._test_tenant_id,
user_id=self._test_user_id,
@@ -437,11 +456,11 @@ class TestDraftVariableLoader(unittest.TestCase):
assert selector_tuple == ("test_offload_node", "offloaded_object_var")
assert variable.id == loaded_var.id
assert variable.name == "offloaded_object_var"
- assert variable.value.value == test_object
+ assert variable.value == test_object
finally:
# Clean up
- with Session(bind=db.engine) as session:
+ with Session(bind=self._engine) as session:
# Query and delete by ID to ensure they're tracked in this session
session.execute(delete(WorkflowDraftVariable).where(WorkflowDraftVariable.id == offloaded_var.id))
session.execute(
@@ -449,13 +468,9 @@ class TestDraftVariableLoader(unittest.TestCase):
)
session.execute(delete(UploadFile).where(UploadFile.id == upload_file.id))
session.commit()
- # Clean up storage
- try:
- storage.delete(upload_file.key)
- except Exception:
- pass # Ignore cleanup failures
+ storage.delete(upload_file.key)
- def test_load_variables_with_offloaded_variables_integration(self):
+ def test_load_variables_with_offloaded_variables_integration(self) -> None:
"""Test load_variables method with mix of regular and offloaded variables using real storage."""
# Create a regular variable (already exists from setUp)
# Create offloaded variable content
@@ -490,11 +505,12 @@ class TestDraftVariableLoader(unittest.TestCase):
app_id=self._test_app_id,
user_id=str(uuid.uuid4()),
size=len(content_bytes),
+ length=None,
created_at=datetime_utils.naive_utc_now(),
)
try:
- with Session(bind=db.engine, expire_on_commit=False) as session:
+ with Session(bind=self._engine, expire_on_commit=False) as session:
# Add upload file and variable file first to get their IDs
session.add_all([upload_file, variable_file])
session.flush() # This generates the IDs
@@ -518,7 +534,7 @@ class TestDraftVariableLoader(unittest.TestCase):
# Test load_variables with both regular and offloaded variables
# This method should handle the relationship preloading internally
var_loader = DraftVarLoader(
- engine=db.engine,
+ engine=self._engine,
app_id=self._test_app_id,
tenant_id=self._test_tenant_id,
user_id=self._test_user_id,
@@ -546,7 +562,7 @@ class TestDraftVariableLoader(unittest.TestCase):
finally:
# Clean up
- with Session(bind=db.engine) as session:
+ with Session(bind=self._engine) as session:
# Query and delete by ID to ensure they're tracked in this session
session.execute(delete(WorkflowDraftVariable).where(WorkflowDraftVariable.id == offloaded_var.id))
session.execute(
@@ -554,14 +570,9 @@ class TestDraftVariableLoader(unittest.TestCase):
)
session.execute(delete(UploadFile).where(UploadFile.id == upload_file.id))
session.commit()
- # Clean up storage
- try:
- storage.delete(upload_file.key)
- except Exception:
- pass # Ignore cleanup failures
+ storage.delete(upload_file.key)
[email protected]("flask_req_ctx")
class TestWorkflowDraftVariableServiceResetVariable(unittest.TestCase):
"""Integration tests for reset_variable functionality using real database"""
@@ -569,18 +580,24 @@ class TestWorkflowDraftVariableServiceResetVariable(unittest.TestCase):
_test_tenant_id: str
_test_workflow_id: str
_session: Session
+ _engine: Engine
_node_id = "test_reset_node"
_node_exec_id: str
_workflow_node_exec_id: str
+ @pytest.fixture(autouse=True)
+ def _bind_sqlite(self, sqlite_session: Session, sqlite_engine: Engine) -> None:
+ self._session = sqlite_session
+ self._engine = sqlite_engine
+
@override
- def setUp(self):
+ def setUp(self) -> None:
self._test_app_id = str(uuid.uuid4())
self._test_tenant_id = str(uuid.uuid4())
self._test_workflow_id = str(uuid.uuid4())
+ self._test_user_id = str(uuid.uuid4())
self._node_exec_id = str(uuid.uuid4())
self._workflow_node_exec_id = str(uuid.uuid4())
- self._session: Session = db.session()
# Create a workflow node execution record with outputs
# Note: The WorkflowNodeExecutionModel.id should match the node_execution_id in WorkflowDraftVariable
@@ -660,13 +677,13 @@ class TestWorkflowDraftVariableServiceResetVariable(unittest.TestCase):
)
self._conv_var.last_edited_at = datetime_utils.naive_utc_now()
- with Session(db.engine, expire_on_commit=False) as persistent_session, persistent_session.begin():
+ with Session(self._engine, expire_on_commit=False) as persistent_session, persistent_session.begin():
persistent_session.add(
self._workflow_node_execution,
)
# Add all to database
- db.session.add_all(
+ self._session.add_all(
[
self._node_var_with_exec,
self._node_var_without_exec,
@@ -674,7 +691,7 @@ class TestWorkflowDraftVariableServiceResetVariable(unittest.TestCase):
self._conv_var,
]
)
- db.session.flush()
+ self._session.flush()
# Store IDs for assertions
self._node_var_with_exec_id = self._node_var_with_exec.id
@@ -683,9 +700,9 @@ class TestWorkflowDraftVariableServiceResetVariable(unittest.TestCase):
self._conv_var_id = self._conv_var.id
@override
- def tearDown(self):
+ def tearDown(self) -> None:
self._session.rollback()
- with Session(db.engine) as session, session.begin():
+ with Session(self._engine) as session, session.begin():
stmt = delete(WorkflowNodeExecutionModel).where(
WorkflowNodeExecutionModel.id == self._workflow_node_execution.id
)
@@ -699,7 +716,7 @@ class TestWorkflowDraftVariableServiceResetVariable(unittest.TestCase):
conversation_vars = self._conv_variables
# Create a simple graph with the test node
- graph = {
+ graph: dict[str, object] = {
"nodes": [{"id": "test_reset_node", "type": "llm", "title": "Test Node", "data": {"type": "llm"}}],
"edges": [],
}
@@ -718,102 +735,7 @@ class TestWorkflowDraftVariableServiceResetVariable(unittest.TestCase):
)
return workflow
- def test_reset_node_variable_with_valid_execution_record(self):
- """Test resetting a node variable with valid execution record - should restore from execution"""
- srv = self._get_test_srv()
- mock_workflow = self._create_mock_workflow()
-
- # Get the variable before reset
- variable = srv.get_variable(self._node_var_with_exec_id)
- assert variable is not None
- assert variable.get_value().value == "old_value"
- assert variable.last_edited_at is not None
-
- # Reset the variable
- result = srv.reset_variable(mock_workflow, variable)
-
- # Should return the updated variable
- assert result is not None
- assert result.id == self._node_var_with_exec_id
- assert result.node_execution_id == self._workflow_node_execution.id
- assert result.last_edited_at is None # Should be reset to None
-
- # The returned variable should have the updated value from execution record
- assert result.get_value().value == "output_value"
-
- # Verify the variable was updated in database
- updated_variable = srv.get_variable(self._node_var_with_exec_id)
- assert updated_variable is not None
- # The value should be updated from the execution record's outputs
- assert updated_variable.get_value().value == "output_value"
- assert updated_variable.last_edited_at is None
- assert updated_variable.node_execution_id == self._workflow_node_execution.id
-
- def test_reset_node_variable_with_no_execution_id(self):
- """Test resetting a node variable with no execution ID - should delete variable"""
- srv = self._get_test_srv()
- mock_workflow = self._create_mock_workflow()
-
- # Get the variable before reset
- variable = srv.get_variable(self._node_var_without_exec_id)
- assert variable is not None
-
- # Reset the variable
- result = srv.reset_variable(mock_workflow, variable)
-
- # Should return None (variable deleted)
- assert result is None
-
- # Verify the variable was deleted
- deleted_variable = srv.get_variable(self._node_var_without_exec_id)
- assert deleted_variable is None
-
- def test_reset_node_variable_with_missing_execution_record(self):
- """Test resetting a node variable when execution record doesn't exist"""
- srv = self._get_test_srv()
- mock_workflow = self._create_mock_workflow()
-
- # Get the variable before reset
- variable = srv.get_variable(self._node_var_missing_exec_id)
- assert variable is not None
-
- # Reset the variable
- result = srv.reset_variable(mock_workflow, variable)
-
- # Should return None (variable deleted)
- assert result is None
-
- # Verify the variable was deleted
- deleted_variable = srv.get_variable(self._node_var_missing_exec_id)
- assert deleted_variable is None
-
- def test_reset_conversation_variable(self):
- """Test resetting a conversation variable"""
- srv = self._get_test_srv()
- mock_workflow = self._create_mock_workflow()
-
- # Get the variable before reset
- variable = srv.get_variable(self._conv_var_id)
- assert variable is not None
- assert variable.get_value().value == "old_conv_value"
- assert variable.last_edited_at is not None
-
- # Reset the variable
- result = srv.reset_variable(mock_workflow, variable)
-
- # Should return the updated variable
- assert result is not None
- assert result.id == self._conv_var_id
- assert result.last_edited_at is None # Should be reset to None
-
- # Verify the variable was updated with default value from workflow
- updated_variable = srv.get_variable(self._conv_var_id)
- assert updated_variable is not None
- # The value should be updated from the workflow's conversation variable default
- assert updated_variable.get_value().value == "default_value_1"
- assert updated_variable.last_edited_at is None
-
- def test_reset_system_variable_raises_error(self):
+ def test_reset_system_variable_raises_error(self) -> None:
"""Test that resetting a system variable raises an error"""
srv = self._get_test_srv()
mock_workflow = self._create_mock_workflow()
@@ -826,8 +748,8 @@ class TestWorkflowDraftVariableServiceResetVariable(unittest.TestCase):
value=build_segment("sys_value"),
node_execution_id=self._node_exec_id,
)
- db.session.add(sys_var)
- db.session.flush()
+ self._session.add(sys_var)
+ self._session.flush()
# Attempt to reset the system variable
with pytest.raises(VariableResetError) as exc_info: