How to mock with rocket #923
-
Hi :) #[derive(Database, Debug)]
#[database("database")]
pub struct Db(SeaOrmPool);
#[derive(Debug)]
pub struct SeaOrmPool {
pub conn: sea_orm::DatabaseConnection,
}
#[async_trait]
impl sea_orm_rocket::Pool for SeaOrmPool {
type Connection = sea_orm::DatabaseConnection;
type Error = sea_orm::DbErr;
async fn init(figment: &Figment) -> Result<Self, Self::Error> {
let options = parse_rocket_config(figment);
let conn = sea_orm::Database::connect(options).await?;
Ok(SeaOrmPool { conn })
}
fn borrow(&self) -> &Self::Connection {
&self.conn
}
} Looking at the src i see that connect automatically gives me a mocked connection if the mock feature is enabled. // if the uri is a valid postgres uri it goes here
#[cfg(feature = "sqlx-postgres")]
if DbBackend::Postgres.is_prefix_of(&opt.url) {
return crate::SqlxPostgresConnector::connect(opt).await;
}
// if it isn't it goes here but the mock connector only accepts valid postgres uris because the feature is enabled. see below
#[cfg(feature = "mock")]
if crate::MockDatabaseConnector::accepts(&opt.url) {
return crate::MockDatabaseConnector::connect(&opt.url).await;
}
Err(DbErr::Conn(format!(
"The connection string '{}' has no supporting driver.",
opt.url
))) The implementation of the MockDatabaseConnector looks like this: pub fn accepts(string: &str) -> bool {
#[cfg(feature = "sqlx-postgres")]
if DbBackend::Postgres.is_prefix_of(string) {
return true;
}
false
} It only returns the needed true if the postgres feature is enabled AND its a valid uri but then this code will never be reached because its a valid uri for the valid database. Is this even the intentional way to do it? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
So it looks like i indeed missed something. [features]
mock = ["sea-orm/mock"]
[dependencies]
sea-orm = { version = "0.9.0", features = ["sqlx-postgres", "runtime-tokio-native-tls"] } Fixed code: #[derive(Database, Debug)]
#[database("database")]
pub struct Db(SeaOrmPool);
#[derive(Debug)]
#[cfg_attr(not(feature = "mock"), derive(Clone))]
pub struct SeaOrmPool {
pub conn: sea_orm::DatabaseConnection,
}
/// The real pool implementation using the real database
#[async_trait]
#[cfg(not(feature = "mock"))]
impl Pool for SeaOrmPool {
type Connection = sea_orm::DatabaseConnection;
type Error = sea_orm::DbErr;
async fn init(figment: &Figment) -> Result<Self, Self::Error> {
let options = parse_rocket_config(figment);
let conn = sea_orm::Database::connect(options).await?;
Ok(Self { conn })
}
fn borrow(&self) -> &Self::Connection {
&self.conn
}
}
/// Mocked implementation of the database pool.
#[async_trait]
#[cfg(feature = "mock")]
impl Pool for SeaOrmPool {
type Connection = sea_orm::DatabaseConnection;
type Error = sea_orm::DbErr;
async fn init(_figment: &Figment) -> Result<Self, Self::Error> {
// add mocking logic
let conn = sea_orm::MockDatabase::new(sea_orm::DatabaseBackend::Postgres).into_connection();
Ok(Self { conn })
}
fn borrow(&self) -> &Self::Connection {
&self.conn
}
} |
Beta Was this translation helpful? Give feedback.
-
Hey @open-schnick, perhaps you want to checkout our Rocket example with mock testing (work-in-progress) |
Beta Was this translation helpful? Give feedback.
So it looks like i indeed missed something.
I thought you could just change a feature flag and sea-orm automatically uses a mocked implementation.
This is not the case.
You have to provide a different implementation of the
sea_orm_rocket::Pool
trait when running tests.I implemented that by using a feature flag, that also enables the
mock
feature in sea orm.Cargo.toml:
Fixed code: