Lines
100 %
Functions
Branches
use super::{DB_POOL, DBError, get_connection, test_pool_is_set};
use sqlx::PgPool;
use tokio::sync::OnceCell;
/// Context for keeping environment intact
static CONTEXT: OnceCell<()> = OnceCell::const_new();
async fn setup() {
CONTEXT
.get_or_init(|| async {
#[cfg(feature = "testlog")]
let _ = env_logger::builder()
.is_test(true)
.filter_level(log::LevelFilter::Trace)
.try_init();
})
.await;
}
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn migrations_create_schema_without_seed(pool: PgPool) -> sqlx::Result<()> {
setup().await;
let mut conn = pool.acquire().await?;
// The migration set is DDL-only: it creates the `config` table but no
// longer seeds it (seeding moved to `bootstrap::seed`). So a freshly
// migrated DB has the table present and empty.
let config_rows: i64 = sqlx::query_scalar("SELECT count(*) FROM config")
.fetch_one(&mut *conn)
.await?;
assert_eq!(config_rows, 0, "migration set must not seed config rows");
Ok(())
async fn the_installed_pool_is_an_owned_handle(pool: PgPool) -> anyhow::Result<()> {
DB_POOL.set(Some(pool));
assert!(test_pool_is_set());
let mut conn = get_connection().await?;
let one: i32 = sqlx::query_scalar("SELECT 1").fetch_one(&mut *conn).await?;
assert_eq!(one, 1);
async fn a_closed_installed_pool_reports_pool_closed(pool: PgPool) -> anyhow::Result<()> {
DB_POOL.set(Some(pool.clone()));
pool.close().await;
let outcome = get_connection().await;
assert!(
matches!(outcome, Err(DBError::Sqlx(sqlx::Error::PoolClosed))),
"{outcome:?}"
);
#[tokio::test]
async fn a_missing_test_pool_is_an_error() {
matches!(outcome, Err(DBError::TestPoolUnset)),