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.

refactor: migrate conversation variable updater tests to testcontainers (#33903)

Desel72a71b7909fdd2
diff --git a/api/tests/test_containers_integration_tests/services/test_conversation_variable_updater.py b/api/tests/test_containers_integration_tests/services/test_conversation_variable_updater.py
new file mode 100644
index 00000000000..42a22158967
--- /dev/null
+++ b/api/tests/test_containers_integration_tests/services/test_conversation_variable_updater.py
@@ -0,0 +1,58 @@
+"""Testcontainers integration tests for ConversationVariableUpdater."""
+
+from uuid import uuid4
+
+import pytest
+from sqlalchemy.orm import sessionmaker
+
+from dify_graph.variables import StringVariable
+from extensions.ext_database import db
+from models.workflow import ConversationVariable
+from services.conversation_variable_updater import ConversationVariableNotFoundError, ConversationVariableUpdater
+
+
+class TestConversationVariableUpdater:
+ def _create_conversation_variable(
+ self, db_session_with_containers, *, conversation_id: str, variable: StringVariable, app_id: str | None = None
+ ) -> ConversationVariable:
+ row = ConversationVariable(
+ id=variable.id,
+ conversation_id=conversation_id,
+ app_id=app_id or str(uuid4()),
+ data=variable.model_dump_json(),
+ )
+ db_session_with_containers.add(row)
+ db_session_with_containers.commit()
+ return row
+
+ def test_should_update_conversation_variable_data_and_commit(self, db_session_with_containers):
+ conversation_id = str(uuid4())
+ variable = StringVariable(id=str(uuid4()), name="topic", value="old value")
+ self._create_conversation_variable(
+ db_session_with_containers, conversation_id=conversation_id, variable=variable
+ )
+
+ updated_variable = StringVariable(id=variable.id, name="topic", value="new value")
+ updater = ConversationVariableUpdater(sessionmaker(bind=db.engine))
+
+ updater.update(conversation_id=conversation_id, variable=updated_variable)
+
+ db_session_with_containers.expire_all()
+ row = db_session_with_containers.get(ConversationVariable, (variable.id, conversation_id))
+ assert row is not None
+ assert row.data == updated_variable.model_dump_json()
+
+ def test_should_raise_not_found_when_variable_missing(self, db_session_with_containers):
+ conversation_id = str(uuid4())
+ variable = StringVariable(id=str(uuid4()), name="topic", value="value")
+ updater = ConversationVariableUpdater(sessionmaker(bind=db.engine))
+
+ with pytest.raises(ConversationVariableNotFoundError, match="conversation variable not found in the database"):
+ updater.update(conversation_id=conversation_id, variable=variable)
+
+ def test_should_do_nothing_when_flush_is_called(self, db_session_with_containers):
+ updater = ConversationVariableUpdater(sessionmaker(bind=db.engine))
+
+ result = updater.flush()
+
+ assert result is None
diff --git a/api/tests/unit_tests/services/test_conversation_variable_updater.py b/api/tests/unit_tests/services/test_conversation_variable_updater.py
deleted file mode 100644
index 20f7caa78e9..00000000000
--- a/api/tests/unit_tests/services/test_conversation_variable_updater.py
+++ /dev/null
@@ -1,75 +0,0 @@
-from types import SimpleNamespace
-from unittest.mock import MagicMock
-
-import pytest
-
-from dify_graph.variables import StringVariable
-from services.conversation_variable_updater import ConversationVariableNotFoundError, ConversationVariableUpdater
-
-
-class TestConversationVariableUpdater:
- def test_should_update_conversation_variable_data_and_commit(self):
- """Test update persists serialized variable data when the row exists."""
- conversation_id = "conv-123"
- variable = StringVariable(
- id="var-123",
- name="topic",
- value="new value",
- )
- expected_json = variable.model_dump_json()
-
- row = SimpleNamespace(data="old value")
- session = MagicMock()
- session.scalar.return_value = row
-
- session_context = MagicMock()
- session_context.__enter__.return_value = session
- session_context.__exit__.return_value = None
-
- session_maker = MagicMock(return_value=session_context)
- updater = ConversationVariableUpdater(session_maker)
-
- updater.update(conversation_id=conversation_id, variable=variable)
-
- session_maker.assert_called_once_with()
- session.scalar.assert_called_once()
- stmt = session.scalar.call_args.args[0]
- compiled_params = stmt.compile().params
- assert variable.id in compiled_params.values()
- assert conversation_id in compiled_params.values()
- assert row.data == expected_json
- session.commit.assert_called_once()
-
- def test_should_raise_not_found_error_when_conversation_variable_missing(self):
- """Test update raises ConversationVariableNotFoundError when no matching row exists."""
- conversation_id = "conv-404"
- variable = StringVariable(
- id="var-404",
- name="topic",
- value="value",
- )
-
- session = MagicMock()
- session.scalar.return_value = None
-
- session_context = MagicMock()
- session_context.__enter__.return_value = session
- session_context.__exit__.return_value = None
-
- session_maker = MagicMock(return_value=session_context)
- updater = ConversationVariableUpdater(session_maker)
-
- with pytest.raises(ConversationVariableNotFoundError, match="conversation variable not found in the database"):
- updater.update(conversation_id=conversation_id, variable=variable)
-
- session.commit.assert_not_called()
-
- def test_should_do_nothing_when_flush_is_called(self):
- """Test flush currently behaves as a no-op and returns None."""
- session_maker = MagicMock()
- updater = ConversationVariableUpdater(session_maker)
-
- result = updater.flush()
-
- assert result is None
- session_maker.assert_not_called()