gapfill G4: cover caps.py:54-55 env-get-exception fallback and quota.py:73-74 illegal-env fallback (A9 subset gaps); line numbers verified unchanged after rebase
This commit is contained in:
1 parent
aa6179b5e1
commit
b4270dc117
1 file changed
+55
-4
@@ -17,18 +17,30 @@
|
|||||||
只加测试,不改生产代码。
|
只加测试,不改生产代码。
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
import unittest
|
import unittest
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from cloud.caps import running_count
|
from cloud.caps import DEFAULT_CAPACITY, resolve_capacity, running_count
|
||||||
from cloud.quota import monthly_usage, utc_month
|
from cloud.quota import (
|
||||||
|
DEFAULT_QUOTA_USD_MICROS,
|
||||||
|
default_quota_usd_micros,
|
||||||
|
monthly_usage,
|
||||||
|
utc_month,
|
||||||
|
)
|
||||||
except ImportError: # discover -s cloud/tests 时把项目根目录补进 sys.path
|
except ImportError: # discover -s cloud/tests 时把项目根目录补进 sys.path
|
||||||
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
|
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
|
||||||
from cloud.caps import running_count
|
from cloud.caps import DEFAULT_CAPACITY, resolve_capacity, running_count
|
||||||
from cloud.quota import monthly_usage, utc_month
|
from cloud.quota import (
|
||||||
|
DEFAULT_QUOTA_USD_MICROS,
|
||||||
|
default_quota_usd_micros,
|
||||||
|
monthly_usage,
|
||||||
|
utc_month,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class CapsRunningCountFallbackTestCase(unittest.TestCase):
|
class CapsRunningCountFallbackTestCase(unittest.TestCase):
|
||||||
@@ -46,6 +58,45 @@ class CapsRunningCountFallbackTestCase(unittest.TestCase):
|
|||||||
self.assertEqual(running_count({"a": {}, "b": {}}), 2)
|
self.assertEqual(running_count({"a": {}, "b": {}}), 2)
|
||||||
|
|
||||||
|
|
||||||
|
class _RaisingEnv:
|
||||||
|
"""get 一律抛异常的环境替身:模拟 env 映射本身坏掉时
|
||||||
|
resolve_capacity 的 except 分支(caps.py:54-55)。"""
|
||||||
|
|
||||||
|
def get(self, key):
|
||||||
|
raise RuntimeError("env broken")
|
||||||
|
|
||||||
|
|
||||||
|
class CapsResolveCapacityEnvErrorTestCase(unittest.TestCase):
|
||||||
|
def test_raising_env_falls_back_to_default(self):
|
||||||
|
# caps.py:54-55:source.get 抛异常 -> raw=None -> 默认上限,
|
||||||
|
# 异常不得漏到调用方。
|
||||||
|
self.assertEqual(resolve_capacity(None, env=_RaisingEnv()),
|
||||||
|
DEFAULT_CAPACITY)
|
||||||
|
self.assertEqual(DEFAULT_CAPACITY, 1)
|
||||||
|
|
||||||
|
def test_normal_env_still_parses(self):
|
||||||
|
# 回落分支不得改变正常解析语义。
|
||||||
|
self.assertEqual(
|
||||||
|
resolve_capacity(None, env={"CLOUD_WORKER_CAPACITY": "4"}), 4)
|
||||||
|
|
||||||
|
|
||||||
|
class QuotaDefaultIllegalEnvTestCase(unittest.TestCase):
|
||||||
|
def test_illegal_env_value_falls_back_to_default(self):
|
||||||
|
# quota.py:73-74:int(非法串) 抛 ValueError -> 回落 $10
|
||||||
|
#(DEFAULT_QUOTA_USD_MICROS 微美元),不得把异常漏出。
|
||||||
|
with mock.patch.dict(os.environ,
|
||||||
|
{"CLOUD_DEFAULT_QUOTA_USD_MICROS": "not-a-number"}):
|
||||||
|
self.assertEqual(default_quota_usd_micros(),
|
||||||
|
DEFAULT_QUOTA_USD_MICROS)
|
||||||
|
self.assertEqual(DEFAULT_QUOTA_USD_MICROS, 10000000)
|
||||||
|
|
||||||
|
def test_valid_env_value_still_parses(self):
|
||||||
|
# 回落分支不得改变正常解析语义。
|
||||||
|
with mock.patch.dict(os.environ,
|
||||||
|
{"CLOUD_DEFAULT_QUOTA_USD_MICROS": "250"}):
|
||||||
|
self.assertEqual(default_quota_usd_micros(), 250)
|
||||||
|
|
||||||
|
|
||||||
class QuotaUtcMonthTestCase(unittest.TestCase):
|
class QuotaUtcMonthTestCase(unittest.TestCase):
|
||||||
def test_utc_month_direct_call(self):
|
def test_utc_month_direct_call(self):
|
||||||
# quota.py:79-80:显式 now 直接格式化,不走 now=None 分支。
|
# quota.py:79-80:显式 now 直接格式化,不走 now=None 分支。
|
||||||
|
|||||||
Reference in new issue
Block a user