mirror of
https://github.com/EstrellaXD/Auto_Bangumi.git
synced 2026-04-13 18:11:03 +08:00
fix: integrate mock for OpenAIParser for testing
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import json
|
||||
import os
|
||||
from unittest import mock
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from module.parser.openai import OpenAIParser
|
||||
@@ -10,13 +11,14 @@ load_dotenv()
|
||||
class TestOpenAIParser:
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
api_key = os.getenv("OPENAI_API_KEY")
|
||||
cls.parser = OpenAIParser(api_key=api_key)
|
||||
api_key = os.getenv("OPENAI_API_KEY") or "testing!"
|
||||
cls.mocker = mock.Mock()
|
||||
with mock.patch.object(OpenAIParser, "parse", new_callable=cls.mocker):
|
||||
cls.parser = OpenAIParser(api_key=api_key)
|
||||
|
||||
def test_parse(self):
|
||||
text = "[梦蓝字幕组]New Doraemon 哆啦A梦新番[747][2023.02.25][AVC][1080P][GB_JP][MP4]"
|
||||
result = self.parser.parse(text=text, asdict=False)
|
||||
assert json.loads(result) == {
|
||||
expected = {
|
||||
"group": "梦蓝字幕组",
|
||||
"title_en": "New Doraemon",
|
||||
"resolution": "1080P",
|
||||
@@ -28,3 +30,7 @@ class TestOpenAIParser:
|
||||
"season_raw": "2023.02.25",
|
||||
"source": "AVC",
|
||||
}
|
||||
self.mocker.return_value = expected
|
||||
|
||||
result = self.parser.parse(text=text, asdict=False)
|
||||
assert json.loads(result) == expected
|
||||
|
||||
Reference in New Issue
Block a user