Skip to content

Commit

Permalink
alligned the implementation with #217
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Sep 25, 2024
1 parent 17645a3 commit 9565042
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 36 deletions.
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ pub mod zookeeper;

/// Re-exported version of `testcontainers` to avoid version conflicts
pub use testcontainers;
use testcontainers::CopyDataSource;

#[cfg(any(feature = "postgres", feature = "mariadb", feature = "mysql"))]
#[cfg_attr(
Expand All @@ -158,17 +159,20 @@ pub trait InitSql {
/// # Example
///
/// ```
/// # #[cfg(feature = "postgres")]
/// # {
/// # use testcontainers_modules::postgres::Postgres;
/// # use testcontainers_modules::InitSql;
/// let postgres_image =
/// Postgres::default().with_init_sql("CREATE EXTENSION IF NOT EXISTS hstore;");
/// let postgres_image = Postgres::default()
/// .with_init_sql("CREATE EXTENSION IF NOT EXISTS hstore;".to_string().into_bytes());
/// # }
/// ```
///
/// ```rust,ignore
/// # use testcontainers_modules::postgres::Postgres;
/// # use testcontainers_modules::rdbms::InitSql;
/// let postgres_image = Postgres::default()
/// .with_init_sql(include_str!("path_to_init.sql"));
/// .with_init_sql(include_str!("path_to_init.sql").to_string().into_bytes());
/// ```
fn with_init_sql(self, init_sql: impl ToString) -> Self;
fn with_init_sql(self, init_sql: impl Into<CopyDataSource>) -> Self;
}
19 changes: 9 additions & 10 deletions src/mariadb/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::Cow;

use testcontainers::{core::WaitFor, CopyToContainer, Image};
use testcontainers::{core::WaitFor, CopyDataSource, CopyToContainer, Image};

const NAME: &str = "mariadb";
const TAG: &str = "11.3";
Expand All @@ -27,7 +27,7 @@ const TAG: &str = "11.3";
/// [`MariaDB docker image`]: https://hub.docker.com/_/mariadb
#[derive(Debug, Default, Clone)]
pub struct Mariadb {
init_sqls: Vec<CopyToContainer>,
copy_to_sources: Vec<CopyToContainer>,
}

impl Image for Mariadb {
Expand Down Expand Up @@ -55,17 +55,16 @@ impl Image for Mariadb {
]
}
fn copy_to_sources(&self) -> impl IntoIterator<Item = &CopyToContainer> {
&self.init_sqls
&self.copy_to_sources
}
}
impl crate::InitSql for Mariadb {
fn with_init_sql(mut self, init_sql: impl ToString) -> Self {
let init_vec = init_sql.to_string().into_bytes();
fn with_init_sql(mut self, init_sql: impl Into<CopyDataSource>) -> Self {
let target = format!(
"/docker-entrypoint-initdb.d/init_{i}.sql",
i = self.init_sqls.len()
i = self.copy_to_sources.len()
);
self.init_sqls.push(CopyToContainer::new(init_vec, target));
self.copy_to_sources.push(CopyToContainer::new(init_sql.into(), target));
self
}
}
Expand All @@ -83,7 +82,7 @@ mod tests {
fn mariadb_with_init_sql() -> Result<(), Box<dyn std::error::Error + 'static>> {
use crate::InitSql;
let node = MariadbImage::default()
.with_init_sql("CREATE TABLE foo (bar varchar(255));")
.with_init_sql("CREATE TABLE foo (bar varchar(255));".to_string().into_bytes())
.start()?;

let connection_string = &format!(
Expand All @@ -93,10 +92,10 @@ mod tests {
);
let mut conn = mysql::Conn::new(mysql::Opts::from_url(connection_string).unwrap()).unwrap();

let rows = conn.query("INSERT INTO foo(bar) VALUES ('blub')").unwrap();
let rows:Vec<String> = conn.query("INSERT INTO foo(bar) VALUES ('blub')").unwrap();
assert_eq!(rows.len(), 0);

let rows = conn.query("SELECT bar FROM foo").unwrap();
let rows:Vec<String> = conn.query("SELECT bar FROM foo").unwrap();
assert_eq!(rows.len(), 1);
Ok(())
}
Expand Down
19 changes: 9 additions & 10 deletions src/mysql/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::Cow;

use testcontainers::{core::WaitFor, CopyToContainer, Image};
use testcontainers::{core::WaitFor, CopyDataSource, CopyToContainer, Image};

const NAME: &str = "mysql";
const TAG: &str = "8.1";
Expand All @@ -27,7 +27,7 @@ const TAG: &str = "8.1";
/// [`MySQL docker image`]: https://hub.docker.com/_/mysql
#[derive(Debug, Default, Clone)]
pub struct Mysql {
init_sqls: Vec<CopyToContainer>,
copy_to_sources: Vec<CopyToContainer>,
}

impl Image for Mysql {
Expand Down Expand Up @@ -55,17 +55,16 @@ impl Image for Mysql {
]
}
fn copy_to_sources(&self) -> impl IntoIterator<Item = &CopyToContainer> {
&self.init_sqls
&self.copy_to_sources
}
}
impl crate::InitSql for Mysql {
fn with_init_sql(mut self, init_sql: impl ToString) -> Self {
let init_vec = init_sql.to_string().into_bytes();
fn with_init_sql(mut self, init_sql: impl Into<CopyDataSource>) -> Self {
let target = format!(
"/docker-entrypoint-initdb.d/init_{i}.sql",
i = self.init_sqls.len()
i = self.copy_to_sources.len()
);
self.init_sqls.push(CopyToContainer::new(init_vec, target));
self.copy_to_sources.push(CopyToContainer::new(init_sql.into(), target));
self
}
}
Expand All @@ -84,7 +83,7 @@ mod tests {
fn mysql_with_init_sql() -> Result<(), Box<dyn std::error::Error + 'static>> {
use crate::InitSql;
let node = crate::mysql::Mysql::default()
.with_init_sql("CREATE TABLE foo (bar varchar(255));")
.with_init_sql("CREATE TABLE foo (bar varchar(255));".to_string().into_bytes())
.start()?;

let connection_string = &format!(
Expand All @@ -94,10 +93,10 @@ mod tests {
);
let mut conn = mysql::Conn::new(mysql::Opts::from_url(connection_string).unwrap()).unwrap();

let rows = conn.query("INSERT INTO foo(bar) VALUES ('blub')").unwrap();
let rows:Vec<String> = conn.query("INSERT INTO foo(bar) VALUES ('blub')").unwrap();
assert_eq!(rows.len(), 0);

let rows = conn.query("SELECT bar FROM foo").unwrap();
let rows:Vec<String> = conn.query("SELECT bar FROM foo").unwrap();
assert_eq!(rows.len(), 1);
Ok(())
}
Expand Down
20 changes: 8 additions & 12 deletions src/postgres/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::{borrow::Cow, collections::HashMap};

use testcontainers::{
core::{Mount, WaitFor},
CopyToContainer, Image,
};
use testcontainers::{core::WaitFor, CopyDataSource, CopyToContainer, Image};

const NAME: &str = "postgres";
const TAG: &str = "11-alpine";
Expand Down Expand Up @@ -33,7 +30,7 @@ const TAG: &str = "11-alpine";
#[derive(Debug, Clone)]
pub struct Postgres {
env_vars: HashMap<String, String>,
init_sqls: Vec<CopyToContainer>,
copy_to_sources: Vec<CopyToContainer>,
}

impl Postgres {
Expand Down Expand Up @@ -67,13 +64,12 @@ impl Postgres {
}
}
impl crate::InitSql for Postgres {
fn with_init_sql(mut self, init_sql: impl ToString) -> Self {
let init_vec = init_sql.to_string().into_bytes();
fn with_init_sql(mut self, init_sql: impl Into<CopyDataSource>) -> Self {
let target = format!(
"/docker-entrypoint-initdb.d/init_{i}.sql",
i = self.init_sqls.len()
i = self.copy_to_sources.len()
);
self.init_sqls.push(CopyToContainer::new(init_vec, target));
self.copy_to_sources.push(CopyToContainer::new(init_sql.into(), target));
self
}
}
Expand All @@ -87,7 +83,7 @@ impl Default for Postgres {

Self {
env_vars,
init_sqls: Vec::new(),
copy_to_sources: Vec::new(),
}
}
}
Expand All @@ -114,7 +110,7 @@ impl Image for Postgres {
&self.env_vars
}
fn copy_to_sources(&self) -> impl IntoIterator<Item = &CopyToContainer> {
&self.init_sqls
&self.copy_to_sources
}
}

Expand Down Expand Up @@ -170,7 +166,7 @@ mod tests {
fn postgres_with_init_sql() -> Result<(), Box<dyn std::error::Error + 'static>> {
use crate::InitSql;
let node = Postgres::default()
.with_init_sql("CREATE TABLE foo (bar varchar(255));")
.with_init_sql("CREATE TABLE foo (bar varchar(255));".to_string().into_bytes())
.start()?;

let connection_string = &format!(
Expand Down

0 comments on commit 9565042

Please sign in to comment.