|
| 1 | +use std::future::Future; |
| 2 | +use tokio_postgres::config::Host; |
| 3 | +use tokio_postgres::Config; |
| 4 | + |
| 5 | +use crate::pool::postgres::make_client; |
| 6 | +use crate::Pool; |
| 7 | + |
| 8 | +/// Represents a connection to a Postgres database that can be |
| 9 | +/// used in integration tests to test logic that interacts with |
| 10 | +/// a database. |
| 11 | +pub(crate) struct TestContext { |
| 12 | + db_name: String, |
| 13 | + original_db_url: String, |
| 14 | + // Pre-cached client to avoid creating unnecessary connections in tests |
| 15 | + client: Pool, |
| 16 | +} |
| 17 | + |
| 18 | +impl TestContext { |
| 19 | + async fn new(db_url: &str) -> Self { |
| 20 | + let config: Config = db_url.parse().expect("Cannot parse connection string"); |
| 21 | + |
| 22 | + // Create a new database that will be used for this specific test |
| 23 | + let client = make_client(&db_url) |
| 24 | + .await |
| 25 | + .expect("Cannot connect to database"); |
| 26 | + let db_name = format!("db{}", uuid::Uuid::new_v4().to_string().replace("-", "")); |
| 27 | + client |
| 28 | + .execute(&format!("CREATE DATABASE {db_name}"), &[]) |
| 29 | + .await |
| 30 | + .expect("Cannot create database"); |
| 31 | + drop(client); |
| 32 | + |
| 33 | + // We need to connect to the database against, because Postgres doesn't allow |
| 34 | + // changing the active database mid-connection. |
| 35 | + // There does not seem to be a way to turn the config back into a connection |
| 36 | + // string, so construct it manually. |
| 37 | + let test_db_url = format!( |
| 38 | + "postgresql://{}:{}@{}:{}/{}", |
| 39 | + config.get_user().unwrap(), |
| 40 | + String::from_utf8(config.get_password().unwrap().to_vec()).unwrap(), |
| 41 | + match &config.get_hosts()[0] { |
| 42 | + Host::Tcp(host) => host, |
| 43 | + Host::Unix(_) => |
| 44 | + panic!("Unix sockets in Postgres connection string are not supported"), |
| 45 | + }, |
| 46 | + &config.get_ports()[0], |
| 47 | + db_name |
| 48 | + ); |
| 49 | + let pool = Pool::open(test_db_url.as_str()); |
| 50 | + |
| 51 | + Self { |
| 52 | + db_name, |
| 53 | + original_db_url: db_url.to_string(), |
| 54 | + client: pool, |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + pub(crate) fn db_client(&self) -> &Pool { |
| 59 | + &self.client |
| 60 | + } |
| 61 | + |
| 62 | + async fn finish(self) { |
| 63 | + // Cleanup the test database |
| 64 | + // First, we need to stop using the database |
| 65 | + drop(self.client); |
| 66 | + |
| 67 | + // Then we need to connect to the default database and drop our test DB |
| 68 | + let client = make_client(&self.original_db_url) |
| 69 | + .await |
| 70 | + .expect("Cannot connect to database"); |
| 71 | + client |
| 72 | + .execute(&format!("DROP DATABASE {}", self.db_name), &[]) |
| 73 | + .await |
| 74 | + .unwrap(); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +pub(crate) async fn run_db_test<F, Fut>(f: F) |
| 79 | +where |
| 80 | + F: FnOnce(TestContext) -> Fut, |
| 81 | + Fut: Future<Output = anyhow::Result<TestContext>>, |
| 82 | +{ |
| 83 | + if let Ok(db_url) = std::env::var("TEST_DB_URL") { |
| 84 | + let ctx = TestContext::new(&db_url).await; |
| 85 | + let ctx = f(ctx).await.expect("Test failed"); |
| 86 | + ctx.finish().await; |
| 87 | + } else { |
| 88 | + eprintln!("Skipping test because TEST_DB_URL was not passed"); |
| 89 | + } |
| 90 | +} |
0 commit comments