Skip to content

Run DB tests against SQLite too #2152

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ x509-cert = { version = "0.2.5", features = ["pem"] }
intern = { path = "../intern" }

[dev-dependencies]
uuid = "1.16.0"
uuid = { version = "1.16.0", features = ["v4"] }
67 changes: 51 additions & 16 deletions database/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@ use tokio_postgres::Config;
use crate::pool::postgres::make_client;
use crate::Pool;

enum TestDb {
Postgres {
original_db_url: String,
db_name: String,
},
SQLite,
}

/// Represents a connection to a Postgres database that can be
/// used in integration tests to test logic that interacts with
/// a database.
pub(crate) struct TestContext {
db_name: String,
original_db_url: String,
test_db: TestDb,
// Pre-cached client to avoid creating unnecessary connections in tests
client: Pool,
}

impl TestContext {
async fn new(db_url: &str) -> Self {
async fn new_postgres(db_url: &str) -> Self {
let config: Config = db_url.parse().expect("Cannot parse connection string");

// Create a new database that will be used for this specific test
Expand Down Expand Up @@ -62,8 +69,18 @@ impl TestContext {
let pool = Pool::open(test_db_url.as_str());

Self {
db_name,
original_db_url: db_url.to_string(),
test_db: TestDb::Postgres {
original_db_url: db_url.to_string(),
db_name,
},
client: pool,
}
}

async fn new_sqlite() -> Self {
let pool = Pool::open(":memory:");
Self {
test_db: TestDb::SQLite,
client: pool,
}
}
Expand All @@ -77,25 +94,37 @@ impl TestContext {
// First, we need to stop using the database
drop(self.client);

// Then we need to connect to the default database and drop our test DB
let client = make_client(&self.original_db_url)
.await
.expect("Cannot connect to database");
client
.execute(&format!("DROP DATABASE {}", self.db_name), &[])
.await
.unwrap();
match self.test_db {
TestDb::Postgres {
original_db_url,
db_name,
} => {
// Then we need to connect to the default database and drop our test DB
let client = make_client(&original_db_url)
.await
.expect("Cannot connect to database");
client
.execute(&format!("DROP DATABASE {db_name}"), &[])
.await
.unwrap();
}
TestDb::SQLite => {}
}
}
}

/// Runs a test against an actual database.
/// Checks both Postgres and SQLite.
pub(crate) async fn run_db_test<F, Fut>(f: F)
where
F: FnOnce(TestContext) -> Fut,
F: Fn(TestContext) -> Fut,
Fut: Future<Output = anyhow::Result<TestContext>>,
{
// Postgres
if let Ok(db_url) = std::env::var("TEST_DB_URL") {
let ctx = TestContext::new(&db_url).await;
let ctx = f(ctx).await.expect("Test failed");
eprintln!("Running test with Postgres");
let ctx = TestContext::new_postgres(&db_url).await;
let ctx = f(ctx).await.expect("Postgres test failed");
ctx.finish().await;
} else {
// The github CI does not yet support running containers on Windows,
Expand All @@ -109,4 +138,10 @@ where
);
}
}

// SQLite
eprintln!("Running test with SQLite");
let ctx = TestContext::new_sqlite().await;
let ctx = f(ctx).await.expect("SQLite test failed");
ctx.finish().await;
}
Loading