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