more assert (#24996)
Signed-off-by: -LAN- <laipz8200@outlook.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com>
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import json
|
||||
from unittest.mock import MagicMock, patch
|
||||
from unittest.mock import MagicMock, create_autospec, patch
|
||||
|
||||
import pytest
|
||||
from faker import Faker
|
||||
|
||||
from core.plugin.impl.exc import PluginDaemonClientSideError
|
||||
from models.account import Account
|
||||
from models.model import AppModelConfig, Conversation, EndUser, Message, MessageAgentThought
|
||||
from services.account_service import AccountService, TenantService
|
||||
from services.agent_service import AgentService
|
||||
@@ -21,7 +22,7 @@ class TestAgentService:
|
||||
patch("services.agent_service.PluginAgentClient") as mock_plugin_agent_client,
|
||||
patch("services.agent_service.ToolManager") as mock_tool_manager,
|
||||
patch("services.agent_service.AgentConfigManager") as mock_agent_config_manager,
|
||||
patch("services.agent_service.current_user") as mock_current_user,
|
||||
patch("services.agent_service.current_user", create_autospec(Account, instance=True)) as mock_current_user,
|
||||
patch("services.app_service.FeatureService") as mock_feature_service,
|
||||
patch("services.app_service.EnterpriseService") as mock_enterprise_service,
|
||||
patch("services.app_service.ModelManager") as mock_model_manager,
|
||||
|
@@ -1,9 +1,10 @@
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import create_autospec, patch
|
||||
|
||||
import pytest
|
||||
from faker import Faker
|
||||
from werkzeug.exceptions import NotFound
|
||||
|
||||
from models.account import Account
|
||||
from models.model import MessageAnnotation
|
||||
from services.annotation_service import AppAnnotationService
|
||||
from services.app_service import AppService
|
||||
@@ -24,7 +25,9 @@ class TestAnnotationService:
|
||||
patch("services.annotation_service.enable_annotation_reply_task") as mock_enable_task,
|
||||
patch("services.annotation_service.disable_annotation_reply_task") as mock_disable_task,
|
||||
patch("services.annotation_service.batch_import_annotations_task") as mock_batch_import_task,
|
||||
patch("services.annotation_service.current_user") as mock_current_user,
|
||||
patch(
|
||||
"services.annotation_service.current_user", create_autospec(Account, instance=True)
|
||||
) as mock_current_user,
|
||||
):
|
||||
# Setup default mock returns
|
||||
mock_account_feature_service.get_features.return_value.billing.enabled = False
|
||||
|
@@ -1,9 +1,10 @@
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import create_autospec, patch
|
||||
|
||||
import pytest
|
||||
from faker import Faker
|
||||
|
||||
from constants.model_template import default_app_templates
|
||||
from models.account import Account
|
||||
from models.model import App, Site
|
||||
from services.account_service import AccountService, TenantService
|
||||
from services.app_service import AppService
|
||||
@@ -161,8 +162,13 @@ class TestAppService:
|
||||
app_service = AppService()
|
||||
created_app = app_service.create_app(tenant.id, app_args, account)
|
||||
|
||||
# Get app using the service
|
||||
retrieved_app = app_service.get_app(created_app)
|
||||
# Get app using the service - needs current_user mock
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.id = account.id
|
||||
mock_current_user.current_tenant_id = account.current_tenant_id
|
||||
|
||||
with patch("services.app_service.current_user", mock_current_user):
|
||||
retrieved_app = app_service.get_app(created_app)
|
||||
|
||||
# Verify retrieved app matches created app
|
||||
assert retrieved_app.id == created_app.id
|
||||
@@ -406,7 +412,11 @@ class TestAppService:
|
||||
"use_icon_as_answer_icon": True,
|
||||
}
|
||||
|
||||
with patch("flask_login.utils._get_user", return_value=account):
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.id = account.id
|
||||
mock_current_user.current_tenant_id = account.current_tenant_id
|
||||
|
||||
with patch("services.app_service.current_user", mock_current_user):
|
||||
updated_app = app_service.update_app(app, update_args)
|
||||
|
||||
# Verify updated fields
|
||||
@@ -456,7 +466,11 @@ class TestAppService:
|
||||
|
||||
# Update app name
|
||||
new_name = "New App Name"
|
||||
with patch("flask_login.utils._get_user", return_value=account):
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.id = account.id
|
||||
mock_current_user.current_tenant_id = account.current_tenant_id
|
||||
|
||||
with patch("services.app_service.current_user", mock_current_user):
|
||||
updated_app = app_service.update_app_name(app, new_name)
|
||||
|
||||
assert updated_app.name == new_name
|
||||
@@ -504,7 +518,11 @@ class TestAppService:
|
||||
# Update app icon
|
||||
new_icon = "🌟"
|
||||
new_icon_background = "#FFD93D"
|
||||
with patch("flask_login.utils._get_user", return_value=account):
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.id = account.id
|
||||
mock_current_user.current_tenant_id = account.current_tenant_id
|
||||
|
||||
with patch("services.app_service.current_user", mock_current_user):
|
||||
updated_app = app_service.update_app_icon(app, new_icon, new_icon_background)
|
||||
|
||||
assert updated_app.icon == new_icon
|
||||
@@ -551,13 +569,17 @@ class TestAppService:
|
||||
original_site_status = app.enable_site
|
||||
|
||||
# Update site status to disabled
|
||||
with patch("flask_login.utils._get_user", return_value=account):
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.id = account.id
|
||||
mock_current_user.current_tenant_id = account.current_tenant_id
|
||||
|
||||
with patch("services.app_service.current_user", mock_current_user):
|
||||
updated_app = app_service.update_app_site_status(app, False)
|
||||
assert updated_app.enable_site is False
|
||||
assert updated_app.updated_by == account.id
|
||||
|
||||
# Update site status back to enabled
|
||||
with patch("flask_login.utils._get_user", return_value=account):
|
||||
with patch("services.app_service.current_user", mock_current_user):
|
||||
updated_app = app_service.update_app_site_status(updated_app, True)
|
||||
assert updated_app.enable_site is True
|
||||
assert updated_app.updated_by == account.id
|
||||
@@ -602,13 +624,17 @@ class TestAppService:
|
||||
original_api_status = app.enable_api
|
||||
|
||||
# Update API status to disabled
|
||||
with patch("flask_login.utils._get_user", return_value=account):
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.id = account.id
|
||||
mock_current_user.current_tenant_id = account.current_tenant_id
|
||||
|
||||
with patch("services.app_service.current_user", mock_current_user):
|
||||
updated_app = app_service.update_app_api_status(app, False)
|
||||
assert updated_app.enable_api is False
|
||||
assert updated_app.updated_by == account.id
|
||||
|
||||
# Update API status back to enabled
|
||||
with patch("flask_login.utils._get_user", return_value=account):
|
||||
with patch("services.app_service.current_user", mock_current_user):
|
||||
updated_app = app_service.update_app_api_status(updated_app, True)
|
||||
assert updated_app.enable_api is True
|
||||
assert updated_app.updated_by == account.id
|
||||
|
@@ -1,6 +1,6 @@
|
||||
import hashlib
|
||||
from io import BytesIO
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import create_autospec, patch
|
||||
|
||||
import pytest
|
||||
from faker import Faker
|
||||
@@ -417,11 +417,12 @@ class TestFileService:
|
||||
text = "This is a test text content"
|
||||
text_name = "test_text.txt"
|
||||
|
||||
# Mock current_user
|
||||
with patch("services.file_service.current_user") as mock_current_user:
|
||||
mock_current_user.current_tenant_id = str(fake.uuid4())
|
||||
mock_current_user.id = str(fake.uuid4())
|
||||
# Mock current_user using create_autospec
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.current_tenant_id = str(fake.uuid4())
|
||||
mock_current_user.id = str(fake.uuid4())
|
||||
|
||||
with patch("services.file_service.current_user", mock_current_user):
|
||||
upload_file = FileService.upload_text(text=text, text_name=text_name)
|
||||
|
||||
assert upload_file is not None
|
||||
@@ -443,11 +444,12 @@ class TestFileService:
|
||||
text = "test content"
|
||||
long_name = "a" * 250 # Longer than 200 characters
|
||||
|
||||
# Mock current_user
|
||||
with patch("services.file_service.current_user") as mock_current_user:
|
||||
mock_current_user.current_tenant_id = str(fake.uuid4())
|
||||
mock_current_user.id = str(fake.uuid4())
|
||||
# Mock current_user using create_autospec
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.current_tenant_id = str(fake.uuid4())
|
||||
mock_current_user.id = str(fake.uuid4())
|
||||
|
||||
with patch("services.file_service.current_user", mock_current_user):
|
||||
upload_file = FileService.upload_text(text=text, text_name=long_name)
|
||||
|
||||
# Verify name was truncated
|
||||
@@ -846,11 +848,12 @@ class TestFileService:
|
||||
text = ""
|
||||
text_name = "empty.txt"
|
||||
|
||||
# Mock current_user
|
||||
with patch("services.file_service.current_user") as mock_current_user:
|
||||
mock_current_user.current_tenant_id = str(fake.uuid4())
|
||||
mock_current_user.id = str(fake.uuid4())
|
||||
# Mock current_user using create_autospec
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.current_tenant_id = str(fake.uuid4())
|
||||
mock_current_user.id = str(fake.uuid4())
|
||||
|
||||
with patch("services.file_service.current_user", mock_current_user):
|
||||
upload_file = FileService.upload_text(text=text, text_name=text_name)
|
||||
|
||||
assert upload_file is not None
|
||||
|
@@ -1,4 +1,4 @@
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import create_autospec, patch
|
||||
|
||||
import pytest
|
||||
from faker import Faker
|
||||
@@ -17,7 +17,9 @@ class TestMetadataService:
|
||||
def mock_external_service_dependencies(self):
|
||||
"""Mock setup for external service dependencies."""
|
||||
with (
|
||||
patch("services.metadata_service.current_user") as mock_current_user,
|
||||
patch(
|
||||
"services.metadata_service.current_user", create_autospec(Account, instance=True)
|
||||
) as mock_current_user,
|
||||
patch("services.metadata_service.redis_client") as mock_redis_client,
|
||||
patch("services.dataset_service.DocumentService") as mock_document_service,
|
||||
):
|
||||
|
@@ -1,4 +1,4 @@
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import create_autospec, patch
|
||||
|
||||
import pytest
|
||||
from faker import Faker
|
||||
@@ -17,7 +17,7 @@ class TestTagService:
|
||||
def mock_external_service_dependencies(self):
|
||||
"""Mock setup for external service dependencies."""
|
||||
with (
|
||||
patch("services.tag_service.current_user") as mock_current_user,
|
||||
patch("services.tag_service.current_user", create_autospec(Account, instance=True)) as mock_current_user,
|
||||
):
|
||||
# Setup default mock returns
|
||||
mock_current_user.current_tenant_id = "test-tenant-id"
|
||||
|
@@ -1,5 +1,5 @@
|
||||
from datetime import datetime
|
||||
from unittest.mock import MagicMock, patch
|
||||
from unittest.mock import MagicMock, create_autospec, patch
|
||||
|
||||
import pytest
|
||||
from faker import Faker
|
||||
@@ -231,9 +231,10 @@ class TestWebsiteService:
|
||||
fake = Faker()
|
||||
|
||||
# Mock current_user for the test
|
||||
with patch("services.website_service.current_user") as mock_current_user:
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
|
||||
with patch("services.website_service.current_user", mock_current_user):
|
||||
# Create API request
|
||||
api_request = WebsiteCrawlApiRequest(
|
||||
provider="firecrawl",
|
||||
@@ -285,9 +286,10 @@ class TestWebsiteService:
|
||||
account = self._create_test_account(db_session_with_containers, mock_external_service_dependencies)
|
||||
|
||||
# Mock current_user for the test
|
||||
with patch("services.website_service.current_user") as mock_current_user:
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
|
||||
with patch("services.website_service.current_user", mock_current_user):
|
||||
# Create API request
|
||||
api_request = WebsiteCrawlApiRequest(
|
||||
provider="watercrawl",
|
||||
@@ -336,9 +338,10 @@ class TestWebsiteService:
|
||||
account = self._create_test_account(db_session_with_containers, mock_external_service_dependencies)
|
||||
|
||||
# Mock current_user for the test
|
||||
with patch("services.website_service.current_user") as mock_current_user:
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
|
||||
with patch("services.website_service.current_user", mock_current_user):
|
||||
# Create API request for single page crawling
|
||||
api_request = WebsiteCrawlApiRequest(
|
||||
provider="jinareader",
|
||||
@@ -389,9 +392,10 @@ class TestWebsiteService:
|
||||
account = self._create_test_account(db_session_with_containers, mock_external_service_dependencies)
|
||||
|
||||
# Mock current_user for the test
|
||||
with patch("services.website_service.current_user") as mock_current_user:
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
|
||||
with patch("services.website_service.current_user", mock_current_user):
|
||||
# Create API request with invalid provider
|
||||
api_request = WebsiteCrawlApiRequest(
|
||||
provider="invalid_provider",
|
||||
@@ -419,9 +423,10 @@ class TestWebsiteService:
|
||||
account = self._create_test_account(db_session_with_containers, mock_external_service_dependencies)
|
||||
|
||||
# Mock current_user for the test
|
||||
with patch("services.website_service.current_user") as mock_current_user:
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
|
||||
with patch("services.website_service.current_user", mock_current_user):
|
||||
# Create API request
|
||||
api_request = WebsiteCrawlStatusApiRequest(provider="firecrawl", job_id="test_job_id_123")
|
||||
|
||||
@@ -463,9 +468,10 @@ class TestWebsiteService:
|
||||
account = self._create_test_account(db_session_with_containers, mock_external_service_dependencies)
|
||||
|
||||
# Mock current_user for the test
|
||||
with patch("services.website_service.current_user") as mock_current_user:
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
|
||||
with patch("services.website_service.current_user", mock_current_user):
|
||||
# Create API request
|
||||
api_request = WebsiteCrawlStatusApiRequest(provider="watercrawl", job_id="watercrawl_job_123")
|
||||
|
||||
@@ -502,9 +508,10 @@ class TestWebsiteService:
|
||||
account = self._create_test_account(db_session_with_containers, mock_external_service_dependencies)
|
||||
|
||||
# Mock current_user for the test
|
||||
with patch("services.website_service.current_user") as mock_current_user:
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
|
||||
with patch("services.website_service.current_user", mock_current_user):
|
||||
# Create API request
|
||||
api_request = WebsiteCrawlStatusApiRequest(provider="jinareader", job_id="jina_job_123")
|
||||
|
||||
@@ -544,9 +551,10 @@ class TestWebsiteService:
|
||||
account = self._create_test_account(db_session_with_containers, mock_external_service_dependencies)
|
||||
|
||||
# Mock current_user for the test
|
||||
with patch("services.website_service.current_user") as mock_current_user:
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
|
||||
with patch("services.website_service.current_user", mock_current_user):
|
||||
# Create API request with invalid provider
|
||||
api_request = WebsiteCrawlStatusApiRequest(provider="invalid_provider", job_id="test_job_id_123")
|
||||
|
||||
@@ -569,9 +577,10 @@ class TestWebsiteService:
|
||||
account = self._create_test_account(db_session_with_containers, mock_external_service_dependencies)
|
||||
|
||||
# Mock current_user for the test
|
||||
with patch("services.website_service.current_user") as mock_current_user:
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
|
||||
with patch("services.website_service.current_user", mock_current_user):
|
||||
# Mock missing credentials
|
||||
mock_external_service_dependencies["api_key_auth_service"].get_auth_credentials.return_value = None
|
||||
|
||||
@@ -597,9 +606,10 @@ class TestWebsiteService:
|
||||
account = self._create_test_account(db_session_with_containers, mock_external_service_dependencies)
|
||||
|
||||
# Mock current_user for the test
|
||||
with patch("services.website_service.current_user") as mock_current_user:
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
|
||||
with patch("services.website_service.current_user", mock_current_user):
|
||||
# Mock missing API key in config
|
||||
mock_external_service_dependencies["api_key_auth_service"].get_auth_credentials.return_value = {
|
||||
"config": {"base_url": "https://api.example.com"}
|
||||
@@ -995,9 +1005,10 @@ class TestWebsiteService:
|
||||
account = self._create_test_account(db_session_with_containers, mock_external_service_dependencies)
|
||||
|
||||
# Mock current_user for the test
|
||||
with patch("services.website_service.current_user") as mock_current_user:
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
|
||||
with patch("services.website_service.current_user", mock_current_user):
|
||||
# Create API request for sub-page crawling
|
||||
api_request = WebsiteCrawlApiRequest(
|
||||
provider="jinareader",
|
||||
@@ -1054,9 +1065,10 @@ class TestWebsiteService:
|
||||
mock_external_service_dependencies["requests"].get.return_value = mock_failed_response
|
||||
|
||||
# Mock current_user for the test
|
||||
with patch("services.website_service.current_user") as mock_current_user:
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
|
||||
with patch("services.website_service.current_user", mock_current_user):
|
||||
# Create API request
|
||||
api_request = WebsiteCrawlApiRequest(
|
||||
provider="jinareader",
|
||||
@@ -1096,9 +1108,10 @@ class TestWebsiteService:
|
||||
mock_external_service_dependencies["firecrawl_app"].return_value = mock_firecrawl_instance
|
||||
|
||||
# Mock current_user for the test
|
||||
with patch("services.website_service.current_user") as mock_current_user:
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
mock_current_user = create_autospec(Account, instance=True)
|
||||
mock_current_user.current_tenant_id = account.current_tenant.id
|
||||
|
||||
with patch("services.website_service.current_user", mock_current_user):
|
||||
# Create API request
|
||||
api_request = WebsiteCrawlStatusApiRequest(provider="firecrawl", job_id="active_job_123")
|
||||
|
||||
|
@@ -2,11 +2,12 @@ import datetime
|
||||
from typing import Any, Optional
|
||||
|
||||
# Mock redis_client before importing dataset_service
|
||||
from unittest.mock import Mock, patch
|
||||
from unittest.mock import Mock, create_autospec, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from core.model_runtime.entities.model_entities import ModelType
|
||||
from models.account import Account
|
||||
from models.dataset import Dataset, ExternalKnowledgeBindings
|
||||
from services.dataset_service import DatasetService
|
||||
from services.errors.account import NoPermissionError
|
||||
@@ -78,7 +79,7 @@ class DatasetUpdateTestDataFactory:
|
||||
@staticmethod
|
||||
def create_current_user_mock(tenant_id: str = "tenant-123") -> Mock:
|
||||
"""Create a mock current user."""
|
||||
current_user = Mock()
|
||||
current_user = create_autospec(Account, instance=True)
|
||||
current_user.current_tenant_id = tenant_id
|
||||
return current_user
|
||||
|
||||
@@ -135,7 +136,9 @@ class TestDatasetServiceUpdateDataset:
|
||||
"services.dataset_service.DatasetCollectionBindingService.get_dataset_collection_binding"
|
||||
) as mock_get_binding,
|
||||
patch("services.dataset_service.deal_dataset_vector_index_task") as mock_task,
|
||||
patch("services.dataset_service.current_user") as mock_current_user,
|
||||
patch(
|
||||
"services.dataset_service.current_user", create_autospec(Account, instance=True)
|
||||
) as mock_current_user,
|
||||
):
|
||||
mock_current_user.current_tenant_id = "tenant-123"
|
||||
yield {
|
||||
|
@@ -1,9 +1,10 @@
|
||||
from unittest.mock import Mock, patch
|
||||
from unittest.mock import Mock, create_autospec, patch
|
||||
|
||||
import pytest
|
||||
from flask_restx import reqparse
|
||||
from werkzeug.exceptions import BadRequest
|
||||
|
||||
from models.account import Account
|
||||
from services.entities.knowledge_entities.knowledge_entities import MetadataArgs
|
||||
from services.metadata_service import MetadataService
|
||||
|
||||
@@ -35,19 +36,21 @@ class TestMetadataBugCompleteValidation:
|
||||
mock_metadata_args.name = None
|
||||
mock_metadata_args.type = "string"
|
||||
|
||||
with patch("services.metadata_service.current_user") as mock_user:
|
||||
mock_user.current_tenant_id = "tenant-123"
|
||||
mock_user.id = "user-456"
|
||||
mock_user = create_autospec(Account, instance=True)
|
||||
mock_user.current_tenant_id = "tenant-123"
|
||||
mock_user.id = "user-456"
|
||||
|
||||
with patch("services.metadata_service.current_user", mock_user):
|
||||
# Should crash with TypeError
|
||||
with pytest.raises(TypeError, match="object of type 'NoneType' has no len"):
|
||||
MetadataService.create_metadata("dataset-123", mock_metadata_args)
|
||||
|
||||
# Test update method as well
|
||||
with patch("services.metadata_service.current_user") as mock_user:
|
||||
mock_user.current_tenant_id = "tenant-123"
|
||||
mock_user.id = "user-456"
|
||||
mock_user = create_autospec(Account, instance=True)
|
||||
mock_user.current_tenant_id = "tenant-123"
|
||||
mock_user.id = "user-456"
|
||||
|
||||
with patch("services.metadata_service.current_user", mock_user):
|
||||
with pytest.raises(TypeError, match="object of type 'NoneType' has no len"):
|
||||
MetadataService.update_metadata_name("dataset-123", "metadata-456", None)
|
||||
|
||||
|
@@ -1,8 +1,9 @@
|
||||
from unittest.mock import Mock, patch
|
||||
from unittest.mock import Mock, create_autospec, patch
|
||||
|
||||
import pytest
|
||||
from flask_restx import reqparse
|
||||
|
||||
from models.account import Account
|
||||
from services.entities.knowledge_entities.knowledge_entities import MetadataArgs
|
||||
from services.metadata_service import MetadataService
|
||||
|
||||
@@ -24,20 +25,22 @@ class TestMetadataNullableBug:
|
||||
mock_metadata_args.name = None # This will cause len() to crash
|
||||
mock_metadata_args.type = "string"
|
||||
|
||||
with patch("services.metadata_service.current_user") as mock_user:
|
||||
mock_user.current_tenant_id = "tenant-123"
|
||||
mock_user.id = "user-456"
|
||||
mock_user = create_autospec(Account, instance=True)
|
||||
mock_user.current_tenant_id = "tenant-123"
|
||||
mock_user.id = "user-456"
|
||||
|
||||
with patch("services.metadata_service.current_user", mock_user):
|
||||
# This should crash with TypeError when calling len(None)
|
||||
with pytest.raises(TypeError, match="object of type 'NoneType' has no len"):
|
||||
MetadataService.create_metadata("dataset-123", mock_metadata_args)
|
||||
|
||||
def test_metadata_service_update_with_none_name_crashes(self):
|
||||
"""Test that MetadataService.update_metadata_name crashes when name is None."""
|
||||
with patch("services.metadata_service.current_user") as mock_user:
|
||||
mock_user.current_tenant_id = "tenant-123"
|
||||
mock_user.id = "user-456"
|
||||
mock_user = create_autospec(Account, instance=True)
|
||||
mock_user.current_tenant_id = "tenant-123"
|
||||
mock_user.id = "user-456"
|
||||
|
||||
with patch("services.metadata_service.current_user", mock_user):
|
||||
# This should crash with TypeError when calling len(None)
|
||||
with pytest.raises(TypeError, match="object of type 'NoneType' has no len"):
|
||||
MetadataService.update_metadata_name("dataset-123", "metadata-456", None)
|
||||
@@ -81,10 +84,11 @@ class TestMetadataNullableBug:
|
||||
mock_metadata_args.name = None # From args["name"]
|
||||
mock_metadata_args.type = None # From args["type"]
|
||||
|
||||
with patch("services.metadata_service.current_user") as mock_user:
|
||||
mock_user.current_tenant_id = "tenant-123"
|
||||
mock_user.id = "user-456"
|
||||
mock_user = create_autospec(Account, instance=True)
|
||||
mock_user.current_tenant_id = "tenant-123"
|
||||
mock_user.id = "user-456"
|
||||
|
||||
with patch("services.metadata_service.current_user", mock_user):
|
||||
# Step 4: Service layer crashes on len(None)
|
||||
with pytest.raises(TypeError, match="object of type 'NoneType' has no len"):
|
||||
MetadataService.create_metadata("dataset-123", mock_metadata_args)
|
||||
|
Reference in New Issue
Block a user