use a fresh db for each test

This commit is contained in:
azdle 2025-07-22 12:15:26 -05:00
parent 741268ef1e
commit 458207cea3
2 changed files with 8 additions and 20 deletions

View file

@ -13,7 +13,7 @@ async fn login_succeeds_with_valid_credentials() -> Result<()> {
let resp = client
.post(server.url("/auth/signup"))
.header("Content-Type", "application/x-www-form-urlencoded")
.body("email=admin1&password=hunter2")
.body("email=admin&password=hunter2")
.send()
.await?;
@ -23,7 +23,7 @@ async fn login_succeeds_with_valid_credentials() -> Result<()> {
let resp = client
.post(server.url("/auth/login"))
.header("Content-Type", "application/x-www-form-urlencoded")
.body("email=admin1&password=hunter2")
.body("email=admin&password=hunter2")
.send()
.await?;
@ -46,7 +46,7 @@ async fn login_fails_with_invalid_credentials() -> Result<()> {
let resp = client
.post(server.url("/auth/signup"))
.header("Content-Type", "application/x-www-form-urlencoded")
.body("email=admin2&password=hunter2")
.body("email=admin&password=hunter2")
.send()
.await?;
@ -56,7 +56,7 @@ async fn login_fails_with_invalid_credentials() -> Result<()> {
let resp = client
.post(server.url("/auth/login"))
.header("Content-Type", "application/x-www-form-urlencoded")
.body("email=admin2&password=hunter3")
.body("email=admin&password=hunter3")
.send()
.await?;

View file

@ -1,4 +1,4 @@
use anyhow::{bail, Result};
use anyhow::Result;
use bollard::query_parameters::{CreateImageOptionsBuilder, ListContainersOptionsBuilder};
use bollard::secret::{
ContainerCreateBody, ContainerInspectResponse, ContainerState, CreateImageInfo, Health,
@ -12,26 +12,16 @@ use sqlx::migrate::MigrateDatabase;
use sqlx::{Connection, PgConnection};
use std::net::SocketAddr;
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::OnceCell;
use tokio::task::JoinHandle;
use tokio::time::sleep;
use tracing::{debug, trace};
use zero_to_axum::{conf, Conf, ZeroToAxum};
static SHARED_DB: OnceCell<Arc<TestDb>> = OnceCell::const_new();
async fn get_shared_db() -> Arc<TestDb> {
SHARED_DB
.get_or_init(|| async { Arc::new(TestDb::spawn().await) })
.await
.clone()
}
pub struct TestServer {
server_task_handle: JoinHandle<()>,
addr: SocketAddr,
db: Arc<TestDb>,
_db: TestDb,
pub mock_smtp_server: maik::MockServer,
}
@ -39,9 +29,7 @@ impl TestServer {
pub async fn spawn() -> TestServer {
debug!("start test server");
// TODO: allow per-test DBs in some cases
// let db = TestDb::spawn().await;
let db = get_shared_db().await;
let db = TestDb::spawn().await;
let url = dbg!(db.get_url());
let mock_smtp_server = maik::MockServer::builder()
@ -76,7 +64,7 @@ impl TestServer {
TestServer {
server_task_handle,
addr,
db,
_db: db,
mock_smtp_server,
}
}