Skip to content

Commit bb1fa43

Browse files
authored
✨ allow call api raise exception
1 parent 3abc22c commit bb1fa43

4 files changed

Lines changed: 54 additions & 6 deletions

File tree

nonebug/mixin/call_api/__init__.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,23 +132,32 @@ def should_call_api(
132132
self,
133133
api: str,
134134
data: Dict[str, Any],
135-
result: Any,
135+
result: Optional[Any] = None,
136+
exception: Optional[Exception] = None,
136137
adapter: Optional["Adapter"] = None,
137138
) -> Api:
138-
model = Api(name=api, data=data, result=result, adapter=adapter)
139+
model = Api(
140+
name=api, data=data, result=result, exception=exception, adapter=adapter
141+
)
139142
self.wait_list.put(model)
140143
return model
141144

142145
def should_call_send(
143146
self,
144147
event: "Event",
145148
message: Union[str, "Message", "MessageSegment"],
146-
result: Any,
149+
result: Optional[Any] = None,
150+
exception: Optional[Exception] = None,
147151
bot: Optional["Bot"] = None,
148152
**kwargs: Any,
149153
) -> Send:
150154
model = Send(
151-
event=event, message=message, kwargs=kwargs, result=result, bot=bot
155+
event=event,
156+
message=message,
157+
kwargs=kwargs,
158+
result=result,
159+
exception=exception,
160+
bot=bot,
152161
)
153162
self.wait_list.put(model)
154163
return model
@@ -171,6 +180,9 @@ def got_call_api(self, adapter: "Adapter", api: str, **data: Any) -> Any:
171180
pytest.fail(
172181
f"Application got api call {api} with adapter {adapter} but expected {model.adapter}"
173182
)
183+
184+
if model.exception is not None:
185+
raise model.exception
174186
return model.result
175187

176188
def got_call_send(
@@ -203,6 +215,9 @@ def got_call_send(
203215
pytest.fail(
204216
f"Application got send call with bot {bot} but expected {model.bot}"
205217
)
218+
219+
if model.exception is not None:
220+
raise model.exception
206221
return model.result
207222

208223
@contextlib.contextmanager

nonebug/mixin/call_api/model.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Api(Model):
1515
name: str
1616
data: Dict[str, Any]
1717
result: Any
18+
exception: Optional[Exception]
1819
adapter: Optional["Adapter"]
1920

2021

@@ -24,4 +25,5 @@ class Send(Model):
2425
message: Union[str, "Message", "MessageSegment"]
2526
kwargs: Dict[str, Any]
2627
result: Any
28+
exception: Optional[Exception]
2729
bot: Optional["Bot"]

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import nonebot
66
from nonebot.plugin import Plugin
77

8-
from nonebug.fixture import *
98
from nonebug import NONEBOT_INIT_KWARGS
9+
from nonebug.fixture import nonebug_app, nonebug_init
1010

1111

1212
def pytest_configure(config: pytest.Config) -> None:

tests/test_call_api.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class FakeBot(Bot):
8080

8181

8282
@pytest.mark.asyncio
83-
async def test_got_call(app: App):
83+
async def test_got_call_api(app: App):
8484
async with app.test_api() as ctx:
8585
adapter = ctx.create_adapter()
8686
bot = ctx.create_bot(self_id="test", adapter=adapter)
@@ -92,6 +92,23 @@ async def test_got_call(app: App):
9292

9393
assert "test" not in get_bots()
9494

95+
async with app.test_api() as ctx:
96+
adapter = ctx.create_adapter()
97+
bot = ctx.create_bot(self_id="test", adapter=adapter)
98+
assert "test" in get_bots()
99+
api = ctx.should_call_api(
100+
"test", {"key": "value"}, exception=RuntimeError(), adapter=adapter
101+
)
102+
with pytest.raises(RuntimeError):
103+
result = await bot.call_api("test", key="value")
104+
105+
assert ctx.wait_list.empty()
106+
107+
assert "test" not in get_bots()
108+
109+
110+
@pytest.mark.asyncio
111+
async def test_got_call_send(app: App):
95112
async with app.test_api() as ctx:
96113
bot = ctx.create_bot(self_id="test")
97114
assert "test" in get_bots()
@@ -102,3 +119,17 @@ async def test_got_call(app: App):
102119
assert result == "result"
103120

104121
assert "test" not in get_bots()
122+
123+
async with app.test_api() as ctx:
124+
bot = ctx.create_bot(self_id="test")
125+
assert "test" in get_bots()
126+
event = make_fake_event()()
127+
api = ctx.should_call_send(
128+
event, "test", exception=RuntimeError(), bot=bot, key="value"
129+
)
130+
with pytest.raises(RuntimeError):
131+
result = await bot.send(event, "test", key="value")
132+
133+
assert ctx.wait_list.empty()
134+
135+
assert "test" not in get_bots()

0 commit comments

Comments
 (0)