Skip to content

Add test suite for input.py with prompt_toolkit mocking - #20

Merged
hzhangxyz merged 4 commits into
mainfrom
copilot/add-tests-for-input-py
Dec 22, 2025
Merged

Add test suite for input.py with prompt_toolkit mocking#20
hzhangxyz merged 4 commits into
mainfrom
copilot/add-tests-for-input-py

Conversation

Copilot AI commented Dec 22, 2025

Copy link
Copy Markdown
Contributor

Plan for Creating Tests for input.py

  • Understand how to mock/simulate prompt_toolkit input for testing
  • Create test_input.py with similar structure to test_output.py
  • Test 1: Test valid input parsing and storing in database (Facts)
  • Test 2: Test valid input that generates Ideas
  • Test 3: Test invalid/malformed input handling
  • Test 4: Test empty input handling (should continue loop)
  • Test 5: Test cancellation handling (similar to output test)
  • Test 6: Test KeyboardInterrupt handling
  • Test 7: Test multiple entries
  • Run tests to verify they work correctly (all 7 tests pass)
  • Address code review feedback:
    • Reorganize imports: stdlib imports (unittest.mock) before third-party imports (pytest)
    • Improve invalid input test to use "=>" instead of "invalid((data"
    • Remove blank lines between import groups
    • Reorder mock imports: patch before AsyncMock and MagicMock
  • Verify minimal changes and commit final version
Original prompt

This section details on the original issue you should resolve

<issue_title>给input.py写一个类似output.py的tests</issue_title>
<issue_description>需要注意的是,input.py使用了prompt toolkit,给他输入的方式可能比较复杂。</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
Copilot AI changed the title [WIP] Add tests for input.py similar to output.py Add test suite for input.py with prompt_toolkit mocking Dec 22, 2025
Copilot AI requested a review from hzhangxyz December 22, 2025 16:19
Comment thread tests/test_input.py Outdated
import pathlib
import pytest
import pytest_asyncio
from unittest.mock import AsyncMock, patch, MagicMock

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

patch放在最前面或者最后面吧。另外,这个unittest是python官方的么?如果是的话,在pytest前面import吧

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reorganized imports to place unittest.mock (stdlib) before third-party imports like pytest. Commit: b93b7a5

Comment thread tests/test_input.py Outdated
# 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()])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个太丑了, "=>" 单独这样一个箭头也是非法的

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 hzhangxyz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import之间不要有空行

另外mock的那三个,函数patch放在两个类前面

Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>

Copilot AI commented Dec 22, 2025

Copy link
Copy Markdown
Contributor Author

import之间不要有空行

另外mock的那三个,函数patch放在两个类前面

Removed blank lines between imports and reordered mock imports (patch before AsyncMock and MagicMock). Commit: 96d38c0

Copilot AI requested a review from hzhangxyz December 22, 2025 16:36
@hzhangxyz
hzhangxyz marked this pull request as ready for review December 22, 2025 16:40
Copilot AI review requested due to automatic review settings December 22, 2025 16:40
@hzhangxyz
hzhangxyz merged commit fc7686c into main Dec 22, 2025
21 checks passed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 patch and AsyncMock to 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.

Comment thread tests/test_input.py
task = asyncio.create_task(main(addr, engine, session))
try:
await task
except asyncio.CancelledError:

Copilot AI Dec 22, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'except' clause does nothing but pass and there is no explanatory comment.

Copilot uses AI. Check for mistakes.
Comment thread tests/test_input.py
task = asyncio.create_task(main(addr, engine, session))
try:
await task
except asyncio.CancelledError:

Copilot AI Dec 22, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'except' clause does nothing but pass and there is no explanatory comment.

Copilot uses AI. Check for mistakes.
Comment thread tests/test_input.py
task = asyncio.create_task(main(addr, engine, session))
try:
await task
except asyncio.CancelledError:

Copilot AI Dec 22, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'except' clause does nothing but pass and there is no explanatory comment.

Copilot uses AI. Check for mistakes.
Comment thread tests/test_input.py
task = asyncio.create_task(main(addr, engine, session))
try:
await task
except asyncio.CancelledError:

Copilot AI Dec 22, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'except' clause does nothing but pass and there is no explanatory comment.

Suggested change
except asyncio.CancelledError:
except asyncio.CancelledError:
# Expected - main cancels its task when input loop terminates

Copilot uses AI. Check for mistakes.
Comment thread tests/test_input.py
task = asyncio.create_task(main(addr, engine, session))
try:
await task
except asyncio.CancelledError:

Copilot AI Dec 22, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'except' clause does nothing but pass and there is no explanatory comment.

Suggested change
except asyncio.CancelledError:
except asyncio.CancelledError:
# Task cancellation can occur as part of normal test execution; ignore it.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

给input.py写一个类似output.py的tests

3 participants