From 00ba8a280452a067c3e6f502995e5919a1719da7 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Thu, 25 May 2023 21:40:04 -0400 Subject: [PATCH] return mock db when mock feature is enabled --- .../native-utils/crates/database/Cargo.toml | 9 ++++- .../native-utils/crates/database/src/error.rs | 2 +- .../native-utils/crates/database/src/lib.rs | 33 ++++++++++++------- .../native-utils/crates/model/Cargo.toml | 7 +++- .../native-utils/crates/model/src/error.rs | 2 +- 5 files changed, 37 insertions(+), 16 deletions(-) diff --git a/packages/backend/native-utils/crates/database/Cargo.toml b/packages/backend/native-utils/crates/database/Cargo.toml index 8498f52f24..e4c60623e0 100644 --- a/packages/backend/native-utils/crates/database/Cargo.toml +++ b/packages/backend/native-utils/crates/database/Cargo.toml @@ -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"] } diff --git a/packages/backend/native-utils/crates/database/src/error.rs b/packages/backend/native-utils/crates/database/src/error.rs index b70964f2ee..babdd68318 100644 --- a/packages/backend/native-utils/crates/database/src/error.rs +++ b/packages/backend/native-utils/crates/database/src/error.rs @@ -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, diff --git a/packages/backend/native-utils/crates/database/src/lib.rs b/packages/backend/native-utils/crates/database/src/lib.rs index 5506c702a2..756be123e0 100644 --- a/packages/backend/native-utils/crates/database/src/lib.rs +++ b/packages/backend/native-utils/crates/database/src/lib.rs @@ -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 = OnceCell::new(); -static DB_MOCK: Lazy = - Lazy::new(|| MockDatabase::new(DatabaseBackend::Postgres).into_connection()); +static DB_CONN: once_cell::sync::OnceCell = once_cell::sync::OnceCell::new(); + +#[cfg(feature = "mock")] +static DB_MOCK: once_cell::sync::Lazy = once_cell::sync::Lazy::new(|| { + sea_orm::MockDatabase::new(sea_orm::DatabaseBackend::Postgres).into_connection() +}); pub async fn init_database(connection_uri: impl Into) -> Result<(), Error> { let conn = Database::connect(connection_uri.into()).await?; @@ -16,19 +18,26 @@ pub async fn init_database(connection_uri: impl Into) -> Result<(), Erro } pub fn get_database() -> Result<&'static DatabaseConnection, Error> { - if cfg!(test) { - Ok(&DB_MOCK) - } else { - DB_CONN.get().ok_or(Error::Uninitialized) - } + #[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(()) } } diff --git a/packages/backend/native-utils/crates/model/Cargo.toml b/packages/backend/native-utils/crates/model/Cargo.toml index 832375ec7c..8714afdb41 100644 --- a/packages/backend/native-utils/crates/model/Cargo.toml +++ b/packages/backend/native-utils/crates/model/Cargo.toml @@ -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"] } diff --git a/packages/backend/native-utils/crates/model/src/error.rs b/packages/backend/native-utils/crates/model/src/error.rs index f75c0119a3..379ecb7842 100644 --- a/packages/backend/native-utils/crates/model/src/error.rs +++ b/packages/backend/native-utils/crates/model/src/error.rs @@ -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),