Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add documentation for postgres image #51

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 21 additions & 29 deletions src/postgres/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ use testcontainers::{core::WaitFor, Image};
const NAME: &str = "postgres";
const TAG: &str = "11-alpine";

/// Module to work with [`Postgres`] inside of tests.
///
/// Starts an instance of Postgres.
/// This module is based on the official [`Postgres docker image`].
///
/// # Example
/// ```
/// use testcontainers::clients;
/// use testcontainers_modules::postgres;
///
/// let docker = clients::Cli::default();
/// let postgres_instance = docker.run(postgres::Postgres::default());
///
/// let connection_string = format!(
/// "postgres://postgres:postgres@127.0.0.1:{}/postgres",
/// postgres_instance.get_host_port_ipv4(5432)
/// );
/// ```
///
/// [`Postgres`]: https://www.postgresql.org/
/// [`Postgres docker image`]: https://hub.docker.com/_/postgres
#[derive(Debug)]
pub struct Postgres {
env_vars: HashMap<String, String>,
Expand Down Expand Up @@ -68,27 +89,6 @@ mod tests {
assert_eq!(first_column, 2);
}

#[test]
fn postgres_one_plus_one_with_custom_mapped_port() {
let _ = pretty_env_logger::try_init();
let free_local_port = free_local_port();

let docker = clients::Cli::default();
let image =
RunnableImage::from(PostgresImage::default()).with_mapped_port((free_local_port, 5432));
let _node = docker.run(image);

let mut conn = postgres::Client::connect(
&format!("postgres://postgres:postgres@localhost:{free_local_port}/postgres",),
postgres::NoTls,
)
.unwrap();
let rows = conn.query("SELECT 1+1 AS result;", &[]).unwrap();

assert_eq!(rows.len(), 1);
assert_eq!(rows[0].get::<_, i32>("result"), 2);
}

#[test]
fn postgres_custom_version() {
let docker = clients::Cli::default();
Expand All @@ -108,12 +108,4 @@ mod tests {
let first_column: String = first_row.get(0);
assert!(first_column.contains("13"));
}

#[must_use]
fn free_local_port() -> u16 {
std::net::TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, 0))
.and_then(|listener| listener.local_addr())
.map(|addr| addr.port())
.expect("free port not found")
}
}
2 changes: 1 addition & 1 deletion src/rabbitmq/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const TAG: &str = "3.8.22-management";
/// Starts an instance of RabbitMQ with the [`management-plugin`] started by default,
/// so you are able to use the [`RabbitMQ Management HTTP API`] to manage the configuration if the started [`RabbitMQ`] instance at test runtime.
///
/// This module is based on the officlal [`RabbitMQ docker image`].
/// This module is based on the official [`RabbitMQ docker image`].
///
/// # Example
/// ```
Expand Down
Loading