Skip to content

Conversation

@nishika26
Copy link
Collaborator

@nishika26 nishika26 commented Jul 17, 2025

Summary

Target issue is #204
Explain the motivation for making this change. What existing problem does the pull request solve?
WIP

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.

Notes

This PR introduces the creation of a .env.test file for testing configurations, along with a utility function that loads the test environment variables from this file if it exists. It also adds an .env.test.example file as a reference for developers to easily create their own .env.test files. These changes ensure that testing can be done in an isolated environment without affecting the production settings.

Changes:

  • Created .env.test File:

    • Added .env.test to store environment-specific variables for testing, ensuring that the testing environment is isolated from production.
  • Added .env.test.example File:

    • Provided an example .env.test.example file to guide developers in setting up their local .env.test file. This file includes placeholders for necessary environment variables.
  • Utility Function to Load Test Environment Variables:

    • Created the load_environment function that:

    • Loads the environment variables from .env.test if the file exists.

    • If the .env.test file is missing, it prints a warning and uses the default environment settings.

    • After overriding the .env with .env.test, we call settings.init() to reinitialize the settings object, ensuring that it picks up the new environment variables defined in the test configuration

  • Updated conftest.py:

    • The load_environment function is now referred to in conftest.py to ensure the test environment is loaded automatically before test execution.

Summary by CodeRabbit

  • New Features

    • Added an example environment configuration file for test setups.
    • Enhanced test environment management with new utilities for loading environment variables and seeding baseline data.
  • Bug Fixes

    • Improved database connection handling in tests to ensure proper cleanup after each test.
  • Chores

    • Removed unused environment variables and configuration fields related to OpenAI and Langfuse from code, environment files, and Docker setup.
    • Updated continuous integration and benchmark workflows to use dedicated test environment files and remove unnecessary secrets.
    • Improved connection pool management for database reliability.
    • Cleaned up and reorganized import statements in test files.
    • Broadened .gitignore to exclude all .env* files.

@coderabbitai
Copy link

coderabbitai bot commented Jul 17, 2025

Walkthrough

This change set introduces improved test environment management and database connection handling. It adds a .env.test.example template, updates test fixtures for better environment and session control, and enhances database engine configuration. Unused environment variables and configuration fields related to OpenAI and Langfuse are removed across the codebase, with corresponding updates to workflow and compose files.

Changes

Cohort / File(s) Change Summary
Test Environment & Fixtures
backend/app/tests/conftest.py, backend/app/tests/utils/utils.py, .env.test.example
Adds a .env.test.example template. Introduces load_environment utility and new/updated test fixtures for environment loading, baseline seeding, and session cleanup.
Database Engine Configuration
backend/app/core/db.py
Adds explicit connection pool settings to the SQLAlchemy engine, with environment-based tuning and improved connection reliability.
Environment Variable Cleanup
.env.example, backend/app/core/config.py, docker-compose.yml, .github/workflows/continuous_integration.yml, .github/workflows/benchmark.yml
Removes OpenAI and Langfuse-related environment variables and config fields from environment files, config class, Docker Compose, and GitHub workflows.
Test Imports & Minor Refactoring
backend/app/tests/api/routes/documents/test_route_document_upload.py, backend/app/tests/api/routes/test_onboarding.py, backend/app/tests/crud/test_thread_result.py
Cleans up and consolidates imports, removes unused imports, and adjusts import order. No logic changes.
Pre-start Script Logging
backend/app/tests_pre_start.py
Loads .env.test before engine import, removes a commented-out line, and adds a log statement showing the masked database URL after initialization.
Git Ignore Update
.gitignore
Broadens ignore pattern from .env to .env* to cover all env files.

Sequence Diagram(s)

sequenceDiagram
    participant Pytest
    participant conftest.py
    participant utils.py
    participant Database

    Pytest->>conftest.py: Start session (autouse fixture)
    conftest.py->>utils.py: load_environment(".env.test")
    utils.py->>utils.py: Load env vars, validate, check "test" DB
    conftest.py->>Database: Seed baseline data
    Pytest->>conftest.py: Start test (autouse fixture)
    conftest.py->>Database: Run test with session
    conftest.py->>Database: Dispose engine after test
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15 minutes

Possibly related PRs

  • Testcases: Using DB Transactions #278: Both PRs modify the db fixture in backend/app/tests/conftest.py to implement session-scoped baseline seeding and function-scoped transactional test sessions, directly relating their changes at the fixture and test database management level.

Suggested labels

on hold

Suggested reviewers

  • nishika26
  • kartpop

Poem

In the warren where test bunnies dwell,
A new .env.test begins to swell.
With fixtures neat and sessions clean,
Our database stays fresh and keen.
Old keys are gone, the pool is set—
Hopping forward, no regrets!
🐇✨

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 1239e2a and 874f177.

📒 Files selected for processing (1)
  • backend/app/tests/utils/utils.py (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • backend/app/tests/utils/utils.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: checks (3.11.7, 6)
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch enhancement/env_test

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
🪧 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.
  • 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.

Support

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

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.

@coderabbitai
Copy link

coderabbitai bot commented Jul 17, 2025

✅ Actions performed

Review triggered.

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.

@nishika26
Copy link
Collaborator Author

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Jul 17, 2025

✅ Actions performed

Review triggered.

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.

@codecov
Copy link

codecov bot commented Jul 17, 2025

Codecov Report

❌ Patch coverage is 94.11765% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
backend/app/tests/utils/utils.py 90.62% 3 Missing ⚠️
backend/app/tests_pre_start.py 80.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b404bee and ac7495d.

📒 Files selected for processing (2)
  • backend/app/tests/conftest.py (1 hunks)
  • backend/app/tests/utils/utils.py (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
backend/app/tests/conftest.py (2)
backend/app/api/deps.py (1)
  • get_db (33-35)
backend/app/tests/utils/utils.py (2)
  • get_superuser_token_headers (38-47)
  • load_environment (90-98)
🪛 Ruff (0.12.2)
backend/app/tests/conftest.py

4-4: os imported but unused

Remove unused import: os

(F401)


8-8: dotenv.load_dotenv imported but unused

Remove unused import: dotenv.load_dotenv

(F401)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: checks (3.11.7, 6)
🔇 Additional comments (3)
backend/app/tests/utils/utils.py (1)

5-6: LGTM! Imports are correctly added for the new functionality.

The os and dotenv.load_dotenv imports are properly added to support the new environment loading functionality.

backend/app/tests/conftest.py (2)

19-21: LGTM! Fixture implementation is correct.

The load_test_env fixture is properly configured with session scope and autouse enabled, ensuring test environment variables are loaded automatically before any tests run. The implementation correctly calls the load_environment function from the utils module.


15-15: LGTM! Import is correctly added.

The import of load_environment from the utils module is properly added to support the new fixture functionality.

@nishika26 nishika26 changed the title overriding env with test env Testing: overriding env with test env Jul 17, 2025
@nishika26 nishika26 self-assigned this Jul 17, 2025
@nishika26 nishika26 added the enhancement New feature or request label Jul 17, 2025
@nishika26 nishika26 marked this pull request as ready for review July 17, 2025 16:53
Copy link

@coderabbitai coderabbitai bot left a comment

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)
.env.test.example (1)

8-13: Keep quoting style and key order consistent

Some values (EMAIL_TEST_USER, PROJECT_NAME) are quoted while adjacent ones are not, and linter warnings flag the ordering.
Uniform style improves readability and avoids false-positive drift with tools like dotenv-linter. Consider:

-FIRST_SUPERUSER=superuser@example.com
-FIRST_SUPERUSER_PASSWORD=changethis
-EMAIL_TEST_USER="test@example.com"
+EMAIL_TEST_USER=test@example.com
+FIRST_SUPERUSER=superuser@example.com
+FIRST_SUPERUSER_PASSWORD=changethis

(Similarly adjust other sections.)
This is cosmetic but helps future diff hygiene.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ac7495d and bdb0459.

📒 Files selected for processing (1)
  • .env.test.example (1 hunks)
🧰 Additional context used
🪛 dotenv-linter (3.3.0)
.env.test.example

[warning] 10-10: [UnorderedKey] The FIRST_SUPERUSER key should go before the SECRET_KEY key


[warning] 11-11: [UnorderedKey] The FIRST_SUPERUSER_PASSWORD key should go before the SECRET_KEY key


[warning] 12-12: [QuoteCharacter] The value has quote characters (', ")


[warning] 12-12: [UnorderedKey] The EMAIL_TEST_USER key should go before the FIRST_SUPERUSER key


[warning] 17-17: [UnorderedKey] The POSTGRES_PORT key should go before the POSTGRES_SERVER key


[warning] 18-18: [UnorderedKey] The POSTGRES_DB key should go before the POSTGRES_PORT key


[warning] 20-20: [UnorderedKey] The POSTGRES_PASSWORD key should go before the POSTGRES_PORT key


[warning] 33-33: [UnorderedKey] The AWS_DEFAULT_REGION key should go before the AWS_SECRET_ACCESS_KEY key


[warning] 34-34: [SpaceCharacter] The line has spaces around equal sign


[warning] 34-34: [UnorderedKey] The AWS_S3_BUCKET_PREFIX key should go before the AWS_SECRET_ACCESS_KEY key


[warning] 38-38: [QuoteCharacter] The value has quote characters (', ")


[warning] 39-39: [QuoteCharacter] The value has quote characters (', ")


[warning] 39-39: [UnorderedKey] The LANGFUSE_PUBLIC_KEY key should go before the OPENAI_API_KEY key


[warning] 40-40: [QuoteCharacter] The value has quote characters (', ")


[warning] 40-40: [UnorderedKey] The LANGFUSE_SECRET_KEY key should go before the OPENAI_API_KEY key


[warning] 41-41: [QuoteCharacter] The value has quote characters (', ")


[warning] 41-41: [UnorderedKey] The LANGFUSE_HOST key should go before the LANGFUSE_PUBLIC_KEY key


[warning] 45-45: [QuoteCharacter] The value has quote characters (', ")

🔇 Additional comments (1)
.env.test.example (1)

38-42: Placeholders are fine, but double-check secrets aren’t real

The example keys for OpenAI and Langfuse are clearly dummy values—good. Ensure the real .env.test is git-ignored so secrets never land in the repo.

@avirajsingh7 avirajsingh7 linked an issue Jul 21, 2025 that may be closed by this pull request
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (1)
backend/app/tests/conftest.py (1)

9-9: Remove unused import.

The load_dotenv import is not used directly in this file since environment loading is handled by the load_environment function.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between bdb0459 and ec994bc.

📒 Files selected for processing (2)
  • backend/app/tests/conftest.py (2 hunks)
  • backend/app/tests/utils/utils.py (2 hunks)
🧬 Code Graph Analysis (1)
backend/app/tests/conftest.py (1)
backend/app/tests/utils/utils.py (1)
  • load_environment (98-106)
🪛 Ruff (0.12.2)
backend/app/tests/conftest.py

9-9: dotenv.load_dotenv imported but unused

Remove unused import: dotenv.load_dotenv

(F401)


22-22: Undefined name load_environment

(F821)

🚧 Files skipped from review as they are similar to previous changes (1)
  • backend/app/tests/utils/utils.py
🧰 Additional context used
🧬 Code Graph Analysis (1)
backend/app/tests/conftest.py (1)
backend/app/tests/utils/utils.py (1)
  • load_environment (98-106)
🪛 Ruff (0.12.2)
backend/app/tests/conftest.py

9-9: dotenv.load_dotenv imported but unused

Remove unused import: dotenv.load_dotenv

(F401)


22-22: Undefined name load_environment

(F821)

🔇 Additional comments (1)
backend/app/tests/conftest.py (1)

20-23: Well-designed fixture for test environment isolation.

The session-scoped, auto-use fixture appropriately ensures test environment variables are loaded once at the beginning of test execution, achieving the PR objective of environment isolation.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (2)
.env.test.example (1)

21-21: Remove the spaces around the = to prevent an invalid key name.

The current line has spaces around the equal sign which can cause the key to include trailing spaces, making environment variable lookups fail silently.

-AWS_S3_BUCKET_PREFIX = "bucket-prefix-name"
+AWS_S3_BUCKET_PREFIX="bucket-prefix-name"
backend/app/tests/utils/utils.py (1)

108-108: Settings reinitialization may not work properly.

The settings.__init__() call may not properly reload environment variables from the newly loaded .env.test file, depending on how the Settings class is implemented. Based on the Settings class in backend/app/core/config.py, it uses Pydantic's BaseSettings which loads environment variables during instantiation.

Consider creating a new settings instance or using a more reliable approach:

-        settings.__init__()
+        # Force reload by creating new settings instance
+        from importlib import reload
+        from app.core import config
+        reload(config)

Alternatively, verify that the current approach works correctly with your Settings implementation.

🧹 Nitpick comments (3)
.env.test.example (1)

6-6: Remove unnecessary quotes from environment variable values.

Environment variables don't need quotes unless the values contain spaces or special characters that require escaping. The quotes will be included as part of the actual values.

-EMAIL_TEST_USER="test@example.com"
+EMAIL_TEST_USER=test@example.com
-OPENAI_API_KEY="this_is_not_a_secret"
+OPENAI_API_KEY=this_is_not_a_secret

Also applies to: 25-25

backend/app/tests/utils/utils.py (1)

13-13: Remove unused import.

The Settings import is not used in this file.

-from app.core.config import settings, Settings
+from app.core.config import settings
backend/app/tests/conftest.py (1)

35-35: Remove debug print statements before merging.

The debug print statements should be removed before merging to production, as they will clutter test output.

-    print("enginer_1", engine)
-    print("enginer_2", engine)
-        print("Seeding baseline data...")

Also note the typo in "enginer" - should be "engine" if the print statements are kept temporarily.

Also applies to: 54-54, 56-56

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ec994bc and 4703a57.

📒 Files selected for processing (3)
  • .env.test.example (1 hunks)
  • backend/app/tests/conftest.py (2 hunks)
  • backend/app/tests/utils/utils.py (2 hunks)
🧬 Code Graph Analysis (1)
backend/app/tests/utils/utils.py (1)
backend/app/core/config.py (1)
  • Settings (26-100)
🪛 Ruff (0.12.2)
backend/app/tests/utils/utils.py

13-13: app.core.config.Settings imported but unused

Remove unused import: app.core.config.Settings

(F401)

🪛 dotenv-linter (3.3.0)
.env.test.example

[warning] 4-4: [UnorderedKey] The FIRST_SUPERUSER key should go before the SECRET_KEY key


[warning] 5-5: [UnorderedKey] The FIRST_SUPERUSER_PASSWORD key should go before the SECRET_KEY key


[warning] 6-6: [QuoteCharacter] The value has quote characters (', ")


[warning] 6-6: [UnorderedKey] The EMAIL_TEST_USER key should go before the FIRST_SUPERUSER key


[warning] 11-11: [UnorderedKey] The POSTGRES_PORT key should go before the POSTGRES_SERVER key


[warning] 12-12: [UnorderedKey] The POSTGRES_DB key should go before the POSTGRES_PORT key


[warning] 14-14: [UnorderedKey] The POSTGRES_PASSWORD key should go before the POSTGRES_PORT key


[warning] 20-20: [UnorderedKey] The AWS_DEFAULT_REGION key should go before the AWS_SECRET_ACCESS_KEY key


[warning] 21-21: [SpaceCharacter] The line has spaces around equal sign


[warning] 21-21: [UnorderedKey] The AWS_S3_BUCKET_PREFIX key should go before the AWS_SECRET_ACCESS_KEY key


[warning] 25-25: [QuoteCharacter] The value has quote characters (', ")

🧰 Additional context used
🧬 Code Graph Analysis (1)
backend/app/tests/utils/utils.py (1)
backend/app/core/config.py (1)
  • Settings (26-100)
🪛 Ruff (0.12.2)
backend/app/tests/utils/utils.py

13-13: app.core.config.Settings imported but unused

Remove unused import: app.core.config.Settings

(F401)

🪛 dotenv-linter (3.3.0)
.env.test.example

[warning] 4-4: [UnorderedKey] The FIRST_SUPERUSER key should go before the SECRET_KEY key


[warning] 5-5: [UnorderedKey] The FIRST_SUPERUSER_PASSWORD key should go before the SECRET_KEY key


[warning] 6-6: [QuoteCharacter] The value has quote characters (', ")


[warning] 6-6: [UnorderedKey] The EMAIL_TEST_USER key should go before the FIRST_SUPERUSER key


[warning] 11-11: [UnorderedKey] The POSTGRES_PORT key should go before the POSTGRES_SERVER key


[warning] 12-12: [UnorderedKey] The POSTGRES_DB key should go before the POSTGRES_PORT key


[warning] 14-14: [UnorderedKey] The POSTGRES_PASSWORD key should go before the POSTGRES_PORT key


[warning] 20-20: [UnorderedKey] The AWS_DEFAULT_REGION key should go before the AWS_SECRET_ACCESS_KEY key


[warning] 21-21: [SpaceCharacter] The line has spaces around equal sign


[warning] 21-21: [UnorderedKey] The AWS_S3_BUCKET_PREFIX key should go before the AWS_SECRET_ACCESS_KEY key


[warning] 25-25: [QuoteCharacter] The value has quote characters (', ")

🔇 Additional comments (5)
.env.test.example (1)

1-26: Good template structure for test environment configuration.

The file provides a comprehensive template covering all necessary environment variables for testing including database, AWS, and API configurations.

backend/app/tests/utils/utils.py (1)

126-128: Inconsistent error handling approach.

The function now raises a FileNotFoundError when the .env.test file is missing, which differs from the warning approach suggested in past reviews. This makes the function more strict but may not align with the original intent of graceful fallback.

Please clarify the intended behavior:

  1. Should missing .env.test file cause tests to fail (current implementation)?
  2. Or should it warn and continue with default settings (past review suggestion)?

The current approach ensures test environment isolation but requires .env.test to always exist.

backend/app/tests/conftest.py (3)

19-22: Good session-scoped fixture for test environment loading.

The load_test_env fixture properly loads the test environment at session scope before any tests run, ensuring consistent environment setup. The relative path ../.env.test correctly references the expected location.


24-30: Excellent centralization of database connection management.

The engine_connection fixture provides a clean way to share database engine and connection across test fixtures, improving resource reuse and test performance.


32-48: Good refactoring to use shared database connection.

The updated db fixture properly utilizes the shared connection from engine_connection, maintaining transaction isolation while improving resource efficiency.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4703a57 and e290dd7.

📒 Files selected for processing (1)
  • .github/workflows/continuous_integration.yml (1 hunks)

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

♻️ Duplicate comments (2)
backend/app/tests/utils/utils.py (2)

99-99: Replace hardcoded absolute path with relative path.

The hardcoded absolute path /Users/nishikayadav/Desktop/platform/.env.test will fail for other developers and CI environments.

Apply this fix:

-    env_test_path: str = "./Users/nishikayadav/Desktop/platform/.env.test",
+    env_test_path: str = "../.env.test",

108-108: Potential issue with settings reinitialization.

The settings.__init__() call may not properly reinitialize the settings object, as it depends on the internal implementation of the settings class. This was previously flagged and the concern remains valid.

Consider verifying that settings.__init__() properly reloads all environment variables for your specific settings implementation, or use an alternative approach to force settings reload.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between bbdf4de and b7f26cd.

📒 Files selected for processing (1)
  • backend/app/tests/utils/utils.py (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
backend/app/tests/utils/utils.py (1)
backend/app/models/assistants.py (1)
  • Assistant (29-40)
🪛 Ruff (0.12.2)
backend/app/tests/utils/utils.py

132-132: Blank line contains whitespace

(W293)


133-133: SyntaxError: unindent does not match any outer indentation level

🪛 GitHub Actions: AI Platform CI
backend/app/tests/utils/utils.py

[error] 133-133: Black formatting failed: unindent does not match any outer indentation level (tokenize error at line 133).


[error] Pre-commit hook 'trailing-whitespace' failed and modified the file to fix trailing whitespace issues.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (1)
backend/app/tests/utils/utils.py (1)

102-139: Address previous review feedback and align with PR objectives.

The current implementation contradicts the PR objectives and ignores previous review feedback. The function should provide graceful fallback when .env.test is missing, not raise an exception that will cause tests to fail.

Based on the past review comments and PR objectives, apply these improvements:

+import logging
+
+logger = logging.getLogger(__name__)
+
 def load_environment(env_test_path: str = "../.env.test"):
     """Loads the test environment variables if the .env.test file exists.
     
-    Raises an error if any required PostgreSQL credentials are missing or if the
-    POSTGRES_DB value does not contain the word 'test', to ensure a safe test database is used.
+    Falls back to default environment if .env.test is missing.
+    Validates PostgreSQL credentials and ensures test database usage when .env.test exists.
     """
     
     if os.path.exists(env_test_path):
         load_dotenv(dotenv_path=env_test_path, override=True)
-        settings.__init__()
+        # Force reload of settings - verify this works with your settings implementation
+        settings.__init__()
         
         required_vars = [
             "POSTGRES_USER", 
             "POSTGRES_PASSWORD",
             "POSTGRES_SERVER",
             "POSTGRES_PORT", 
             "POSTGRES_DB",
         ]
         
         missing_vars = [var for var in required_vars if not os.getenv(var)]
         
         if missing_vars:
             raise ValueError(
                 f"Missing the following PostgreSQL credentials in {env_test_path}: {', '.join(missing_vars)}"
             )
             
         db_name = os.getenv("POSTGRES_DB", "").lower()
         if "test" not in db_name:
             raise RuntimeError(
                 f"Connected to database '{db_name}', which doesn't appear to be a test database"
             )
     else:
-        raise FileNotFoundError(
-            f"{env_test_path} not found. No environment variables loaded."
-        )
+        logger.warning(
+            f"{env_test_path} not found. Using default environment settings."
+        )
     
     return settings
🧹 Nitpick comments (3)
backend/app/tests/utils/utils.py (1)

98-99: Remove unused imports identified by static analysis.

The make_url and create_engine imports from SQLAlchemy are not used in this file and should be removed to clean up the code.

-from sqlalchemy.engine.url import make_url
-from sqlalchemy import create_engine
backend/app/tests/conftest.py (2)

4-4: Remove unused import.

The create_engine import from sqlmodel is not used in this file.

-from sqlmodel import Session, create_engine
+from sqlmodel import Session

13-23: Reorganize imports to follow PEP 8 while maintaining fixture functionality.

The current import organization violates PEP 8 by having module-level imports scattered throughout the file. However, the load_test_env fixture needs the load_environment function imported.

Reorganize the imports to the top of the file:

 from collections.abc import Generator
 import pytest
 from fastapi.testclient import TestClient
 from sqlmodel import Session
 from sqlalchemy import event
+
+from app.core.config import settings
+from app.core.db import engine
+from app.api.deps import get_db
+from app.main import app
+from app.models import APIKeyPublic
+from app.seed_data.seed_data import seed_database
+from app.tests.utils.user import authentication_token_from_email
+from app.tests.utils.utils import (
+    get_superuser_token_headers,
+    get_api_key_by_email,
+    load_environment,
+)


 @pytest.fixture(scope="session", autouse=True)
 def load_test_env():
     load_environment("../.env.test")


-from app.api.deps import get_db
-from app.main import app
-from app.models import APIKeyPublic
-from app.tests.utils.user import authentication_token_from_email
-
-
-@pytest.fixture(scope="session", autouse=True)
-def load_test_env():
-    load_environment("../.env.test")
-
-
-from app.core.config import settings
-from app.tests.utils.utils import (
-    get_superuser_token_headers,
-    get_api_key_by_email,
-    load_environment,
-)
-from app.seed_data.seed_data import seed_database
-from app.core.db import engine
-
-
 @pytest.fixture(scope="session", autouse=True)
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b7f26cd and 08ed5c6.

📒 Files selected for processing (3)
  • backend/app/tests/conftest.py (1 hunks)
  • backend/app/tests/utils/utils.py (2 hunks)
  • backend/app/tests_pre_start.py (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
backend/app/tests_pre_start.py (3)
backend/app/tests/utils/utils.py (1)
  • load_environment (102-139)
backend/app/backend_pre_start.py (1)
  • main (32-35)
backend/app/tests/scripts/test_test_pre_start.py (1)
  • test_init_successful_connection (8-33)
🪛 GitHub Actions: AI Platform CI
backend/app/tests_pre_start.py

[error] 9-9: RuntimeError: Connected to database 'ai_platform', which doesn't appear to be a test database

🪛 Ruff (0.12.2)
backend/app/tests/conftest.py

4-4: sqlmodel.create_engine imported but unused

Remove unused import: sqlmodel.create_engine

(F401)


18-18: Module level import not at top of file

(E402)


19-23: Module level import not at top of file

(E402)


24-24: Module level import not at top of file

(E402)


25-25: Module level import not at top of file

(E402)

backend/app/tests/utils/utils.py

98-98: Module level import not at top of file

(E402)


98-98: sqlalchemy.engine.url.make_url imported but unused

Remove unused import: sqlalchemy.engine.url.make_url

(F401)


99-99: Module level import not at top of file

(E402)


99-99: sqlalchemy.create_engine imported but unused

Remove unused import: sqlalchemy.create_engine

(F401)

🔇 Additional comments (2)
backend/app/tests_pre_start.py (1)

39-41: Useful debugging addition for database URL logging.

The database URL logging with password masking is a helpful addition for debugging connection issues during test setup.

backend/app/tests/conftest.py (1)

28-33: Well-designed session-scoped fixture for baseline data seeding.

The session-scoped autouse fixture for seeding baseline data is an excellent design choice that will improve test performance by avoiding repeated seeding operations.

The print statement provides useful feedback during test setup, and the session scope ensures the seeding happens once per test session.

Comment on lines 7 to 9
from app.tests.utils.utils import load_environment

load_environment("../.env.test")
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

❓ Verification inconclusive

Good placement of environment loading, but pipeline failure needs attention.

The placement of load_environment before importing the database engine is correct and ensures test environment variables are loaded first.

However, the pipeline failure indicates the database validation is working correctly - it detected that 'ai_platform' is not a test database. Ensure the CI environment has:

  1. A proper .env.test file with POSTGRES_DB containing "test" (e.g., "ai_platform_test")
  2. The CI workflow correctly copies .env.test.example to .env.test with appropriate test database configuration

🏁 Script executed:

#!/bin/bash
# Verify CI workflow configuration for .env.test setup
fd -t f "continuous_integration.yml" --exec cat {} | grep -A 10 -B 5 "\.env\.test"

Length of output: 84


I’ll search the workflow directory and the repo for any references to “.env.test” to confirm CI setup.

#!/bin/bash
# List GitHub Actions workflow files
echo "== Workflow files =="
fd -e yml -d 2 .github/workflows || echo "(no workflows directory)"

# Search entire repo for .env.test references
echo "== References to .env.test =="
rg -n "\.env\.test" || echo "(none found)"

CI setup missing .env.test configuration

I searched the repository for any references to .env.test in CI workflows and found none. Without a step to copy or define .env.test, the call to load_environment("../.env.test") will fail in CI, triggering the “non-test database” runtime error.

Please update your CI configuration to:

  • Include a .env.test file (e.g., copy from .env.test.example).
  • Add a workflow step that ensures .env.test is available before tests run.

Example for GitHub Actions (.github/workflows/ci.yml):

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      # …checkout and setup steps…
      - name: Copy test environment file
        run: cp .env.test.example .env.test
      - name: Run pre-start script
        run: python backend/app/tests_pre_start.py
      - name: Run tests
        run: pytest

Once .env.test is correctly configured in CI, the pipeline should load the test database (POSTGRES_DB containing “test”) and pass validation.

🧰 Tools
🪛 GitHub Actions: AI Platform CI

[error] 9-9: RuntimeError: Connected to database 'ai_platform', which doesn't appear to be a test database

🤖 Prompt for AI Agents
In backend/app/tests_pre_start.py around lines 7 to 9, the code loads the
.env.test file, but the CI workflow does not ensure this file exists, causing
failures. Update the CI configuration (e.g., .github/workflows/ci.yml) to
include a step that copies .env.test.example to .env.test before running tests.
This ensures the environment file is present for
load_environment("../.env.test") and prevents runtime errors related to missing
test database configuration.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
.env.test.example (2)

10-11: Remove unnecessary quotes for values without spaces

EMAIL_TEST_USER does not contain spaces, so the surrounding double-quotes are superfluous and trigger dotenv-linter’s QuoteCharacter warning.
Keeping quoting only where it’s needed (e.g., PROJECT_NAME) improves consistency and silences the linter.

-EMAIL_TEST_USER="test@example.com"
+EMAIL_TEST_USER=test@example.com

25-25: Drop quotes from AWS_S3_BUCKET_PREFIX for linter compliance

The value has no spaces; removing the quotes avoids the QuoteCharacter warning while leaving the semantics unchanged.

-AWS_S3_BUCKET_PREFIX="bucket-prefix-name"
+AWS_S3_BUCKET_PREFIX=bucket-prefix-name
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d663406 and ae259aa.

📒 Files selected for processing (4)
  • .env.test.example (1 hunks)
  • .github/workflows/benchmark.yml (0 hunks)
  • backend/app/core/config.py (0 hunks)
  • docker-compose.yml (0 hunks)
💤 Files with no reviewable changes (3)
  • backend/app/core/config.py
  • docker-compose.yml
  • .github/workflows/benchmark.yml
🧰 Additional context used
🪛 dotenv-linter (3.3.0)
.env.test.example

[warning] 8-8: [UnorderedKey] The FIRST_SUPERUSER key should go before the SECRET_KEY key


[warning] 9-9: [UnorderedKey] The FIRST_SUPERUSER_PASSWORD key should go before the SECRET_KEY key


[warning] 10-10: [QuoteCharacter] The value has quote characters (', ")


[warning] 10-10: [UnorderedKey] The EMAIL_TEST_USER key should go before the FIRST_SUPERUSER key


[warning] 15-15: [UnorderedKey] The POSTGRES_PORT key should go before the POSTGRES_SERVER key


[warning] 16-16: [UnorderedKey] The POSTGRES_DB key should go before the POSTGRES_PORT key


[warning] 18-18: [UnorderedKey] The POSTGRES_PASSWORD key should go before the POSTGRES_PORT key


[warning] 24-24: [UnorderedKey] The AWS_DEFAULT_REGION key should go before the AWS_SECRET_ACCESS_KEY key


[warning] 25-25: [QuoteCharacter] The value has quote characters (', ")


[warning] 25-25: [UnorderedKey] The AWS_S3_BUCKET_PREFIX key should go before the AWS_SECRET_ACCESS_KEY key

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: checks (3.11.7, 6)
🔇 Additional comments (1)
.env.test.example (1)

22-24: Clarify placeholder AWS credentials or mock provider

Empty AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY often cause SDKs to raise credential-related exceptions.
If tests rely on real AWS clients, add dummy placeholders (e.g., test) or switch the code to use a local mock such as LocalStack or moto when ENVIRONMENT=local.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (3)
backend/app/tests/utils/utils.py (3)

97-97: Add default parameter value for env_test_path.

The function parameter should have a default value as suggested in previous reviews to make it more convenient to use.

-def load_environment(env_test_path: str):
+def load_environment(env_test_path: str = "../.env.test"):

106-106: Settings reinitialization may not work properly.

The settings.__init__() approach has been flagged in previous reviews as potentially unreliable for reloading environment variables, as it depends on the internal implementation of the settings class.

Consider using a more robust approach:

-        settings.__init__()
+        # Force reload of settings by reimporting the module
+        from app.core.config import settings as _settings
+        global settings
+        settings = _settings

Alternatively, verify that settings.__init__() properly reloads all environment variables for your specific settings implementation.


129-132: Replace FileNotFoundError with warning to align with PR objectives.

The current implementation raises a FileNotFoundError when the .env.test file is missing, but the PR objectives state the goal is "graceful fallback to default environment settings." Raising an exception contradicts this objective and will cause tests to fail when the file is missing.

+import logging
+
+logger = logging.getLogger(__name__)
+
     else:
-        raise FileNotFoundError(
-            f"{env_test_path} not found. No environment variables loaded."
-        )
+        logger.warning(
+            f"{env_test_path} not found. Using default environment settings."
+        )

This approach aligns with the PR objective of enabling graceful fallback when the test environment file is not available.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between de5a19c and aa14f7e.

📒 Files selected for processing (4)
  • .env.example (0 hunks)
  • .github/workflows/continuous_integration.yml (2 hunks)
  • backend/app/tests/api/routes/documents/test_route_document_upload.py (2 hunks)
  • backend/app/tests/utils/utils.py (2 hunks)
💤 Files with no reviewable changes (1)
  • .env.example
🚧 Files skipped from review as they are similar to previous changes (2)
  • .github/workflows/continuous_integration.yml
  • backend/app/tests/api/routes/documents/test_route_document_upload.py
🧰 Additional context used
🪛 Ruff (0.12.2)
backend/app/tests/utils/utils.py

94-94: Module level import not at top of file

(E402)


94-94: Redefinition of unused load_dotenv from line 6

Remove definition: load_dotenv

(F811)


94-94: dotenv.find_dotenv imported but unused

Remove unused import: dotenv.find_dotenv

(F401)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: checks (3.11.7, 6)

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (1)
backend/app/tests/utils/utils.py (1)

98-134: Improve environment loading implementation with better error handling.

The load_environment function has several areas for improvement:

  1. Settings reinitialization issue: The function doesn't actually reload the settings object after loading new environment variables. The settings object is imported at module level and won't reflect the new environment variables.

  2. Security validation: Good implementation of database name validation to prevent accidental production database usage.

  3. Error handling: Proper validation of required PostgreSQL variables.

Apply this improved implementation:

 def load_environment(env_test_path: str = "../.env.test"):
     """Loads the test environment variables if the .env.test file exists.
 
     Raises an error if any required PostgreSQL credentials are missing or if the
     POSTGRES_DB value does not contain the word 'test', to ensure a safe test database is used.
     """
 
     if os.path.exists(env_test_path):
         load_dotenv(env_test_path, override=True)
 
         required_vars = [
             "POSTGRES_USER",
             "POSTGRES_PASSWORD",
             "POSTGRES_SERVER",
             "POSTGRES_PORT",
             "POSTGRES_DB",
         ]
 
         missing_vars = [var for var in required_vars if not os.getenv(var)]
 
         if missing_vars:
             raise ValueError(
                 f"Missing the following PostgreSQL credentials in {env_test_path}: {', '.join(missing_vars)}"
             )
 
         db_name = os.getenv("POSTGRES_DB", "").lower()
         if "test" not in db_name:
             raise RuntimeError(
                 f"Connected to database '{db_name}', which doesn't appear to be a test database"
             )
 
+        # Force reload of settings to pick up new environment variables
+        from importlib import reload
+        import app.core.config
+        reload(app.core.config)
+        from app.core.config import settings as reloaded_settings
+        return reloaded_settings
     else:
         logger.warning(
             f"{env_test_path} not found. Using default environment settings."
         )
-
-    return settings
+        return settings

This addresses the settings reinitialization issue by properly reloading the config module, ensuring the new environment variables are reflected in the settings object.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between aa14f7e and f69f428.

📒 Files selected for processing (1)
  • backend/app/tests/utils/utils.py (2 hunks)
🧰 Additional context used
🪛 Ruff (0.12.2)
backend/app/tests/utils/utils.py

6-6: typing.Type is deprecated, use type instead

(UP035)


13-13: Redefinition of unused load_dotenv from line 8

Remove definition: load_dotenv

(F811)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: checks (3.11.7, 6)

Copy link
Collaborator

@avirajsingh7 avirajsingh7 left a comment

Choose a reason for hiding this comment

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

@AkhileshNegi @nishika26
I'm still not fully clear on how env.test is overriding or changing the value of settings in config.py

@pytest.fixture(scope="session", autouse=True)
def seed_baseline():
# Load test environment to ensure correct bucket name
path = find_dotenv(".env.test")
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will use .env file, if there is no .env.test available.

)

db_name = os.getenv("POSTGRES_DB", "").lower()
if "test" not in db_name:
Copy link
Collaborator

Choose a reason for hiding this comment

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

there is no need to verify that test should be in db_name, if someone is using .env.test they know which db they want to use.

def seed_baseline():
# Load test environment to ensure correct bucket name
path = find_dotenv(".env.test")
load_environment(path)
Copy link
Collaborator

Choose a reason for hiding this comment

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

@AkhileshNegi I have modified seed_baseline function with

Screenshot 2025-08-06 at 5 24 52 PM

And this is my output

app/tests/api/routes/test_api_key.py Loading environment variables for testing...
postgresql+psycopg://postgres:postgres@localhost:5432/aipostgres
postgresql+psycopg://postgres:postgres@localhost:5432/aipostgres

After loading also settings is having old reference of env variables. db should be aipostgres_test.

@AkhileshNegi AkhileshNegi deleted the enhancement/env_test branch October 13, 2025 13:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Testing: Create .env.test File and Override Environment Variables for Testing Testcases Run

4 participants