mirror of
https://github.com/langgenius/dify.git
synced 2026-05-03 08:58:09 +08:00
test(api): add autospec to MagicMock-based patch usage (#32752)
This commit is contained in:
@ -301,7 +301,7 @@ class TestAppModelConfig:
|
||||
)
|
||||
|
||||
# Mock database query to return None
|
||||
with patch("models.model.db.session.query") as mock_query:
|
||||
with patch("models.model.db.session.query", autospec=True) as mock_query:
|
||||
mock_query.return_value.where.return_value.first.return_value = None
|
||||
|
||||
# Act
|
||||
@ -952,7 +952,7 @@ class TestSiteModel:
|
||||
def test_site_generate_code(self):
|
||||
"""Test Site.generate_code static method."""
|
||||
# Mock database query to return 0 (no existing codes)
|
||||
with patch("models.model.db.session.query") as mock_query:
|
||||
with patch("models.model.db.session.query", autospec=True) as mock_query:
|
||||
mock_query.return_value.where.return_value.count.return_value = 0
|
||||
|
||||
# Act
|
||||
@ -1167,7 +1167,7 @@ class TestConversationStatusCount:
|
||||
conversation.id = str(uuid4())
|
||||
|
||||
# Mock the database query to return no messages
|
||||
with patch("models.model.db.session.scalars") as mock_scalars:
|
||||
with patch("models.model.db.session.scalars", autospec=True) as mock_scalars:
|
||||
mock_scalars.return_value.all.return_value = []
|
||||
|
||||
# Act
|
||||
@ -1192,7 +1192,7 @@ class TestConversationStatusCount:
|
||||
conversation.id = conversation_id
|
||||
|
||||
# Mock the database query to return no messages with workflow_run_id
|
||||
with patch("models.model.db.session.scalars") as mock_scalars:
|
||||
with patch("models.model.db.session.scalars", autospec=True) as mock_scalars:
|
||||
mock_scalars.return_value.all.return_value = []
|
||||
|
||||
# Act
|
||||
@ -1277,7 +1277,7 @@ class TestConversationStatusCount:
|
||||
return mock_result
|
||||
|
||||
# Act & Assert
|
||||
with patch("models.model.db.session.scalars", side_effect=mock_scalars):
|
||||
with patch("models.model.db.session.scalars", side_effect=mock_scalars, autospec=True):
|
||||
result = conversation.status_count
|
||||
|
||||
# Verify only 2 database queries were made (not N+1)
|
||||
@ -1340,7 +1340,7 @@ class TestConversationStatusCount:
|
||||
return mock_result
|
||||
|
||||
# Act
|
||||
with patch("models.model.db.session.scalars", side_effect=mock_scalars):
|
||||
with patch("models.model.db.session.scalars", side_effect=mock_scalars, autospec=True):
|
||||
result = conversation.status_count
|
||||
|
||||
# Assert - query should include app_id filter
|
||||
@ -1385,7 +1385,7 @@ class TestConversationStatusCount:
|
||||
),
|
||||
]
|
||||
|
||||
with patch("models.model.db.session.scalars") as mock_scalars:
|
||||
with patch("models.model.db.session.scalars", autospec=True) as mock_scalars:
|
||||
# Mock the messages query
|
||||
def mock_scalars_side_effect(query):
|
||||
mock_result = MagicMock()
|
||||
@ -1441,7 +1441,7 @@ class TestConversationStatusCount:
|
||||
),
|
||||
]
|
||||
|
||||
with patch("models.model.db.session.scalars") as mock_scalars:
|
||||
with patch("models.model.db.session.scalars", autospec=True) as mock_scalars:
|
||||
|
||||
def mock_scalars_side_effect(query):
|
||||
mock_result = MagicMock()
|
||||
|
||||
Reference in New Issue
Block a user