""" backend/tests/test_env_settings_refresh_cache.py 用途:验证 app_settings 进程内 TTL 缓存,避免 before_request 每请求打库。 依赖:pytest、env_persistence、Flask app fixture。 逻辑:断言 TTL 内二次 apply 不读库;force / invalidate / TTL=0 强制重载;persist 后失效缓存。 """ from __future__ import annotations from app.core.config import env_persistence def test_apply_env_settings_reuses_cache_within_ttl(app, monkeypatch): """TTL 内第二次 apply 不应再调用 _load_from_db。""" env_persistence.invalidate_env_settings_cache() monkeypatch.setitem(app.config, 'APP_SETTINGS_REFRESH_SECONDS', 60) calls = {'db': 0, 'file': 0} def _fake_db(prefixes): calls['db'] += 1 return {'WECOM_CORP_ID': 'corp-cached'} def _fake_file(prefixes): calls['file'] += 1 return {} monkeypatch.setattr(env_persistence, '_load_from_db', _fake_db) monkeypatch.setattr(env_persistence, '_load_from_env_file', _fake_file) with app.app_context(): first = env_persistence.apply_env_settings(force=True) second = env_persistence.apply_env_settings() assert first['WECOM_CORP_ID'] == 'corp-cached' assert second['WECOM_CORP_ID'] == 'corp-cached' assert calls['db'] == 1 assert calls['file'] == 1 def test_apply_env_settings_force_bypasses_ttl(app, monkeypatch): """force=True 即使在 TTL 内也重新读库。""" env_persistence.invalidate_env_settings_cache() monkeypatch.setitem(app.config, 'APP_SETTINGS_REFRESH_SECONDS', 60) calls = {'db': 0} def _fake_db(prefixes): calls['db'] += 1 return {'WECOM_CORP_ID': f'corp-{calls["db"]}'} monkeypatch.setattr(env_persistence, '_load_from_db', _fake_db) monkeypatch.setattr(env_persistence, '_load_from_env_file', lambda prefixes: {}) with app.app_context(): env_persistence.apply_env_settings(force=True) env_persistence.apply_env_settings(force=True) assert calls['db'] == 2 def test_persist_env_settings_invalidates_cache(app, monkeypatch): """persist 后缓存失效,下一次 apply 会重新读库。""" env_persistence.invalidate_env_settings_cache() monkeypatch.setitem(app.config, 'APP_SETTINGS_REFRESH_SECONDS', 60) calls = {'db': 0} def _fake_db(prefixes): calls['db'] += 1 return {'WECOM_CORP_ID': f'corp-{calls["db"]}'} monkeypatch.setattr(env_persistence, '_load_from_db', _fake_db) monkeypatch.setattr(env_persistence, '_load_from_env_file', lambda prefixes: {}) monkeypatch.setattr(env_persistence, '_persist_to_db', lambda values: True) monkeypatch.setattr(env_persistence, '_persist_to_env_file', lambda values: True) with app.app_context(): env_persistence.apply_env_settings(force=True) assert calls['db'] == 1 env_persistence.persist_env_settings({'WECOM_CORP_ID': 'corp-new'}) env_persistence.apply_env_settings() assert calls['db'] == 2 def test_ttl_zero_reloads_every_time(app, monkeypatch): """APP_SETTINGS_REFRESH_SECONDS=0 时每次强制读库。""" env_persistence.invalidate_env_settings_cache() monkeypatch.setitem(app.config, 'APP_SETTINGS_REFRESH_SECONDS', 0) calls = {'db': 0} def _fake_db(prefixes): calls['db'] += 1 return {'WECOM_CORP_ID': 'x'} monkeypatch.setattr(env_persistence, '_load_from_db', _fake_db) monkeypatch.setattr(env_persistence, '_load_from_env_file', lambda prefixes: {}) with app.app_context(): env_persistence.apply_env_settings() env_persistence.apply_env_settings() assert calls['db'] == 2