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(e2e): annotate three container service modules for strict pyrefly (#45333)

Felix Shi5e3edb5f344a
diff --git a/api/tests/e2e_tests/containers/pyrefly.toml b/api/tests/e2e_tests/containers/pyrefly.toml
index ae70d736794..c5762bbad15 100644
--- a/api/tests/e2e_tests/containers/pyrefly.toml
+++ b/api/tests/e2e_tests/containers/pyrefly.toml
@@ -40,7 +40,6 @@ project-excludes = [
"repositories/test_sqlalchemy_workflow_node_execution_repository.py",
"repositories/test_workflow_run_repository.py",
"services/auth/test_auth_integration.py",
- "services/dataset_collection_binding.py",
"services/dataset_service_update_delete.py",
"services/document_service_status.py",
"services/test_advanced_prompt_template_service.py",
@@ -50,14 +49,12 @@ project-excludes = [
"services/test_app_service.py",
"services/test_attachment_service.py",
"services/test_audio_service_db.py",
- "services/test_billing_service.py",
"services/test_conversation_service.py",
"services/test_conversation_variable_updater.py",
"services/test_dataset_permission_service.py",
"services/test_dataset_service.py",
"services/test_dataset_service_batch_update_document_status.py",
"services/test_dataset_service_create_dataset.py",
- "services/test_dataset_service_delete_dataset.py",
"services/test_dataset_service_document.py",
"services/test_dataset_service_get_segments.py",
"services/test_dataset_service_permissions.py",
diff --git a/api/tests/e2e_tests/containers/services/dataset_collection_binding.py b/api/tests/e2e_tests/containers/services/dataset_collection_binding.py
index 037282a7d4e..89e1fd0e6d3 100644
--- a/api/tests/e2e_tests/containers/services/dataset_collection_binding.py
+++ b/api/tests/e2e_tests/containers/services/dataset_collection_binding.py
@@ -66,7 +66,7 @@ class TestDatasetCollectionBindingServiceGetBinding:
including various provider/model combinations, collection types, and edge cases.
"""
- def test_get_dataset_collection_binding_existing_binding_success(self, db_session_with_containers: Session):
+ def test_get_dataset_collection_binding_existing_binding_success(self, db_session_with_containers: Session) -> None:
"""
Test successful retrieval of an existing collection binding.
@@ -95,7 +95,9 @@ class TestDatasetCollectionBindingServiceGetBinding:
assert result.id == existing_binding.id
assert result.collection_name == "existing-collection"
- def test_get_dataset_collection_binding_create_new_binding_success(self, db_session_with_containers: Session):
+ def test_get_dataset_collection_binding_create_new_binding_success(
+ self, db_session_with_containers: Session
+ ) -> None:
"""
Test successful creation of a new collection binding when none exists.
@@ -119,7 +121,9 @@ class TestDatasetCollectionBindingServiceGetBinding:
assert result.type == collection_type
assert result.collection_name is not None
- def test_get_dataset_collection_binding_different_collection_type(self, db_session_with_containers: Session):
+ def test_get_dataset_collection_binding_different_collection_type(
+ self, db_session_with_containers: Session
+ ) -> None:
"""Test get_dataset_collection_binding with different collection type."""
# Arrange
provider_name = "openai"
@@ -136,7 +140,7 @@ class TestDatasetCollectionBindingServiceGetBinding:
assert result.provider_name == provider_name
assert result.model_name == model_name
- def test_get_dataset_collection_binding_default_collection_type(self, db_session_with_containers: Session):
+ def test_get_dataset_collection_binding_default_collection_type(self, db_session_with_containers: Session) -> None:
"""Test get_dataset_collection_binding with default collection type parameter."""
# Arrange
provider_name = "openai"
@@ -154,7 +158,7 @@ class TestDatasetCollectionBindingServiceGetBinding:
def test_get_dataset_collection_binding_different_provider_model_combination(
self, db_session_with_containers: Session
- ):
+ ) -> None:
"""Test get_dataset_collection_binding with various provider/model combinations."""
# Arrange
combinations = [
@@ -181,7 +185,7 @@ class TestDatasetCollectionBindingServiceGetBindingByIdAndType:
including successful retrieval and error handling for missing bindings.
"""
- def test_get_dataset_collection_binding_by_id_and_type_success(self, db_session_with_containers: Session):
+ def test_get_dataset_collection_binding_by_id_and_type_success(self, db_session_with_containers: Session) -> None:
"""Test successful retrieval of collection binding by ID and type."""
# Arrange
binding = DatasetCollectionBindingTestDataFactory.create_collection_binding(
@@ -204,7 +208,9 @@ class TestDatasetCollectionBindingServiceGetBindingByIdAndType:
assert result.collection_name == "test-collection"
assert result.type == CollectionBindingType.DATASET
- def test_get_dataset_collection_binding_by_id_and_type_not_found_error(self, db_session_with_containers: Session):
+ def test_get_dataset_collection_binding_by_id_and_type_not_found_error(
+ self, db_session_with_containers: Session
+ ) -> None:
"""Test error handling when collection binding is not found by ID and type."""
# Arrange
non_existent_id = str(uuid4())
@@ -217,7 +223,7 @@ class TestDatasetCollectionBindingServiceGetBindingByIdAndType:
def test_get_dataset_collection_binding_by_id_and_type_different_collection_type(
self, db_session_with_containers: Session
- ):
+ ) -> None:
"""Test retrieval by ID and type with different collection type."""
# Arrange
binding = DatasetCollectionBindingTestDataFactory.create_collection_binding(
@@ -239,7 +245,7 @@ class TestDatasetCollectionBindingServiceGetBindingByIdAndType:
def test_get_dataset_collection_binding_by_id_and_type_default_collection_type(
self, db_session_with_containers: Session
- ):
+ ) -> None:
"""Test retrieval by ID with default collection type."""
# Arrange
binding = DatasetCollectionBindingTestDataFactory.create_collection_binding(
@@ -259,7 +265,9 @@ class TestDatasetCollectionBindingServiceGetBindingByIdAndType:
assert result.id == binding.id
assert result.type == CollectionBindingType.DATASET
- def test_get_dataset_collection_binding_by_id_and_type_wrong_type_error(self, db_session_with_containers: Session):
+ def test_get_dataset_collection_binding_by_id_and_type_wrong_type_error(
+ self, db_session_with_containers: Session
+ ) -> None:
"""Test error when binding exists but with wrong collection type."""
# Arrange
binding = DatasetCollectionBindingTestDataFactory.create_collection_binding(
diff --git a/api/tests/e2e_tests/containers/services/test_billing_service.py b/api/tests/e2e_tests/containers/services/test_billing_service.py
index 80462f79d73..c10f1f3d636 100644
--- a/api/tests/e2e_tests/containers/services/test_billing_service.py
+++ b/api/tests/e2e_tests/containers/services/test_billing_service.py
@@ -1,4 +1,5 @@
import json
+from collections.abc import Iterator
from unittest.mock import patch
import pytest
@@ -23,7 +24,7 @@ class TestBillingServiceGetPlanBulkWithCache:
"""
@pytest.fixture(autouse=True)
- def setup_redis_cleanup(self, flask_app_with_containers: Flask):
+ def setup_redis_cleanup(self, flask_app_with_containers: Flask) -> Iterator[None]:
"""Clean up Redis cache before and after each test."""
with flask_app_with_containers.app_context():
# Clean up before test
@@ -40,12 +41,12 @@ class TestBillingServiceGetPlanBulkWithCache:
) -> billing.SubscriptionPlanItem:
return builders.plan_item(plan, expiration_date)
- def _set_cache(self, tenant_id: str, plan_data: billing.SubscriptionPlanItem, ttl: int = 600):
+ def _set_cache(self, tenant_id: str, plan_data: billing.SubscriptionPlanItem, ttl: int = 600) -> None:
"""Helper to set cache data in Redis."""
cache_key = BillingService._make_plan_cache_key(tenant_id)
redis_client.setex(cache_key, ttl, plan_data.model_dump_json())
- def _get_cache(self, tenant_id: str):
+ def _get_cache(self, tenant_id: str) -> str | None:
"""Helper to get cache data from Redis."""
cache_key = BillingService._make_plan_cache_key(tenant_id)
value = redis_client.get(cache_key)
@@ -55,7 +56,7 @@ class TestBillingServiceGetPlanBulkWithCache:
return value
return None
- def test_get_plan_bulk_with_cache_all_cache_hit(self, flask_app_with_containers: Flask):
+ def test_get_plan_bulk_with_cache_all_cache_hit(self, flask_app_with_containers: Flask) -> None:
"""Test bulk plan retrieval when all tenants are in cache."""
with flask_app_with_containers.app_context():
# Arrange
@@ -86,7 +87,7 @@ class TestBillingServiceGetPlanBulkWithCache:
# Verify API was not called
mock_get_plan_bulk.assert_not_called()
- def test_get_plan_bulk_with_cache_all_cache_miss(self, flask_app_with_containers: Flask):
+ def test_get_plan_bulk_with_cache_all_cache_miss(self, flask_app_with_containers: Flask) -> None:
"""Test bulk plan retrieval when all tenants are not in cache."""
with flask_app_with_containers.app_context():
# Arrange
@@ -126,7 +127,7 @@ class TestBillingServiceGetPlanBulkWithCache:
assert ttl_1 > 0
assert ttl_1 <= 600 # Should be <= 600 seconds
- def test_get_plan_bulk_with_cache_partial_cache_hit(self, flask_app_with_containers: Flask):
+ def test_get_plan_bulk_with_cache_partial_cache_hit(self, flask_app_with_containers: Flask) -> None:
"""Test bulk plan retrieval when some tenants are in cache, some are not."""
with flask_app_with_containers.app_context():
# Arrange
@@ -157,7 +158,7 @@ class TestBillingServiceGetPlanBulkWithCache:
cached_data_3 = billing.SubscriptionPlanItem.model_validate_json(cached_3)
assert cached_data_3 == missing_plan["tenant-3"]
- def test_get_plan_bulk_with_cache_redis_mget_failure(self, flask_app_with_containers: Flask):
+ def test_get_plan_bulk_with_cache_redis_mget_failure(self, flask_app_with_containers: Flask) -> None:
"""Test fallback to API when Redis mget fails."""
with flask_app_with_containers.app_context():
# Arrange
@@ -188,7 +189,7 @@ class TestBillingServiceGetPlanBulkWithCache:
assert cached_1 is not None
assert cached_2 is not None
- def test_get_plan_bulk_with_cache_invalid_json_in_cache(self, flask_app_with_containers: Flask):
+ def test_get_plan_bulk_with_cache_invalid_json_in_cache(self, flask_app_with_containers: Flask) -> None:
"""Test fallback to API when cache contains invalid JSON."""
with flask_app_with_containers.app_context():
# Arrange
@@ -240,7 +241,7 @@ class TestBillingServiceGetPlanBulkWithCache:
cached_data_3 = billing.SubscriptionPlanItem.model_validate_json(cached_3)
assert cached_data_3 == expected_plans["tenant-3"]
- def test_get_plan_bulk_with_cache_invalid_plan_data_in_cache(self, flask_app_with_containers: Flask):
+ def test_get_plan_bulk_with_cache_invalid_plan_data_in_cache(self, flask_app_with_containers: Flask) -> None:
"""Test fallback to API when cache data doesn't match the plan model."""
with flask_app_with_containers.app_context():
# Arrange
@@ -273,7 +274,7 @@ class TestBillingServiceGetPlanBulkWithCache:
# Verify API was called for tenant-2 and tenant-3
mock_get_plan_bulk.assert_called_once_with(["tenant-2", "tenant-3"])
- def test_get_plan_bulk_with_cache_redis_pipeline_failure(self, flask_app_with_containers: Flask):
+ def test_get_plan_bulk_with_cache_redis_pipeline_failure(self, flask_app_with_containers: Flask) -> None:
"""Test that pipeline failure doesn't affect return value."""
with flask_app_with_containers.app_context():
# Arrange
@@ -302,7 +303,7 @@ class TestBillingServiceGetPlanBulkWithCache:
# Verify pipeline was attempted
mock_pipeline.assert_called_once()
- def test_get_plan_bulk_with_cache_empty_tenant_ids(self, flask_app_with_containers: Flask):
+ def test_get_plan_bulk_with_cache_empty_tenant_ids(self, flask_app_with_containers: Flask) -> None:
"""Test with empty tenant_ids list."""
with flask_app_with_containers.app_context():
# Act
@@ -320,7 +321,7 @@ class TestBillingServiceGetPlanBulkWithCache:
# But we should check that mget was not called at all
# Since we can't easily verify this without more mocking, we just verify the result
- def test_get_plan_bulk_with_cache_ttl_expired(self, flask_app_with_containers: Flask):
+ def test_get_plan_bulk_with_cache_ttl_expired(self, flask_app_with_containers: Flask) -> None:
"""Test that expired cache keys are treated as cache misses."""
with flask_app_with_containers.app_context():
# Arrange
diff --git a/api/tests/e2e_tests/containers/services/test_dataset_service_delete_dataset.py b/api/tests/e2e_tests/containers/services/test_dataset_service_delete_dataset.py
index 4ec0329757b..87d1c3b00fe 100644
--- a/api/tests/e2e_tests/containers/services/test_dataset_service_delete_dataset.py
+++ b/api/tests/e2e_tests/containers/services/test_dataset_service_delete_dataset.py
@@ -18,7 +18,7 @@ class DatasetDeleteIntegrationDataFactory:
"""Create persisted entities used by delete_dataset integration tests."""
@staticmethod
- def create_account_with_tenant(db_session_with_containers) -> tuple[Account, Tenant]:
+ def create_account_with_tenant(db_session_with_containers: Session) -> tuple[Account, Tenant]:
"""Persist an owner account, tenant, and tenant join for dataset deletion tests."""
account = Account(
email=f"owner-{uuid4()}@example.com",
@@ -50,7 +50,7 @@ class DatasetDeleteIntegrationDataFactory:
@staticmethod
def create_dataset(
- db_session_with_containers,
+ db_session_with_containers: Session,
tenant_id: str,
created_by: str,
*,
@@ -78,7 +78,7 @@ class DatasetDeleteIntegrationDataFactory:
@staticmethod
def create_document(
- db_session_with_containers,
+ db_session_with_containers: Session,
*,
tenant_id: str,
dataset_id: str,
@@ -105,7 +105,7 @@ class DatasetDeleteIntegrationDataFactory:
class TestDatasetServiceDeleteDataset:
"""Integration coverage for DatasetService.delete_dataset using testcontainers."""
- def test_delete_dataset_with_documents_success(self, db_session_with_containers: Session):
+ def test_delete_dataset_with_documents_success(self, db_session_with_containers: Session) -> None:
"""Delete a dataset with documents and dispatch cleanup through the real signal handler."""
# Arrange
owner, tenant = DatasetDeleteIntegrationDataFactory.create_account_with_tenant(db_session_with_containers)
@@ -148,7 +148,7 @@ class TestDatasetServiceDeleteDataset:
dataset.pipeline_id,
)
- def test_delete_empty_dataset_success(self, db_session_with_containers: Session):
+ def test_delete_empty_dataset_success(self, db_session_with_containers: Session) -> None:
"""Delete an empty dataset without scheduling cleanup when both gating fields are absent."""
# Arrange
owner, tenant = DatasetDeleteIntegrationDataFactory.create_account_with_tenant(db_session_with_containers)
@@ -176,7 +176,7 @@ class TestDatasetServiceDeleteDataset:
assert db_session_with_containers.get(Dataset, dataset.id) is None
clean_dataset_delay.assert_not_called()
- def test_delete_dataset_with_partial_none_values(self, db_session_with_containers: Session):
+ def test_delete_dataset_with_partial_none_values(self, db_session_with_containers: Session) -> None:
"""Delete a dataset without cleanup when indexing_technique is missing but doc_form resolves."""
# Arrange
owner, tenant = DatasetDeleteIntegrationDataFactory.create_account_with_tenant(db_session_with_containers)
@@ -204,7 +204,9 @@ class TestDatasetServiceDeleteDataset:
assert db_session_with_containers.get(Dataset, dataset.id) is None
clean_dataset_delay.assert_not_called()
- def test_delete_dataset_with_doc_form_none_indexing_technique_exists(self, db_session_with_containers: Session):
+ def test_delete_dataset_with_doc_form_none_indexing_technique_exists(
+ self, db_session_with_containers: Session
+ ) -> None:
"""Delete a dataset without cleanup when indexing exists but doc_form resolves to None."""
# Arrange
owner, tenant = DatasetDeleteIntegrationDataFactory.create_account_with_tenant(db_session_with_containers)
@@ -232,7 +234,7 @@ class TestDatasetServiceDeleteDataset:
assert db_session_with_containers.get(Dataset, dataset.id) is None
clean_dataset_delay.assert_not_called()
- def test_delete_dataset_not_found(self, db_session_with_containers: Session):
+ def test_delete_dataset_not_found(self, db_session_with_containers: Session) -> None:
"""Return False without scheduling cleanup when the target dataset does not exist."""
# Arrange
owner, _ = DatasetDeleteIntegrationDataFactory.create_account_with_tenant(db_session_with_containers)