#postgresql #ready #connection #testing #accept #url

with_postgres_ready

Helper for running tests with a Postgres database in a Docker container that is ready to accept connections

2 releases

0.1.1 Jul 8, 2023
0.1.0 Jul 8, 2023

#2320 in Database interfaces

MIT/Apache

11KB
175 lines

with_postgres_ready

with_postgres_ready makes it easy to write tests that relies on a postgres database being ready to accept connections. It does this by starting a Docker container with postgres, polling the database until it is ready, and then executing the block.

Examples

To get a connection url with the default configuration:

use with_postgres_ready::*;

#[test_log::test]
fn it_can_use_defaults() {
    with_postgres_ready(|url| async move {
        // Connect to the database using the url.
    });
}

To get more control, use the Runner builder:

use with_postgres_ready::*;

#[test_log::test]
fn it_can_use_custom_connection_timeout() {
    Runner::new().connection_timeout(Duration::from_secs(5)).run(|url| async move {
        // Connect to the database using the url.
    });
}

lib.rs:

with_postgres_ready makes it easy to write tests that relies on a postgres database being ready to accept connections. It does this by starting a Docker container with postgres, polling the database until it is ready, and then executing the block.

Examples

To get a connection url with the default configuration:

use with_postgres_ready::*;

#[test_log::test]
fn it_can_use_defaults() {
    with_postgres_ready(|url| async move {
        // Connect to the database using the url.
    });
}

To get more control, use the Runner builder:

use with_postgres_ready::*;

#[test_log::test]
fn it_can_use_custom_connection_timeout() {
    Runner::new().connection_timeout(Duration::from_secs(5)).run(|url| async move {
        // Connect to the database using the url.
    });
}

Dependencies

~12–23MB
~346K SLoC