Skip to content

Add external Postgres option for tests#121

Open
leynos wants to merge 1 commit intomainfrom
rxe870-codex/modify-setup_postgres-to-check-postgres_test_url
Open

Add external Postgres option for tests#121
leynos wants to merge 1 commit intomainfrom
rxe870-codex/modify-setup_postgres-to-check-postgres_test_url

Conversation

@leynos
Copy link
Copy Markdown
Owner

@leynos leynos commented Jun 13, 2025

Summary

  • allow tests to reuse a running PostgreSQL instance via POSTGRES_TEST_URL
  • avoid creating a temporary directory when using that external DB
  • make the temp directory optional inside TestServer
  • document cleaning the reused database between test runs
  • enforce mutually exclusive back-end features and add a regression test
  • remove unused cfg-if dependency and share launch helper
  • refine regression test setup to avoid unsafe blocks
  • extracted helpers to reduce nesting in Postgres setup
  • clarify in the agent instructions that nixie is a standalone CLI
  • document start as a wrapper around start_with_setup
  • clarify POSTGRES_TEST_URL usage in README

Testing

  • cargo fmt --all
  • cargo clippy -- -D warnings
  • cargo test
  • npx -y markdownlint-cli2 '**/*.md' '#node_modules'
  • nixie docs/chat-schema.md docs/news-schema.md docs/file-sharing-design.md docs/fuzzing.md README.md

https://chatgpt.com/codex/tasks/task_e_684abdf1fd7c83228ebcfd3622ab5c44

Summary by Sourcery

Enable tests to optionally connect to an external PostgreSQL instance, refactor TestServer setup for both embedded and external databases, update documentation, and add a test to validate the new behavior.

New Features:

  • Allow tests to reuse an external PostgreSQL instance via the POSTGRES_TEST_URL environment variable.

Enhancements:

  • Refactor TestServer to separate embedded and external Postgres setup and introduce a shared spawn_server helper.
  • Make the temporary directory in TestServer optional and add a uses_embedded_postgres method to detect embedded DB usage.

Build:

  • Add temp-env as a dev dependency to support test environment variable handling.

Documentation:

  • Document POSTGRES_TEST_URL usage and cleanup requirements in the README.
  • Clarify that nixie is a standalone CLI in AGENTS.md.

Tests:

  • Add a regression test to ensure POSTGRES_TEST_URL is honored and no embedded server is started when set.

Summary by CodeRabbit

  • Documentation
    • Clarified instructions for validating Markdown Mermaid diagrams and updated README with guidance on using an external PostgreSQL database for testing.
  • New Features
    • Added support for using an external PostgreSQL database during tests by setting an environment variable, with automatic fallback to embedded PostgreSQL if not provided.
  • Bug Fixes
    • Improved feature validation to ensure only one of SQLite or PostgreSQL is enabled during compilation.
  • Tests
    • Added a test to verify that an external PostgreSQL URL is correctly used when specified.

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Jun 13, 2025

Reviewer's Guide

Refactors the test utility to conditionally reuse an external PostgreSQL instance via an environment variable, streamline server startup with extracted helpers and optional temp directories, and updates documentation plus tests to cover the new flow.

Sequence Diagram for Test Server Initialization Logic

sequenceDiagram
    actor Developer
    participant TestRunner
    participant TestServer
    participant ExternalPostgres
    participant EmbeddedPostgres

    Developer->>TestRunner: Execute tests
    TestRunner->>TestServer: Setup(POSTGRES_TEST_URL_value)
    alt POSTGRES_TEST_URL_value is provided
        TestServer->>TestServer: Initialize with external DB (no TestServer temp dir for DB)
        TestServer->>ExternalPostgres: Connect(POSTGRES_TEST_URL_value)
        ExternalPostgres-->>TestServer: Connection Success
    else POSTGRES_TEST_URL_value is null
        TestServer->>TestServer: Initialize with embedded DB (manages temp dir)
        TestServer->>EmbeddedPostgres: Start()
        EmbeddedPostgres-->>TestServer: Instance Ready
    end
    TestServer-->>TestRunner: Setup Complete
    TestRunner->>Developer: Test results
Loading

Updated Class Diagram for TestServer and Helpers

classDiagram
    class TestServer {
        - postgres_test_url_override: Option<String>
        - manages_own_temp_directory: bool
        - database_connection: DatabaseConnection
        + new(postgres_test_url_override: Option<String>)
        + start()
        - setup_database()
        - initialize_embedded_postgres_instance()
        - connect_to_external_postgres(url: String)
    }
    class PostgresSetupHelper {
        + static configure_database_connection(url_option: Option<String>, requires_temp_dir_management: Pointer<bool>): DatabaseConnection
        - start_new_embedded_instance_with_temp_dir(): DatabaseConnection
        - establish_connection_to_external_server(url: String): DatabaseConnection
    }
    TestServer ..> PostgresSetupHelper : uses
Loading

File-Level Changes

Change Details Files
Enable external PostgreSQL reuse in tests
  • Extract external_postgres_url helper
  • Update setup_postgres to handle external URL and return Option
  • Expose setup_postgres_for_test for external-use
  • Add uses_embedded_postgres method on TestServer
test-util/src/lib.rs
Simplify TestServer startup and database setup
  • Make TempDir optional instead of mandatory
  • Introduce spawn_server to bind ports and launch processes
  • Refactor start_with_setup into separate sqlite and postgres branches
  • Add start() wrapper delegating to start_with_setup
test-util/src/lib.rs
Update documentation for external DB and CLI usage
  • Document POSTGRES_TEST_URL behavior and cleanup requirement in README
  • Clarify nixie as standalone CLI in AGENTS.md
  • Note start() is a wrapper around start_with_setup
README.md
AGENTS.md
Add regression test and adjust dependencies
  • Add tests/postgres_env.rs to verify external Postgres path
  • Add temp-env dev-dependency for test convenience
  • Remove unused cfg-if dependency and share launch helper
tests/postgres_env.rs
Cargo.toml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 13, 2025

Walkthrough

This update enhances the test infrastructure to support using an external PostgreSQL database via an environment variable, refines feature validation for database backends, and clarifies documentation for both Markdown diagram validation and PostgreSQL test setup. It also adds a new test to verify the external PostgreSQL configuration.

Changes

File(s) Change Summary
AGENTS.md, README.md Updated documentation: clarified Markdown Mermaid diagram validation instructions and explained PostgreSQL test setup options.
Cargo.toml Added temp-env as a development dependency.
test-util/src/lib.rs Enforced feature validation, refactored test server setup for SQLite/PostgreSQL, supported external PostgreSQL URLs, added helpers.
tests/postgres_env.rs Added a test to verify that an external PostgreSQL URL is used when specified.

Sequence Diagram(s)

sequenceDiagram
    participant Tester
    participant Env
    participant TestUtil
    participant EmbeddedPG

    Tester->>Env: Check POSTGRES_TEST_URL
    alt POSTGRES_TEST_URL is set
        Env-->>TestUtil: Provide external DB URL
        TestUtil-->>Tester: Use external DB URL, no embedded PG
    else
        TestUtil->>EmbeddedPG: Start embedded PostgreSQL
        EmbeddedPG-->>TestUtil: Provide embedded DB URL
        TestUtil-->>Tester: Use embedded DB URL, embedded PG active
    end
Loading

Possibly related PRs

  • leynos/mxd#114: Modifies setup_postgres and test server setup to support using an external PostgreSQL instance via the POSTGRES_TEST_URL environment variable.
  • leynos/mxd#94: Introduces the db_url helper method for TestServer, which is further extended in this PR.
  • leynos/mxd#92: Builds the initial PostgreSQL test infrastructure that this PR extends with environment variable support and improved validation.

Poem

Hopping through tests with a twitch of my nose,
Now Postgres can live where the user best chose!
If your env var is set, we’ll use your own base,
Or else spin up Postgres in a temporary place.
With docs now more clear, and the code hopping neat—
This rabbit declares: our test suite’s complete! 🐇

✨ Finishing Touches
  • 📝 Generate Docstrings

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

Copy link
Copy Markdown

@codescene-delta-analysis codescene-delta-analysis Bot left a comment

Choose a reason for hiding this comment

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

Code Health Improved (1 files improve in Code Health)

Gates Passed
6 Quality Gates Passed

See analysis details in CodeScene

View Improvements
File Code Health Impact Categories Improved
lib.rs 9.10 → 9.69 Code Duplication, String Heavy Function Arguments

Quality Gate Profile: Pay Down Tech Debt
Want more control? Customize Code Health rules or catch issues early with our IDE extension and CLI tool.

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey @leynos - I've reviewed your changes - here's some feedback:

  • The two branches in TestServer::start_with_setup share a lot of logic and use early returns; consider unifying SQLite and Postgres setup flows to reduce duplication and nesting.
  • Binding and dropping a TcpListener to pick a port may race if another process grabs the port; for more robust tests you could pass the listener into the child process or let the server pick an OS-assigned port directly.
  • external_postgres_url only checks for a non-empty environment variable; consider adding basic URL validation or user feedback to catch malformed connection strings early.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The two branches in TestServer::start_with_setup share a lot of logic and use early returns; consider unifying SQLite and Postgres setup flows to reduce duplication and nesting.
- Binding and dropping a TcpListener to pick a port may race if another process grabs the port; for more robust tests you could pass the listener into the child process or let the server pick an OS-assigned port directly.
- external_postgres_url only checks for a non-empty environment variable; consider adding basic URL validation or user feedback to catch malformed connection strings early.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown
Contributor

@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 (4)
README.md (1)

87-91: Minor punctuation nit

Consider inserting a comma after “between runs” for readability:

-database must be emptied between runs as the suite assumes a pristine schema.
+database must be emptied between runs, as the suite assumes a pristine schema.
🧰 Tools
🪛 LanguageTool

[uncategorized] ~90-~90: Possible missing comma found.
Context: ...renced database must be emptied between runs as the suite assumes a pristine schema....

(AI_HYDRA_LEO_MISSING_COMMA)

test-util/src/lib.rs (3)

45-50: Validate URL early

external_postgres_url only checks for non-empty text. Parsing with url::Url::parse would fail fast on obviously invalid strings and give clearer error messages.

-use std::env::var_os("POSTGRES_TEST_URL").and_then(|raw| {
-    let url = raw.to_string_lossy();
-    (!url.trim().is_empty()).then(|| url.into_owned())
-})
+std::env::var_os("POSTGRES_TEST_URL").and_then(|raw| {
+    let url = raw.to_string_lossy();
+    if url.trim().is_empty() {
+        return None;
+    }
+    url::Url::parse(&url).ok().map(|_| url.into_owned())
+})

53-68: Runtime per embedded instance may be heavy

Spawning a fresh Tokio runtime for every test that needs an embedded Postgres incurs noticeable overhead. Consider:

• Passing in an existing Handle, or
• Using tokio::runtime::Handle::current() when already inside a runtime.

This will speed up large test suites.


139-150: Re-compiling the binary for each test

spawn_server invokes cargo run repeatedly, which can dominate test time. If feasible, build once with cargo build in a #[ctor] or test-setup script and then run the produced executable for each test.

This avoids redundant compilations while preserving the random-port logic.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d3ffb39 and 7aadcc9.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (5)
  • AGENTS.md (1 hunks)
  • Cargo.toml (1 hunks)
  • README.md (1 hunks)
  • test-util/src/lib.rs (7 hunks)
  • tests/postgres_env.rs (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/postgres_env.rs (1)
test-util/src/lib.rs (1)
  • setup_postgres_for_test (86-93)
🪛 LanguageTool
AGENTS.md

[misspelling] ~125-~125: Use “a” instead of ‘an’ if the following word doesn’t start with a vowel sound, e.g. ‘a sentence’, ‘a university’.
Context: ... is a standalone command-line tool, not an npm package, so invoke it directly ra...

(EN_A_VS_AN)

README.md

[uncategorized] ~90-~90: Possible missing comma found.
Context: ...renced database must be emptied between runs as the suite assumes a pristine schema....

(AI_HYDRA_LEO_MISSING_COMMA)

🔇 Additional comments (5)
Cargo.toml (1)

61-61: Addition of temp-env looks good

The crate is lightweight and scoped to tests only; no concerns.

AGENTS.md (1)

124-126: No action required

The clarification improves precision; wording is already correct (“an npm” is fine because the abbreviation begins with a vowel sound).

🧰 Tools
🪛 LanguageTool

[misspelling] ~125-~125: Use “a” instead of ‘an’ if the following word doesn’t start with a vowel sound, e.g. ‘a sentence’, ‘a university’.
Context: ... is a standalone command-line tool, not an npm package, so invoke it directly ra...

(EN_A_VS_AN)

tests/postgres_env.rs (1)

8-15: Solid regression test

The test correctly exercises the environment-variable path and ensures no embedded server starts. Nice coverage.

test-util/src/lib.rs (2)

11-16: Compile-time feature guard is spot-on

Preventing dual-backend builds at compile time avoids an entire class of runtime errors.


233-237: Helpful visibility

uses_embedded_postgres is a handy diagnostic; good foresight.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant