Skip to content

Commit

Permalink
Added Postgres on CI
Browse files Browse the repository at this point in the history
Enabled tests related to postgres on CI
  • Loading branch information
MichaelWats0n committed Aug 4, 2022
1 parent 6fa7aa6 commit 646f1d0
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 90 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/general.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ jobs:
test:
name: Test
runs-on: ubuntu-latest
timeout-minutes: 30
services:
postgres:
image: postgres
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: tests
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
Expand All @@ -41,6 +55,8 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: test
env:
DATABASE_URL_TEST: postgresql://postgres:postgres@localhost:5432/tests

fmt:
name: Rustfmt
Expand Down
32 changes: 8 additions & 24 deletions core/src/database/events/recorder/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,13 @@ mod tests {
use chrono::Utc;
use mmb_database::impl_event;
use mmb_database::postgres_db::events::{Event, InsertEvent, TableName};
use mmb_database::postgres_db::PgPool;
use mmb_database::postgres_db::tests::{get_database_url, PgPoolMutex};
use mmb_utils::DateTime;
use scopeguard::defer;
use serde::{Deserialize, Serialize};
use std::fs;
use tokio_postgres::NoTls;

const DATABASE_URL: &str = "postgres://postgres:postgres@localhost/tests";
const TABLE_NAME: &str = "fallback_events";

#[derive(Debug, Serialize, Deserialize)]
Expand All @@ -248,18 +247,12 @@ mod tests {
TestEvent { date: Utc::now() }
}

async fn init_test() -> PgPool {
let pool = PgPool::create(DATABASE_URL, 2)
.await
.expect("connect to db");

let connection = pool.get_connection_expected().await;

async fn init_test() -> PgPoolMutex {
let pool_mutex = PgPoolMutex::create(&get_database_url(), 1).await;
let connection = pool_mutex.pool.get_connection_expected().await;
recreate_table(&connection).await;

drop(connection);

pool
pool_mutex
}

async fn recreate_table<'a>(
Expand Down Expand Up @@ -327,18 +320,9 @@ mod tests {
pretty_assertions::assert_eq!(file_format, expected);
}

#[ignore = "need postgres initialized for tests"]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn restore_postponed_events_in_db() {
// arrange
let pool = init_test().await;
defer! {
let pool = pool.clone();
let _ = tokio::spawn(async move {
let connection = pool.get_connection_expected().await;
connection.execute(&format!("DROP TABLE IF EXISTS {TABLE_NAME}"), &[]).await.expect("drop table");
});
};
let pool_mutex = init_test().await;

let fallback = EventRecorderFallback::new(None).expect("in test");
defer! {
Expand Down Expand Up @@ -374,11 +358,11 @@ mod tests {
.expect("in test");

fallback
.try_restore_to_db_postponed_events(&pool, &file_names)
.try_restore_to_db_postponed_events(&pool_mutex.pool, &file_names)
.await;

// check saved event to db
let connection = pool.get_connection_expected().await;
let connection = pool_mutex.pool.get_connection_expected().await;
let rows = connection
.query(&format!("SELECT * FROM {TABLE_NAME}"), &[])
.await
Expand Down
63 changes: 26 additions & 37 deletions core/src/database/events/recorder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,11 @@ mod tests {
use crate::infrastructure::init_lifetime_manager;
use mmb_database::impl_event;
use mmb_database::postgres_db::events::TableName;
use mmb_database::postgres_db::tests::{get_database_url, PgPoolMutex};
use serde::{Deserialize, Serialize};
use std::time::{Duration, Instant};
use tokio::time::sleep;
use tokio_postgres::{Client, NoTls};

const DATABASE_URL: &str = "postgres://postgres:postgres@localhost/tests";
const TABLE_NAME: &str = "persons";

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -302,29 +301,23 @@ mod tests {
phone_numbers: Vec<String>,
}

async fn init_test() -> Client {
async fn init_test() -> PgPoolMutex {
init_lifetime_manager();

let (client, connection) = tokio_postgres::connect(DATABASE_URL, NoTls)
let pool_mutex = PgPoolMutex::create(&get_database_url(), 1).await;
let connection = pool_mutex.pool.get_connection_expected().await;
connection
.batch_execute(
&include_str!(
"../../../../../mmb_database/src/postgres_db/sql/create_or_truncate_table.sql"
)
.replace("TABLE_NAME", TABLE_NAME),
)
.await
.expect("connect to DB in test");

tokio::spawn(async move {
if let Err(e) = connection.await {
eprintln!("connection error: {}", e);
}
});
.expect("TRUNCATE persons");

truncate_table(&client).await;

client
}

async fn truncate_table(client: &Client) {
let _ = client
.execute(&format!("truncate table {TABLE_NAME}"), &[])
.await
.expect("truncate persons");
drop(connection);
pool_mutex
}

impl_event!(Person, TABLE_NAME);
Expand All @@ -342,13 +335,13 @@ mod tests {
}
}

#[ignore = "need postgres initialized for tests"]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn save_1_event() {
let client = init_test().await;
let pool_mutex = init_test().await;
let connection = pool_mutex.pool.get_connection_expected().await;

let event_recorder = EventRecorder::start(Some(DbSettings {
database_url: DATABASE_URL.to_string(),
database_url: get_database_url(),
postponed_events_dir: None,
}))
.await
Expand All @@ -359,50 +352,48 @@ mod tests {

sleep(Duration::from_millis(1_500)).await;

let rows = client
let rows = connection
.query("select * from persons", &[])
.await
.expect("select persons in test");

assert_eq!(rows.len(), 1);
}

#[ignore = "need postgres initialized for tests"]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn not_save_1_event_without_db_initialization() {
let client = init_test().await;
let pool_mutex = init_test().await;
let connection = pool_mutex.pool.get_connection_expected().await;

// arrange
let person = test_person();

let database_url = None; // database_url is not initialized

// act
let event_recorder = EventRecorder::start(database_url).await.expect("in test");
let event_recorder = EventRecorder::start(None).await.expect("in test");

event_recorder.save(person).expect("in test");

sleep(Duration::from_secs(2)).await;

// assert
let rows = client
let rows = connection
.query("select * from persons", &[])
.await
.expect("select persons in test");

assert_eq!(rows.len(), 0);
}

#[ignore = "need postgres initialized for tests"]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn simple_flush_and_stop() {
let client = init_test().await;
let pool_mutex = init_test().await;
let connection = pool_mutex.pool.get_connection_expected().await;

// arrange
let person = test_person();

let db_settings = DbSettings {
database_url: DATABASE_URL.to_string(),
database_url: get_database_url(),
postponed_events_dir: None,
};

Expand All @@ -427,13 +418,11 @@ mod tests {
"expected fast execution ({saving_event_time:?} < 1sec)"
);

let rows = client
let rows = connection
.query("select * from persons", &[])
.await
.expect("select persons in test");

assert_eq!(rows.len(), 1);

truncate_table(&client).await;
}
}
4 changes: 2 additions & 2 deletions mmb_database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ chrono = { version = "0.4", features = ["serde"] }
futures = "0.3"
itertools = "0.10"
log = "0.4"
once_cell = "1.8"
parking_lot = { version = "0.12", features = ["serde"]}
tokio-postgres = { version = "0.7", features = ["with-serde_json-1", "with-chrono-0_4"] }
bb8-postgres = { version = "0.8", features = ["with-serde_json-1", "with-chrono-0_4"] }
serde = { version = "1", features = ["derive"] }
Expand All @@ -21,7 +23,5 @@ sqlx = { version = "0.5.13", features = [ "chrono", "macros", "postgres", "runti

[dev-dependencies]
ntest = "0.8"
once_cell = "1.8"
parking_lot = { version = "0.12", features = ["serde"]}
scopeguard = "1.1"
tokio = { version = "1", features = ["macros", "rt-multi-thread"]}
30 changes: 11 additions & 19 deletions mmb_database/src/postgres_db/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::fmt::{Display, Formatter};
use tokio_postgres::binary_copy::BinaryCopyInWriter;
use tokio_postgres::types::Type;
use tokio_postgres::{NoTls, Statement};

pub type TableName = &'static str;
pub type TableNameRef<'a> = &'a str;

Expand Down Expand Up @@ -165,19 +164,14 @@ pub async fn save_events_one_by_one(
#[cfg(test)]
mod tests {
use crate::postgres_db::events::{save_events_batch, save_events_one_by_one, InsertEvent};
use crate::postgres_db::PgPool;
use crate::postgres_db::tests::{get_database_url, PgPoolMutex};
use serde_json::json;

const DATABASE_URL: &str = "postgres://postgres:postgres@localhost/tests";
const TABLE_NAME: &str = "persons";

async fn init_test() -> PgPool {
let pool = PgPool::create(DATABASE_URL, 2)
.await
.expect("connect to db");

let connection = pool.get_connection_expected().await;

async fn init_test() -> PgPoolMutex {
let pool_mutex = PgPoolMutex::create(&get_database_url(), 1).await;
let connection = pool_mutex.pool.get_connection_expected().await;
connection
.batch_execute(
&include_str!("./sql/create_or_truncate_table.sql")
Expand All @@ -187,12 +181,10 @@ mod tests {
.expect("TRUNCATE persons");

drop(connection);

pool
pool_mutex
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[ignore = "need postgres initialized for tests"]
async fn save_batch_events_1_item() {
let pool = init_test().await;

Expand All @@ -207,12 +199,12 @@ mod tests {
};

// act
save_events_batch(&pool, TABLE_NAME, &[item])
save_events_batch(&pool.pool, TABLE_NAME, &[item])
.await
.expect("in test");

// assert
let connection = pool.get_connection_expected().await;
let connection = pool.pool.get_connection_expected().await;

let rows = connection
.query(&format!("SELECT * FROM {TABLE_NAME}"), &[])
Expand All @@ -228,9 +220,8 @@ mod tests {
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[ignore = "need postgres initialized for tests"]
async fn save_one_by_one_events_1_item() {
let pool = init_test().await;
let pool_mutex = init_test().await;

// arrange
let expected_json = json!({
Expand All @@ -243,13 +234,14 @@ mod tests {
};

// act
let (results, failed_events) = save_events_one_by_one(&pool, TABLE_NAME, vec![item]).await;
let (results, failed_events) =
save_events_one_by_one(&pool_mutex.pool, TABLE_NAME, vec![item]).await;
results.expect("in test");

// assert
assert_eq!(failed_events.len(), 0, "there are failed saving events");

let connection = pool.get_connection_expected().await;
let connection = pool_mutex.pool.get_connection_expected().await;

let rows = connection
.query(&format!("SELECT * FROM {TABLE_NAME}"), &[])
Expand Down
8 changes: 4 additions & 4 deletions mmb_database/src/postgres_db/migrator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async fn create_connection_pool(
mod tests {
use super::apply_migrations;
use crate::postgres_db::migrator::create_connection_pool;
use crate::postgres_db::tests::get_database_url;
use itertools::Itertools;
use ntest::timeout;
use sqlx::{Pool, Postgres};
Expand All @@ -73,7 +74,6 @@ mod tests {
const TABLE_NAME1: &str = "test_mig1";
const TABLE_NAME2: &str = "test_mig2";

#[ignore("needed db initialization")]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[timeout(20_000)]
async fn test_apply_undo_migrations() {
Expand All @@ -89,11 +89,11 @@ mod tests {
.map(|p| sql_dir.clone().join(p))
.collect_vec();

apply_migrations(DATABASE_URL, sources)
apply_migrations(&get_database_url(), sources)
.await
.expect("failed apply_migrations in test");

let pool = create_connection_pool(DATABASE_URL, 2)
let pool = create_connection_pool(&get_database_url(), 2)
.await
.expect("failed create_connection_pool in test");

Expand Down Expand Up @@ -123,7 +123,7 @@ mod tests {
}

async fn init_test() {
let pool = create_connection_pool(DATABASE_URL, 2)
let pool = create_connection_pool(&get_database_url(), 2)
.await
.expect("failed create_connection_pool in test");

Expand Down
Loading

0 comments on commit 646f1d0

Please sign in to comment.