Skip to content

tests: Add comprehensive test suite for Telegram handlers - #718

Open
ImMohammad20000 wants to merge 8 commits into
devfrom
telegram/tests
Open

tests: Add comprehensive test suite for Telegram handlers#718
ImMohammad20000 wants to merge 8 commits into
devfrom
telegram/tests

Conversation

@ImMohammad20000

@ImMohammad20000 ImMohammad20000 commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Summary

Type of change

  • Tests / CI
  • Bug fix
  • New feature
  • Breaking change
  • Refactor / cleanup
  • Documentation

Checklist

  • I tested the change locally or explained why it cannot be tested.

  • I added or updated tests for behavior changes.

  • I did not include secrets, tokens, private keys, or unrelated changes.

  • I updated documentation, translations, or examples if needed.

  • I checked database migrations when models or schema changed.

Summary by CodeRabbit

  • Tests
    • Expanded automated coverage for Telegram admin bulk actions, confirmations, main menu actions, and user management flows.
    • Added reusable Telegram test doubles and async fixtures for state, messages, callbacks, inline queries, and dummy text lookups.
    • Improved test database isolation/cleanup: SQLite schema reset, plus targeted truncation for PostgreSQL and MySQL to keep test runs consistent.

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 57dda7e9-8ba0-4fb2-9b6f-e677b0a5ce81

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Walkthrough

The PR adds shared Telegram test fakes and broad async coverage for admin, client, base, and error handlers. It also improves test database isolation across SQLite, PostgreSQL, and MySQL and removes stale local SQLite artifacts before migrations.

Changes

Telegram fixtures and local database cleanup

Layer / File(s) Summary
Telegram fixtures and local database cleanup
tests/api/__init__.py, tests/telegram/conftest.py
Adds reusable Telegram events, FSM state, user fixtures, and cleanup of local SQLite database artifacts before migrations.

Bulk admin action flows

Layer / File(s) Summary
Bulk admin action flows
tests/telegram/handlers/admin/test_bulk_actions.py
Covers bulk template creation, validation, username strategies, expiration operations, and data-limit updates.

Admin menu and user handlers

Layer / File(s) Summary
Admin menu and user handlers
tests/telegram/handlers/admin/test_confirm_action.py, tests/telegram/handlers/admin/test_main_menu.py, tests/telegram/handlers/admin/test_user.py
Tests admin confirmations, menu actions, FSM transitions, validation, user operations, templates, authorization, and callbacks.

Client, base, and error handlers

Layer / File(s) Summary
Client, base, and error handlers
tests/telegram/handlers/client/test_show_info.py, tests/telegram/handlers/test_base.py, tests/telegram/handlers/test_error_handler.py
Covers text/file responses, startup fallbacks, admin state cleanup, and truncated validation errors.

Cross-database test isolation

Layer / File(s) Summary
Cross-database test isolation
tests/test_record_usages.py
Uses SQLite schema recreation and PostgreSQL/MySQL table truncation during fixture setup and cleanup.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Poem

I’m a rabbit with fixtures in tow,
Testing each handler’s flow.
SQLite leaves clean,
While mocks hop unseen,
And every callback says “go!”

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: a broad new test suite for Telegram handlers.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch telegram/tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ImMohammad20000

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot added the Backend label Jul 22, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/telegram/handlers/test_error_handler.py (1)

16-18: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Assert the cleanup and alert contracts, not only that calls occurred.

The message-path test does not verify old messages are deleted or state is cleared; the callback-path test would pass without show_alert=True or the required ... truncation suffix.

Proposed assertions
 async def test_handle_exception_value_error_message_path(monkeypatch, fake_state, fake_message):
     state = fake_state
+    state._state = "busy"
     state._data = {"messages_to_delete": [1, 2]}
 ...
     await error_handler.handle_exception(event, state)

     msg.answer.assert_awaited_once()
+    msg.bot.delete_messages.assert_awaited_once_with(msg.chat.id, [1, 2])
+    assert await state.get_state() is None
+    assert await state.get_value("messages_to_delete") == [msg.message_id + 1]
 ...
     callback.answer.assert_awaited_once()
     sent_text = callback.answer.await_args.args[0]
     assert len(sent_text) == 200
+    assert sent_text.endswith("...")
+    assert callback.answer.await_args.kwargs == {"show_alert": True}

Also applies to: 33-37

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/telegram/handlers/test_error_handler.py` around lines 16 - 18,
Strengthen the error-handler tests around handle_exception in both message and
callback paths: assert that prior messages are deleted, state is cleared, and
the user notification uses show_alert=True with the required “...” truncation
suffix. Verify the exact cleanup and alert arguments rather than only checking
that answer was awaited.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/api/__init__.py`:
- Around line 105-112: Update the database cleanup block in the test setup to
run only for SQLite URLs, parse the configured database path from DATABASE_URL,
and remove that exact database file plus its -shm and -wal sidecars. Stop
checking or deleting hardcoded CWD paths such as test.db, while preserving the
existing best-effort cleanup behavior.

---

Nitpick comments:
In `@tests/telegram/handlers/test_error_handler.py`:
- Around line 16-18: Strengthen the error-handler tests around handle_exception
in both message and callback paths: assert that prior messages are deleted,
state is cleared, and the user notification uses show_alert=True with the
required “...” truncation suffix. Verify the exact cleanup and alert arguments
rather than only checking that answer was awaited.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3d75c352-6714-4b6b-86e7-d4c2e7178ed5

📥 Commits

Reviewing files that changed from the base of the PR and between 4a76381 and 403e956.

📒 Files selected for processing (10)
  • tests/api/__init__.py
  • tests/telegram/conftest.py
  • tests/telegram/handlers/admin/test_bulk_actions.py
  • tests/telegram/handlers/admin/test_confirm_action.py
  • tests/telegram/handlers/admin/test_main_menu.py
  • tests/telegram/handlers/admin/test_user.py
  • tests/telegram/handlers/client/test_show_info.py
  • tests/telegram/handlers/test_base.py
  • tests/telegram/handlers/test_error_handler.py
  • tests/test_record_usages.py

Comment thread tests/api/__init__.py Outdated
@ImMohammad20000 ImMohammad20000 changed the title test: Add comprehensive test suite for Telegram handlers tests: Add comprehensive test suite for Telegram handlers Jul 22, 2026
@coderabbitai coderabbitai Bot removed the Backend label Jul 22, 2026
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.

1 participant