use sqlx::SqlitePool;
use umbral_core::app::{App, BuildError};
use umbral_core::db::{self, DbPool};
use umbral_core::settings::Settings;
#[tokio::test]
async fn dbpool_round_trips_through_from_sqlitepool() {
let sp = SqlitePool::connect("sqlite::memory:").await.unwrap();
let dp: DbPool = sp.clone().into();
assert_eq!(dp.backend_name(), "sqlite");
assert!(dp.as_sqlite().is_some());
assert!(dp.as_postgres().is_none());
let inner = dp.sqlite_or_panic();
let (one,): (i64,) = sqlx::query_as("SELECT 1").fetch_one(inner).await.unwrap();
assert_eq!(one, 1);
}
#[tokio::test]
async fn unsupported_scheme_surfaces_as_configuration_error() {
let result = db::connect("mysql://user:pass@host/db").await;
match result {
Err(sqlx::Error::Configuration(msg)) => {
assert!(msg.to_string().contains("mysql"));
}
other => panic!("expected sqlx::Error::Configuration, got {other:?}"),
}
}
#[tokio::test]
async fn build_rejects_url_pool_backend_mismatch() {
let mut settings = Settings::from_env().expect("figment defaults load");
settings.database_url = "postgres://nobody@nowhere/missing".to_string();
let sqlite_pool = SqlitePool::connect("sqlite::memory:").await.unwrap();
let result = App::builder()
.settings(settings)
.database("default", sqlite_pool)
.build();
match result {
Err(BuildError::DatabaseBackendMismatch {
url_backend,
pool_backend,
}) => {
assert_eq!(url_backend, "postgres");
assert_eq!(pool_backend, "sqlite");
}
Err(other) => panic!("expected DatabaseBackendMismatch, got {other:?}"),
Ok(_) => panic!("expected DatabaseBackendMismatch, got Ok"),
}
}