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(api): enable strict typing for quota type tests (#43792)

Eddy ZHANG520539fbc89e
diff --git a/api/tests/unit_tests/enums/test_quota_type.py b/api/tests/unit_tests/enums/test_quota_type.py
index d5574ba55de..77e3dc6ced4 100644
--- a/api/tests/unit_tests/enums/test_quota_type.py
+++ b/api/tests/unit_tests/enums/test_quota_type.py
@@ -1,5 +1,6 @@
"""Unit tests for QuotaType, QuotaService, and QuotaCharge."""
+from collections.abc import Callable
from unittest.mock import patch
import pytest
@@ -9,34 +10,34 @@ from services.quota_service import QuotaCharge, QuotaService, unlimited
class TestQuotaType:
- def test_billing_key_trigger(self):
+ def test_billing_key_trigger(self) -> None:
assert QuotaType.TRIGGER.billing_key == "trigger_event"
- def test_billing_key_workflow(self):
+ def test_billing_key_workflow(self) -> None:
assert QuotaType.WORKFLOW.billing_key == "api_rate_limit"
- def test_billing_key_unlimited_raises(self):
+ def test_billing_key_unlimited_raises(self) -> None:
with pytest.raises(ValueError, match="Invalid quota type"):
_ = QuotaType.UNLIMITED.billing_key
class TestQuotaService:
@pytest.fixture(autouse=True)
- def _cloud_edition(self, config_overrides):
+ def _cloud_edition(self, config_overrides: Callable[..., None]) -> None:
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.CLOUD)
- def test_reserve_outside_cloud_edition(self, config_overrides):
+ def test_reserve_outside_cloud_edition(self, config_overrides: Callable[..., None]) -> None:
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY)
with patch("services.billing_service.BillingService"):
charge = QuotaService.reserve(QuotaType.TRIGGER, "t1")
assert charge.success is True
assert charge.charge_id is None
- def test_reserve_zero_amount_raises(self):
+ def test_reserve_zero_amount_raises(self) -> None:
with pytest.raises(ValueError, match="greater than 0"):
QuotaService.reserve(QuotaType.TRIGGER, "t1", amount=0)
- def test_reserve_success(self):
+ def test_reserve_success(self) -> None:
with patch("services.billing_service.BillingService") as mock_bs:
mock_bs.quota_reserve.return_value = {"reservation_id": "rid-1", "available": 99}
@@ -49,16 +50,16 @@ class TestQuotaService:
assert charge._amount == 1
mock_bs.quota_reserve.assert_called_once()
- def test_reserve_no_reservation_id_raises(self):
+ def test_reserve_no_reservation_id_raises(self) -> None:
from services.errors.app import QuotaExceededError
with patch("services.billing_service.BillingService") as mock_bs:
- mock_bs.quota_reserve.return_value = {}
+ mock_bs.quota_reserve.return_value = dict[str, object]()
with pytest.raises(QuotaExceededError):
QuotaService.reserve(QuotaType.TRIGGER, "t1")
- def test_reserve_quota_exceeded_propagates(self):
+ def test_reserve_quota_exceeded_propagates(self) -> None:
from services.errors.app import QuotaExceededError
with patch("services.billing_service.BillingService") as mock_bs:
@@ -67,7 +68,7 @@ class TestQuotaService:
with pytest.raises(QuotaExceededError):
QuotaService.reserve(QuotaType.TRIGGER, "t1")
- def test_reserve_api_exception_returns_unlimited(self):
+ def test_reserve_api_exception_returns_unlimited(self) -> None:
with patch("services.billing_service.BillingService") as mock_bs:
mock_bs.quota_reserve.side_effect = RuntimeError("network")
@@ -75,109 +76,109 @@ class TestQuotaService:
assert charge.success is True
assert charge.charge_id is None
- def test_consume_calls_reserve_and_commit(self):
+ def test_consume_calls_reserve_and_commit(self) -> None:
with patch("services.billing_service.BillingService") as mock_bs:
mock_bs.quota_reserve.return_value = {"reservation_id": "rid-c"}
- mock_bs.quota_commit.return_value = {}
+ mock_bs.quota_commit.return_value = dict[str, object]()
charge = QuotaService.consume(QuotaType.TRIGGER, "t1")
assert charge.success is True
mock_bs.quota_commit.assert_called_once()
- def test_check_outside_cloud_edition(self, config_overrides):
+ def test_check_outside_cloud_edition(self, config_overrides: Callable[..., None]) -> None:
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY)
assert QuotaService.check(QuotaType.TRIGGER, "t1") is True
- def test_check_zero_amount_raises(self):
+ def test_check_zero_amount_raises(self) -> None:
with pytest.raises(ValueError, match="greater than 0"):
QuotaService.check(QuotaType.TRIGGER, "t1", amount=0)
- def test_check_sufficient_quota(self):
+ def test_check_sufficient_quota(self) -> None:
with patch.object(QuotaService, "get_remaining", return_value=100):
assert QuotaService.check(QuotaType.TRIGGER, "t1", amount=50) is True
- def test_check_insufficient_quota(self):
+ def test_check_insufficient_quota(self) -> None:
with patch.object(QuotaService, "get_remaining", return_value=5):
assert QuotaService.check(QuotaType.TRIGGER, "t1", amount=10) is False
- def test_check_unlimited_quota(self):
+ def test_check_unlimited_quota(self) -> None:
with patch.object(QuotaService, "get_remaining", return_value=-1):
assert QuotaService.check(QuotaType.TRIGGER, "t1", amount=999) is True
- def test_check_exception_returns_true(self):
+ def test_check_exception_returns_true(self) -> None:
with patch.object(QuotaService, "get_remaining", side_effect=RuntimeError):
assert QuotaService.check(QuotaType.TRIGGER, "t1") is True
- def test_release_outside_cloud_edition(self, config_overrides):
+ def test_release_outside_cloud_edition(self, config_overrides: Callable[..., None]) -> None:
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY)
with patch("services.billing_service.BillingService") as mock_bs:
QuotaService.release(QuotaType.TRIGGER, "rid-1", "t1", "trigger_event")
mock_bs.quota_release.assert_not_called()
- def test_release_empty_reservation(self):
+ def test_release_empty_reservation(self) -> None:
with patch("services.billing_service.BillingService") as mock_bs:
QuotaService.release(QuotaType.TRIGGER, "", "t1", "trigger_event")
mock_bs.quota_release.assert_not_called()
- def test_release_success(self):
+ def test_release_success(self) -> None:
with patch("services.billing_service.BillingService") as mock_bs:
- mock_bs.quota_release.return_value = {}
+ mock_bs.quota_release.return_value = dict[str, object]()
QuotaService.release(QuotaType.TRIGGER, "rid-1", "t1", "trigger_event")
mock_bs.quota_release.assert_called_once_with(
tenant_id="t1", feature_key="trigger_event", reservation_id="rid-1"
)
- def test_release_exception_swallowed(self):
+ def test_release_exception_swallowed(self) -> None:
with patch("services.billing_service.BillingService") as mock_bs:
mock_bs.quota_release.side_effect = RuntimeError("fail")
QuotaService.release(QuotaType.TRIGGER, "rid-1", "t1", "trigger_event")
- def test_get_remaining_normal(self):
+ def test_get_remaining_normal(self) -> None:
with patch("services.billing_service.BillingService") as mock_bs:
mock_bs.get_quota_info.return_value = {"trigger_event": {"limit": 100, "usage": 30}}
assert QuotaService.get_remaining(QuotaType.TRIGGER, "t1") == 70
- def test_get_remaining_unlimited(self):
+ def test_get_remaining_unlimited(self) -> None:
with patch("services.billing_service.BillingService") as mock_bs:
mock_bs.get_quota_info.return_value = {"trigger_event": {"limit": -1, "usage": 0}}
assert QuotaService.get_remaining(QuotaType.TRIGGER, "t1") == -1
- def test_get_remaining_over_limit_returns_zero(self):
+ def test_get_remaining_over_limit_returns_zero(self) -> None:
with patch("services.billing_service.BillingService") as mock_bs:
mock_bs.get_quota_info.return_value = {"trigger_event": {"limit": 10, "usage": 15}}
assert QuotaService.get_remaining(QuotaType.TRIGGER, "t1") == 0
- def test_get_remaining_exception_returns_neg1(self):
+ def test_get_remaining_exception_returns_neg1(self) -> None:
with patch("services.billing_service.BillingService") as mock_bs:
mock_bs.get_quota_info.side_effect = RuntimeError
assert QuotaService.get_remaining(QuotaType.TRIGGER, "t1") == -1
- def test_get_remaining_empty_response(self):
+ def test_get_remaining_empty_response(self) -> None:
with patch("services.billing_service.BillingService") as mock_bs:
- mock_bs.get_quota_info.return_value = {}
+ mock_bs.get_quota_info.return_value = dict[str, object]()
assert QuotaService.get_remaining(QuotaType.TRIGGER, "t1") == 0
- def test_get_remaining_non_dict_response(self):
+ def test_get_remaining_non_dict_response(self) -> None:
with patch("services.billing_service.BillingService") as mock_bs:
mock_bs.get_quota_info.return_value = "invalid"
assert QuotaService.get_remaining(QuotaType.TRIGGER, "t1") == 0
- def test_get_remaining_feature_not_in_response(self):
+ def test_get_remaining_feature_not_in_response(self) -> None:
with patch("services.billing_service.BillingService") as mock_bs:
mock_bs.get_quota_info.return_value = {"other_feature": {"limit": 100, "usage": 0}}
remaining = QuotaService.get_remaining(QuotaType.TRIGGER, "t1")
assert remaining == 0
- def test_get_remaining_non_dict_feature_info(self):
+ def test_get_remaining_non_dict_feature_info(self) -> None:
with patch("services.billing_service.BillingService") as mock_bs:
mock_bs.get_quota_info.return_value = {"trigger_event": "not_a_dict"}
assert QuotaService.get_remaining(QuotaType.TRIGGER, "t1") == 0
class TestQuotaCharge:
- def test_commit_success(self):
+ def test_commit_success(self) -> None:
with patch("services.billing_service.BillingService") as mock_bs:
- mock_bs.quota_commit.return_value = {}
+ mock_bs.quota_commit.return_value = dict[str, object]()
charge = QuotaCharge(
success=True,
charge_id="rid-1",
@@ -195,9 +196,9 @@ class TestQuotaCharge:
)
assert charge._committed is True
- def test_commit_with_actual_amount(self):
+ def test_commit_with_actual_amount(self) -> None:
with patch("services.billing_service.BillingService") as mock_bs:
- mock_bs.quota_commit.return_value = {}
+ mock_bs.quota_commit.return_value = dict[str, object]()
charge = QuotaCharge(
success=True,
charge_id="rid-1",
@@ -210,9 +211,9 @@ class TestQuotaCharge:
call_kwargs = mock_bs.quota_commit.call_args[1]
assert call_kwargs["actual_amount"] == 5
- def test_commit_idempotent(self):
+ def test_commit_idempotent(self) -> None:
with patch("services.billing_service.BillingService") as mock_bs:
- mock_bs.quota_commit.return_value = {}
+ mock_bs.quota_commit.return_value = dict[str, object]()
charge = QuotaCharge(
success=True,
charge_id="rid-1",
@@ -225,13 +226,13 @@ class TestQuotaCharge:
charge.commit()
assert mock_bs.quota_commit.call_count == 1
- def test_commit_no_charge_id_noop(self):
+ def test_commit_no_charge_id_noop(self) -> None:
with patch("services.billing_service.BillingService") as mock_bs:
charge = QuotaCharge(success=True, charge_id=None, _quota_type=QuotaType.TRIGGER)
charge.commit()
mock_bs.quota_commit.assert_not_called()
- def test_commit_no_tenant_id_noop(self):
+ def test_commit_no_tenant_id_noop(self) -> None:
with patch("services.billing_service.BillingService") as mock_bs:
charge = QuotaCharge(
success=True,
@@ -243,7 +244,7 @@ class TestQuotaCharge:
charge.commit()
mock_bs.quota_commit.assert_not_called()
- def test_commit_exception_swallowed(self):
+ def test_commit_exception_swallowed(self) -> None:
with patch("services.billing_service.BillingService") as mock_bs:
mock_bs.quota_commit.side_effect = RuntimeError("fail")
charge = QuotaCharge(
@@ -256,7 +257,7 @@ class TestQuotaCharge:
)
charge.commit()
- def test_refund_success(self):
+ def test_refund_success(self) -> None:
with patch.object(QuotaService, "release") as mock_rel:
charge = QuotaCharge(
success=True,
@@ -268,13 +269,13 @@ class TestQuotaCharge:
charge.refund()
mock_rel.assert_called_once_with(QuotaType.TRIGGER, "rid-1", "t1", "trigger_event")
- def test_refund_no_charge_id_noop(self):
+ def test_refund_no_charge_id_noop(self) -> None:
with patch.object(QuotaService, "release") as mock_rel:
charge = QuotaCharge(success=True, charge_id=None, _quota_type=QuotaType.TRIGGER)
charge.refund()
mock_rel.assert_not_called()
- def test_refund_no_tenant_id_noop(self):
+ def test_refund_no_tenant_id_noop(self) -> None:
with patch.object(QuotaService, "release") as mock_rel:
charge = QuotaCharge(
success=True,
@@ -287,7 +288,7 @@ class TestQuotaCharge:
class TestUnlimited:
- def test_unlimited_returns_success_with_no_charge_id(self):
+ def test_unlimited_returns_success_with_no_charge_id(self) -> None:
charge = unlimited()
assert charge.success is True
assert charge.charge_id is None
diff --git a/api/tests/unit_tests/pyrefly.toml b/api/tests/unit_tests/pyrefly.toml
index 3fac6d9030e..6d8f5cc06bb 100644
--- a/api/tests/unit_tests/pyrefly.toml
+++ b/api/tests/unit_tests/pyrefly.toml
@@ -600,7 +600,6 @@ project-excludes = [
"enterprise/telemetry/test_event_handlers.py",
"enterprise/telemetry/test_exporter.py",
"enterprise/telemetry/test_gateway.py",
- "enums/test_quota_type.py",
"events/event_handlers/test_clean_when_document_deleted.py",
"events/event_handlers/test_delete_tool_parameters_cache_when_sync_draft_workflow.py",
"events/test_app_event_signals.py",