Skip to content

Conversation

@avirajsingh7
Copy link
Collaborator

@avirajsingh7 avirajsingh7 commented Jul 24, 2025

Summary

This PR fixes a compatibility issue with Python 3.12 that caused all tests in the scripts/ directory to fail due to incorrect usage of called_once_with on a mock object.

Target issue is #228

In Python 3.11 and earlier, using called_once_with as an attribute on a mock did not raise an error. However, Python 3.12 enforces stricter checks, and using called_once_with directly now results in:

  • Replaced incorrect called_once_with usage with the correct assert_called_once_with(...) assertion.
  • Properly mocked the Session context manager and select call inside the init() function.
  • Ensured the test simulates a successful DB connection scenario.

Checklist

Before submitting a pull request, please ensure that you mark these task.

  • Ran fastapi run --reload app/main.py or docker compose up in the repository root and test.
  • If you've fixed a bug or added code that is tested and has test cases.

Summary by CodeRabbit

  • Tests
    • Simplified and consolidated tests for backend and test initialization processes, replacing previous tests with streamlined versions that focus on successful connection scenarios.

@coderabbitai
Copy link

coderabbitai bot commented Jul 24, 2025

Walkthrough

The test files for backend and test pre-start initialization were refactored to consolidate their logic. Each now contains a single, simplified test that mocks session and select behavior, removes logger patching, and focuses on verifying successful execution of the init function by checking that the session's exec method is called as expected.

Changes

File(s) Change Summary
backend/app/tests/scripts/test_backend_pre_start.py,
backend/app/tests/scripts/test_test_pre_start.py
Refactored tests: removed test_init_successful_connection, added test_init_success, simplified mocks, removed logger patching, and streamlined test logic to focus on session execution and success verification.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

In the warren where tests reside,
Refactoring swept the old aside.
With mocks and patches, clean and neat,
The rabbit ensures connections complete.
Fewer lines, a lighter load—
Hippity-hop down the testing road! 🐇

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f1d651b and 0b086a8.

📒 Files selected for processing (2)
  • backend/app/tests/scripts/test_backend_pre_start.py (1 hunks)
  • backend/app/tests/scripts/test_test_pre_start.py (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
backend/app/tests/scripts/test_test_pre_start.py (1)
backend/app/tests_pre_start.py (1)
  • init (22-29)
🔇 Additional comments (10)
backend/app/tests/scripts/test_test_pre_start.py (5)

2-2: LGTM: Import statement is correct.

The import correctly references the init function from app.tests_pre_start module.


5-8: LGTM: Proper mock setup for Session context manager.

The mock session setup correctly implements the context manager protocol by setting __enter__ to return the mock session itself, and configures the exec method to return None. This accurately simulates the behavior expected by the init function.


10-13: LGTM: Appropriate patching strategy.

The patching approach correctly mocks both the Session constructor and the select function within the app.tests_pre_start module scope, ensuring the test has full control over the dependencies.


15-15: LGTM: Correct function invocation.

Calling init with a MagicMock() engine is appropriate since the actual engine parameter isn't used when the Session constructor is mocked.


23-23: LGTM: Correct assertion method for Python 3.12 compatibility.

Using assert_called_once_with(fake_select) is the correct approach for Python 3.12. This fixes the compatibility issue mentioned in the PR objectives where called_once_with was incorrectly used as an attribute instead of a proper assertion method.

backend/app/tests/scripts/test_backend_pre_start.py (5)

2-2: LGTM: Import statement is correct.

The import correctly references the init function from app.backend_pre_start module.


5-8: LGTM: Consistent mock setup for Session context manager.

The mock session setup is identical to the other test file and correctly implements the context manager protocol, ensuring consistency across the test suite.


11-13: LGTM: Correct module-specific patching.

The patching correctly targets app.backend_pre_start.Session and app.backend_pre_start.select, ensuring the mocks are applied to the correct module scope for this test.


15-15: LGTM: Appropriate function call.

Calling init with a MagicMock() engine is consistent with the other test and appropriate for this mocked scenario.


23-23: LGTM: Python 3.12 compatible assertion.

The use of assert_called_once_with(fake_select) correctly addresses the Python 3.12 compatibility issue by using the proper assertion method instead of the problematic called_once_with attribute.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/tests_fails_in_python3.12

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@avirajsingh7 avirajsingh7 added the bug Something isn't working label Jul 24, 2025
@avirajsingh7 avirajsingh7 self-assigned this Jul 24, 2025
@codecov
Copy link

codecov bot commented Jul 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@AkhileshNegi AkhileshNegi merged commit 50708c0 into main Jul 29, 2025
3 checks passed
@AkhileshNegi AkhileshNegi deleted the fix/tests_fails_in_python3.12 branch July 29, 2025 04:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ready-for-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tests in scripts/ fail on Python 3.12 due to incorrect use of called_once_with

4 participants