Add test suite for input.py with prompt_toolkit mocking - #20
Conversation
Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
| import pathlib | ||
| import pytest | ||
| import pytest_asyncio | ||
| from unittest.mock import AsyncMock, patch, MagicMock |
There was a problem hiding this comment.
patch放在最前面或者最后面吧。另外,这个unittest是python官方的么?如果是的话,在pytest前面import吧
There was a problem hiding this comment.
Reorganized imports to place unittest.mock (stdlib) before third-party imports like pytest. Commit: b93b7a5
| # Mock PromptSession to simulate user input | ||
| mock_prompt_session = MagicMock() | ||
| # First input will cause parse error, second exits | ||
| mock_prompt_session.prompt_async = AsyncMock(side_effect=["invalid((data", EOFError()]) |
There was a problem hiding this comment.
Changed invalid input test case to use "=>" which is cleaner and also invalid. Commit: b93b7a5
Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
hzhangxyz
left a comment
There was a problem hiding this comment.
import之间不要有空行
另外mock的那三个,函数patch放在两个类前面
Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
Removed blank lines between imports and reordered mock imports (patch before AsyncMock and MagicMock). Commit: 96d38c0 |
There was a problem hiding this comment.
Pull request overview
This PR adds a comprehensive test suite for input.py, addressing the challenge of testing code that uses prompt_toolkit for user input. The test suite uses unittest.mock to simulate user interactions, enabling automated testing of the input handling logic.
Key changes:
- Added 7 test cases covering all major scenarios including valid input, error handling, cancellation, and multiple entries
- Implemented prompt_toolkit mocking strategy using
patchandAsyncMockto simulate user input - Created tests that verify both Facts and Ideas are correctly stored in the database based on parsed input
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| task = asyncio.create_task(main(addr, engine, session)) | ||
| try: | ||
| await task | ||
| except asyncio.CancelledError: |
There was a problem hiding this comment.
'except' clause does nothing but pass and there is no explanatory comment.
| task = asyncio.create_task(main(addr, engine, session)) | ||
| try: | ||
| await task | ||
| except asyncio.CancelledError: |
There was a problem hiding this comment.
'except' clause does nothing but pass and there is no explanatory comment.
| task = asyncio.create_task(main(addr, engine, session)) | ||
| try: | ||
| await task | ||
| except asyncio.CancelledError: |
There was a problem hiding this comment.
'except' clause does nothing but pass and there is no explanatory comment.
| task = asyncio.create_task(main(addr, engine, session)) | ||
| try: | ||
| await task | ||
| except asyncio.CancelledError: |
There was a problem hiding this comment.
'except' clause does nothing but pass and there is no explanatory comment.
| except asyncio.CancelledError: | |
| except asyncio.CancelledError: | |
| # Expected - main cancels its task when input loop terminates |
| task = asyncio.create_task(main(addr, engine, session)) | ||
| try: | ||
| await task | ||
| except asyncio.CancelledError: |
There was a problem hiding this comment.
'except' clause does nothing but pass and there is no explanatory comment.
| except asyncio.CancelledError: | |
| except asyncio.CancelledError: | |
| # Task cancellation can occur as part of normal test execution; ignore it. |
Plan for Creating Tests for input.py
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.