Skip to content

Commit

Permalink
fix bug in test
Browse files Browse the repository at this point in the history
  • Loading branch information
hellofinch committed Jan 9, 2025
1 parent 3736081 commit 696845e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pdf2zh/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,23 @@ def delete(cls, key):
del instance._config_data[key]
instance._save_config()

@classmethod
def clear(cls):
"""删除配置值并保存"""
instance = cls.get_instance()
with instance._lock:
instance._config_data={}
instance._save_config()

@classmethod
def all(cls):
"""返回所有配置项"""
instance = cls.get_instance()
# 这里只做读取操作,一般可不加锁。不过为了保险也可以加锁。
return instance._config_data

@classmethod
def remove(cls):
instance = cls.get_instance()
with instance._lock:
os.remove(instance._config_path)
6 changes: 6 additions & 0 deletions test/test_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pdf2zh.translator import BaseTranslator
from pdf2zh.translator import OpenAIlikedTranslator
from pdf2zh import cache
from pdf2zh.config import ConfigManager


class AutoIncreaseTranslator(BaseTranslator):
Expand Down Expand Up @@ -81,9 +82,11 @@ def setUp(self) -> None:
"OPENAILIKED_API_KEY": "test_api_key",
"OPENAILIKED_MODEL": "test_model",
}
ConfigManager.custome_config("config.json")

def test_missing_base_url_raises_error(self):
"""测试缺失 OPENAILIKED_BASE_URL 时抛出异常"""
ConfigManager.clear()
with self.assertRaises(ValueError) as context:
OpenAIlikedTranslator(
lang_in="en", lang_out="zh", model="test_model", envs={}
Expand All @@ -96,6 +99,7 @@ def test_missing_model_raises_error(self):
"OPENAILIKED_BASE_URL": "https://api.openailiked.com",
"OPENAILIKED_API_KEY": "test_api_key",
}
ConfigManager.clear()
with self.assertRaises(ValueError) as context:
OpenAIlikedTranslator(
lang_in="en", lang_out="zh", model=None, envs=envs_without_model
Expand All @@ -104,6 +108,7 @@ def test_missing_model_raises_error(self):

def test_initialization_with_valid_envs(self):
"""测试使用有效的环境变量初始化"""
ConfigManager.clear()
translator = OpenAIlikedTranslator(
lang_in="en",
lang_out="zh",
Expand All @@ -126,6 +131,7 @@ def test_default_api_key_fallback(self):
"OPENAILIKED_BASE_URL": "https://api.openailiked.com",
"OPENAILIKED_MODEL": "test_model",
}
ConfigManager.clear()
translator = OpenAIlikedTranslator(
lang_in="en",
lang_out="zh",
Expand Down

0 comments on commit 696845e

Please sign in to comment.