return mock db when mock feature is enabled
This commit is contained in:
parent
2f9a6859f1
commit
00ba8a2804
|
@ -5,7 +5,14 @@ edition = "2021"
|
|||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[features]
|
||||
mock = []
|
||||
|
||||
[dependencies]
|
||||
once_cell = "1.17.1"
|
||||
sea-orm = { version = "0.11.3", features = ["sqlx-postgres", "runtime-tokio-rustls", "mock"] }
|
||||
sea-orm = { version = "0.11.3", features = ["sqlx-postgres", "runtime-tokio-rustls"] }
|
||||
thiserror = "1.0.40"
|
||||
tokio = { version = "1.28.1", features = ["macros"] }
|
||||
|
||||
[dev-dependencies]
|
||||
sea-orm = { version = "0.11.3", features = ["sqlx-postgres", "sqlx-sqlite", "runtime-tokio-rustls", "mock"] }
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use sea_orm::error::DbErr;
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
#[derive(thiserror::Error, Debug, PartialEq, Eq)]
|
||||
pub enum Error {
|
||||
#[error("The database connections have not been initialized yet")]
|
||||
Uninitialized,
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
pub mod error;
|
||||
|
||||
use once_cell::sync::{Lazy, OnceCell};
|
||||
use sea_orm::{Database, DatabaseBackend, DatabaseConnection, MockDatabase};
|
||||
use sea_orm::{Database, DatabaseConnection};
|
||||
|
||||
use crate::error::Error;
|
||||
|
||||
static DB_CONN: OnceCell<DatabaseConnection> = OnceCell::new();
|
||||
static DB_MOCK: Lazy<DatabaseConnection> =
|
||||
Lazy::new(|| MockDatabase::new(DatabaseBackend::Postgres).into_connection());
|
||||
static DB_CONN: once_cell::sync::OnceCell<DatabaseConnection> = once_cell::sync::OnceCell::new();
|
||||
|
||||
#[cfg(feature = "mock")]
|
||||
static DB_MOCK: once_cell::sync::Lazy<DatabaseConnection> = once_cell::sync::Lazy::new(|| {
|
||||
sea_orm::MockDatabase::new(sea_orm::DatabaseBackend::Postgres).into_connection()
|
||||
});
|
||||
|
||||
pub async fn init_database(connection_uri: impl Into<String>) -> Result<(), Error> {
|
||||
let conn = Database::connect(connection_uri.into()).await?;
|
||||
|
@ -16,19 +18,26 @@ pub async fn init_database(connection_uri: impl Into<String>) -> Result<(), Erro
|
|||
}
|
||||
|
||||
pub fn get_database() -> Result<&'static DatabaseConnection, Error> {
|
||||
if cfg!(test) {
|
||||
Ok(&DB_MOCK)
|
||||
} else {
|
||||
#[cfg(feature = "mock")]
|
||||
return Ok(&DB_MOCK);
|
||||
#[cfg(not(feature = "mock"))]
|
||||
DB_CONN.get().ok_or(Error::Uninitialized)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::get_database;
|
||||
use crate::{error::Error, init_database};
|
||||
|
||||
#[test]
|
||||
fn can_get_mock() {
|
||||
get_database().unwrap().as_mock_connection();
|
||||
fn error_uninitialized() {
|
||||
assert_eq!(get_database().unwrap_err(), Error::Uninitialized);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn connect_in_memory_sqlite() -> Result<(), Error> {
|
||||
init_database("sqlite::memory:").await?;
|
||||
get_database()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,9 +13,14 @@ jsonschema = "0.17.0"
|
|||
once_cell = "1.17.1"
|
||||
parse-display = "0.8.0"
|
||||
schemars = { version = "0.8.12", features = ["chrono"] }
|
||||
sea-orm = { version = "0.11.3", features = ["postgres-array", "sqlx-postgres", "runtime-tokio-rustls", "mock"] }
|
||||
sea-orm = { version = "0.11.3", features = ["postgres-array", "sqlx-postgres", "runtime-tokio-rustls"] }
|
||||
serde = { version = "1.0.163", features = ["derive"] }
|
||||
serde_json = "1.0.96"
|
||||
thiserror = "1.0.40"
|
||||
tokio = { version = "1.28.1", features = ["sync"] }
|
||||
util = { path = "../util" }
|
||||
utoipa = "3.3.0"
|
||||
|
||||
[dev-dependencies]
|
||||
database = { path = "../database", features = ["mock"] }
|
||||
sea-orm = { version = "0.11.3", features = ["postgres-array", "sqlx-postgres", "runtime-tokio-rustls", "mock"] }
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#[derive(thiserror::Error, Debug)]
|
||||
#[derive(thiserror::Error, Debug, PartialEq, Eq)]
|
||||
pub enum Error {
|
||||
#[error("Failed to parse string")]
|
||||
ParseError(#[from] parse_display::ParseError),
|
||||
|
|
Loading…
Reference in New Issue