mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 05:26:59 +00:00
test: use custom database name and add clean up
This commit is contained in:
@@ -8,12 +8,14 @@ pub use self::reference::*;
|
||||
pub enum DatabaseInfo {
|
||||
/// Auto-detect the database in use
|
||||
Auto,
|
||||
/// Auto-detect the database in use and create an empty testing database
|
||||
Test(String),
|
||||
/// Use the mock database
|
||||
Reference,
|
||||
/// Connect to MongoDB
|
||||
MongoDb(String),
|
||||
MongoDb { uri: String, database_name: String },
|
||||
/// Use existing MongoDB connection
|
||||
MongoDbFromClient(::mongodb::Client),
|
||||
MongoDbFromClient(::mongodb::Client, String),
|
||||
}
|
||||
|
||||
/// Database
|
||||
@@ -32,20 +34,34 @@ impl DatabaseInfo {
|
||||
Ok(match self {
|
||||
DatabaseInfo::Auto => {
|
||||
if let Ok(uri) = std::env::var("MONGODB") {
|
||||
return DatabaseInfo::MongoDb(uri).connect().await;
|
||||
return DatabaseInfo::MongoDb {
|
||||
uri,
|
||||
database_name: "revolt".to_string(),
|
||||
}
|
||||
.connect()
|
||||
.await;
|
||||
}
|
||||
|
||||
DatabaseInfo::Reference.connect().await?
|
||||
}
|
||||
DatabaseInfo::Test(database_name) => {
|
||||
if let Ok(uri) = std::env::var("MONGODB") {
|
||||
return DatabaseInfo::MongoDb { uri, database_name }.connect().await;
|
||||
}
|
||||
|
||||
DatabaseInfo::Reference.connect().await?
|
||||
}
|
||||
DatabaseInfo::Reference => Database::Reference(Default::default()),
|
||||
DatabaseInfo::MongoDb(uri) => {
|
||||
DatabaseInfo::MongoDb { uri, database_name } => {
|
||||
let client = ::mongodb::Client::with_uri_str(uri)
|
||||
.await
|
||||
.map_err(|_| "Failed to init db connection.".to_string())?;
|
||||
|
||||
Database::MongoDb(MongoDb(client))
|
||||
Database::MongoDb(MongoDb(client, database_name))
|
||||
}
|
||||
DatabaseInfo::MongoDbFromClient(client, database_name) => {
|
||||
Database::MongoDb(MongoDb(client, database_name))
|
||||
}
|
||||
DatabaseInfo::MongoDbFromClient(client) => Database::MongoDb(MongoDb(client)),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ use serde::Serialize;
|
||||
database_derived!(
|
||||
#[cfg(feature = "mongodb")]
|
||||
/// MongoDB implementation
|
||||
pub struct MongoDb(pub ::mongodb::Client);
|
||||
pub struct MongoDb(pub ::mongodb::Client, pub String);
|
||||
);
|
||||
|
||||
impl Deref for MongoDb {
|
||||
@@ -27,7 +27,7 @@ impl Deref for MongoDb {
|
||||
impl MongoDb {
|
||||
/// Get the Revolt database
|
||||
pub fn db(&self) -> mongodb::Database {
|
||||
self.database("revolt")
|
||||
self.database(&self.1)
|
||||
}
|
||||
|
||||
/// Get a collection by its name
|
||||
|
||||
@@ -51,12 +51,21 @@ macro_rules! auto_derived_partial {
|
||||
mod drivers;
|
||||
pub use drivers::*;
|
||||
|
||||
#[cfg(test)]
|
||||
macro_rules! database_test {
|
||||
( $name: expr ) => {
|
||||
$crate::DatabaseInfo::Reference
|
||||
( | $db: ident | $test:expr ) => {
|
||||
let db = $crate::DatabaseInfo::Test(format!("{}:{}", file!().replace('/', "_"), line!()))
|
||||
.connect()
|
||||
.await
|
||||
.expect("Database connection failed.")
|
||||
.expect("Database connection failed.");
|
||||
|
||||
#[allow(clippy::redundant_closure_call)]
|
||||
(|$db: $crate::Database| $test)(db.clone()).await;
|
||||
|
||||
match db {
|
||||
$crate::Database::Reference(_) => {}
|
||||
$crate::Database::MongoDb(db) => db.0.database(&db.1).drop(None).await.unwrap(),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -80,47 +80,47 @@ mod tests {
|
||||
|
||||
#[async_std::test]
|
||||
async fn crud() {
|
||||
let db = database_test!("bot_crud");
|
||||
database_test!(|db| async move {
|
||||
let bot_id = "bot";
|
||||
let user_id = "user";
|
||||
let token = "my_token";
|
||||
|
||||
let bot_id = "bot";
|
||||
let user_id = "user";
|
||||
let token = "my_token";
|
||||
|
||||
let bot = Bot {
|
||||
id: bot_id.to_string(),
|
||||
owner: user_id.to_string(),
|
||||
token: token.to_string(),
|
||||
interactions_url: Some("some url".to_string()),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
db.insert_bot(&bot).await.unwrap();
|
||||
db.update_bot(
|
||||
bot_id,
|
||||
&crate::PartialBot {
|
||||
public: Some(true),
|
||||
let bot = Bot {
|
||||
id: bot_id.to_string(),
|
||||
owner: user_id.to_string(),
|
||||
token: token.to_string(),
|
||||
interactions_url: Some("some url".to_string()),
|
||||
..Default::default()
|
||||
},
|
||||
vec![FieldsBot::Token, FieldsBot::InteractionsURL],
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
};
|
||||
|
||||
let fetched_bot1 = db.fetch_bot(bot_id).await.unwrap();
|
||||
let fetched_bot2 = db.fetch_bot_by_token(&fetched_bot1.token).await.unwrap();
|
||||
let fetched_bots = db.fetch_bots_by_user(user_id).await.unwrap();
|
||||
db.insert_bot(&bot).await.unwrap();
|
||||
db.update_bot(
|
||||
bot_id,
|
||||
&crate::PartialBot {
|
||||
public: Some(true),
|
||||
..Default::default()
|
||||
},
|
||||
vec![FieldsBot::Token, FieldsBot::InteractionsURL],
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(!bot.public);
|
||||
assert!(fetched_bot1.public);
|
||||
assert!(bot.interactions_url.is_some());
|
||||
assert!(fetched_bot1.interactions_url.is_none());
|
||||
assert_ne!(bot.token, fetched_bot1.token);
|
||||
assert_eq!(fetched_bot1, fetched_bot2);
|
||||
assert_eq!(fetched_bot1, fetched_bots[0]);
|
||||
assert_eq!(1, db.get_number_of_bots_by_user(user_id).await.unwrap());
|
||||
let fetched_bot1 = db.fetch_bot(bot_id).await.unwrap();
|
||||
let fetched_bot2 = db.fetch_bot_by_token(&fetched_bot1.token).await.unwrap();
|
||||
let fetched_bots = db.fetch_bots_by_user(user_id).await.unwrap();
|
||||
|
||||
bot.delete(&db).await.unwrap();
|
||||
assert!(db.fetch_bot(bot_id).await.is_err());
|
||||
assert_eq!(0, db.get_number_of_bots_by_user(user_id).await.unwrap());
|
||||
assert!(!bot.public);
|
||||
assert!(fetched_bot1.public);
|
||||
assert!(bot.interactions_url.is_some());
|
||||
assert!(fetched_bot1.interactions_url.is_none());
|
||||
assert_ne!(bot.token, fetched_bot1.token);
|
||||
assert_eq!(fetched_bot1, fetched_bot2);
|
||||
assert_eq!(fetched_bot1, fetched_bots[0]);
|
||||
assert_eq!(1, db.get_number_of_bots_by_user(user_id).await.unwrap());
|
||||
|
||||
bot.delete(&db).await.unwrap();
|
||||
assert!(db.fetch_bot(bot_id).await.is_err());
|
||||
assert_eq!(0, db.get_number_of_bots_by_user(user_id).await.unwrap())
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user