|
| 1 | +use crate::db; |
| 2 | +use crate::db::make_client; |
| 3 | +use crate::db::notifications::record_username; |
| 4 | +use std::future::Future; |
| 5 | +use tokio_postgres::Config; |
| 6 | + |
| 7 | +/// Represents a connection to a Postgres database that can be |
| 8 | +/// used in integration tests to test logic that interacts with |
| 9 | +/// a database. |
| 10 | +pub struct TestContext { |
| 11 | + client: tokio_postgres::Client, |
| 12 | + db_name: String, |
| 13 | + original_db_url: String, |
| 14 | + conn_handle: tokio::task::JoinHandle<()>, |
| 15 | +} |
| 16 | + |
| 17 | +impl TestContext { |
| 18 | + async fn new(db_url: &str) -> Self { |
| 19 | + let mut config: Config = db_url.parse().expect("Cannot parse connection string"); |
| 20 | + |
| 21 | + // Create a new database that will be used for this specific test |
| 22 | + let client = make_client(&db_url) |
| 23 | + .await |
| 24 | + .expect("Cannot connect to database"); |
| 25 | + let db_name = format!("db{}", uuid::Uuid::new_v4().to_string().replace("-", "")); |
| 26 | + client |
| 27 | + .execute(&format!("CREATE DATABASE {db_name}"), &[]) |
| 28 | + .await |
| 29 | + .expect("Cannot create database"); |
| 30 | + drop(client); |
| 31 | + |
| 32 | + // We need to connect to the database against, because Postgres doesn't allow |
| 33 | + // changing the active database mid-connection. |
| 34 | + config.dbname(&db_name); |
| 35 | + let (mut client, connection) = config |
| 36 | + .connect(tokio_postgres::NoTls) |
| 37 | + .await |
| 38 | + .expect("Cannot connect to the newly created database"); |
| 39 | + let conn_handle = tokio::spawn(async move { |
| 40 | + connection.await.unwrap(); |
| 41 | + }); |
| 42 | + |
| 43 | + db::run_migrations(&mut client) |
| 44 | + .await |
| 45 | + .expect("Cannot run database migrations"); |
| 46 | + Self { |
| 47 | + client, |
| 48 | + db_name, |
| 49 | + original_db_url: db_url.to_string(), |
| 50 | + conn_handle, |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + pub fn db_client(&self) -> &tokio_postgres::Client { |
| 55 | + &self.client |
| 56 | + } |
| 57 | + |
| 58 | + pub async fn add_user(&self, name: &str, id: u64) { |
| 59 | + record_username(&self.client, id, name) |
| 60 | + .await |
| 61 | + .expect("Cannot create user"); |
| 62 | + } |
| 63 | + |
| 64 | + async fn finish(self) { |
| 65 | + // Cleanup the test database |
| 66 | + // First, we need to stop using the database |
| 67 | + drop(self.client); |
| 68 | + self.conn_handle.await.unwrap(); |
| 69 | + |
| 70 | + // Then we need to connect to the default database and drop our test DB |
| 71 | + let client = make_client(&self.original_db_url) |
| 72 | + .await |
| 73 | + .expect("Cannot connect to database"); |
| 74 | + client |
| 75 | + .execute(&format!("DROP DATABASE {}", self.db_name), &[]) |
| 76 | + .await |
| 77 | + .unwrap(); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +pub async fn run_test<F, Fut>(f: F) |
| 82 | +where |
| 83 | + F: FnOnce(TestContext) -> Fut, |
| 84 | + Fut: Future<Output = anyhow::Result<TestContext>>, |
| 85 | +{ |
| 86 | + if let Ok(db_url) = std::env::var("TEST_DB_URL") { |
| 87 | + let ctx = TestContext::new(&db_url).await; |
| 88 | + let ctx = f(ctx).await.expect("Test failed"); |
| 89 | + ctx.finish().await; |
| 90 | + } else { |
| 91 | + eprintln!("Skipping test because TEST_DB_URL was not passed"); |
| 92 | + } |
| 93 | +} |
0 commit comments