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(models): pass session into recommended-app and trial-record accessors (#42325)

xiaodu558b3c4db9ac4d
diff --git a/api/models/model.py b/api/models/model.py
index 1af4d50c163..79f52eab5a1 100644
--- a/api/models/model.py
+++ b/api/models/model.py
@@ -449,10 +449,6 @@ class App(Base):
)
use_icon_as_answer_icon: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=sa.false())
- @property
- def desc_or_prompt(self) -> str:
- return self.desc_or_prompt_with_session(session=db.session())
-
def desc_or_prompt_with_session(self, *, session: Session) -> str:
if self.description:
return self.description
@@ -979,9 +975,8 @@ class RecommendedApp(TypeBase):
init=False,
)
- @property
- def app(self) -> App | None:
- return db.session.scalar(select(App).where(App.id == self.app_id))
+ def app(self, *, session: Session) -> App | None:
+ return session.scalar(select(App).where(App.id == self.app_id))
class InstalledApp(TypeBase):
@@ -1054,13 +1049,11 @@ class AccountTrialAppRecord(TypeBase):
sa.DateTime, nullable=False, server_default=func.current_timestamp(), init=False
)
- @property
- def app(self) -> App | None:
- return db.session.scalar(select(App).where(App.id == self.app_id))
+ def app(self, *, session: Session) -> App | None:
+ return session.scalar(select(App).where(App.id == self.app_id))
- @property
- def user(self) -> Account | None:
- return db.session.scalar(select(Account).where(Account.id == self.account_id))
+ def user(self, *, session: Session) -> Account | None:
+ return session.scalar(select(Account).where(Account.id == self.account_id))
class ExporleBanner(TypeBase):
diff --git a/api/tests/unit_tests/models/test_app_models.py b/api/tests/unit_tests/models/test_app_models.py
index 7485031575a..7d0a42ebcb8 100644
--- a/api/tests/unit_tests/models/test_app_models.py
+++ b/api/tests/unit_tests/models/test_app_models.py
@@ -122,8 +122,9 @@ class TestAppModelValidation:
# Assert
assert {t.value for t in IconType} == {"image", "emoji", "link"}
- def test_app_desc_or_prompt_with_description(self):
- """Test desc_or_prompt property when description exists."""
+ @pytest.mark.parametrize("sqlite_session", [(App, AppModelConfig)], indirect=True)
+ def test_app_desc_or_prompt_with_description(self, sqlite_session: Session):
+ """Test desc_or_prompt_with_session when description exists."""
# Arrange
app = App(
tenant_id=str(uuid4()),
@@ -136,7 +137,7 @@ class TestAppModelValidation:
)
# Act
- result = app.desc_or_prompt
+ result = app.desc_or_prompt_with_session(session=sqlite_session)
# Assert
assert result == "App description"
diff --git a/api/tests/unit_tests/models/test_recommended_app_session_accessors.py b/api/tests/unit_tests/models/test_recommended_app_session_accessors.py
new file mode 100644
index 00000000000..e560144b66f
--- /dev/null
+++ b/api/tests/unit_tests/models/test_recommended_app_session_accessors.py
@@ -0,0 +1,102 @@
+"""Regression coverage for the ``@property``→session-parameter refactor on
+``RecommendedApp`` and ``AccountTrialAppRecord``.
+
+The legacy ``@property`` accessors reached for the global ``db.session`` internally and have
+been converted to plain methods taking an explicit ``session: Session`` (per the pattern
+established in #40370/#41394/#41830/#42000, tracked in #40372).
+"""
+
+from uuid import uuid4
+
+from sqlalchemy.orm import Session
+
+from models.account import Account
+from models.model import AccountTrialAppRecord, App, AppMode, RecommendedApp
+
+
+def _persist_app(session: Session) -> App:
+ app = App(
+ tenant_id=str(uuid4()),
+ name="Test App",
+ mode=AppMode.CHAT,
+ enable_site=True,
+ enable_api=False,
+ created_by=str(uuid4()),
+ description="App description",
+ )
+ session.add(app)
+ session.flush()
+ return app
+
+
+def _persist_recommended_app(session: Session, *, app_id: str) -> RecommendedApp:
+ recommended_app = RecommendedApp(
+ app_id=app_id,
+ description={"en_US": "A recommended app"},
+ copyright="Dify",
+ privacy_policy="https://dify.ai/privacy",
+ category="Assistant",
+ )
+ session.add(recommended_app)
+ session.flush()
+ return recommended_app
+
+
+def _persist_account(session: Session) -> Account:
+ account = Account(name="Test User", email=f"user-{uuid4()}@example.com")
+ session.add(account)
+ session.flush()
+ return account
+
+
+def _persist_trial_record(session: Session, *, account_id: str, app_id: str) -> AccountTrialAppRecord:
+ record = AccountTrialAppRecord(account_id=account_id, app_id=app_id, count=1)
+ session.add(record)
+ session.flush()
+ return record
+
+
+class TestRecommendedAppApp:
+ def test_returns_the_matching_app(self, sqlite_session: Session) -> None:
+ app = _persist_app(sqlite_session)
+ recommended_app = _persist_recommended_app(sqlite_session, app_id=app.id)
+
+ result = recommended_app.app(session=sqlite_session)
+
+ assert result is not None
+ assert result.id == app.id
+
+ def test_returns_none_when_app_missing(self, sqlite_session: Session) -> None:
+ recommended_app = _persist_recommended_app(sqlite_session, app_id=str(uuid4()))
+
+ assert recommended_app.app(session=sqlite_session) is None
+
+
+class TestAccountTrialAppRecordAccessors:
+ def test_app_returns_the_matching_app(self, sqlite_session: Session) -> None:
+ app = _persist_app(sqlite_session)
+ record = _persist_trial_record(sqlite_session, account_id=str(uuid4()), app_id=app.id)
+
+ result = record.app(session=sqlite_session)
+
+ assert result is not None
+ assert result.id == app.id
+
+ def test_app_returns_none_when_app_missing(self, sqlite_session: Session) -> None:
+ record = _persist_trial_record(sqlite_session, account_id=str(uuid4()), app_id=str(uuid4()))
+
+ assert record.app(session=sqlite_session) is None
+
+ def test_user_returns_the_matching_account(self, sqlite_session: Session) -> None:
+ account = _persist_account(sqlite_session)
+ record = _persist_trial_record(sqlite_session, account_id=account.id, app_id=str(uuid4()))
+
+ result = record.user(session=sqlite_session)
+
+ assert result is not None
+ assert result.id == account.id
+
+ def test_user_returns_none_when_account_missing(self, sqlite_session: Session) -> None:
+ record = _persist_trial_record(sqlite_session, account_id=str(uuid4()), app_id=str(uuid4()))
+
+ assert record.user(session=sqlite_session) is None