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 {
|
pub enum DatabaseInfo {
|
||||||
/// Auto-detect the database in use
|
/// Auto-detect the database in use
|
||||||
Auto,
|
Auto,
|
||||||
|
/// Auto-detect the database in use and create an empty testing database
|
||||||
|
Test(String),
|
||||||
/// Use the mock database
|
/// Use the mock database
|
||||||
Reference,
|
Reference,
|
||||||
/// Connect to MongoDB
|
/// Connect to MongoDB
|
||||||
MongoDb(String),
|
MongoDb { uri: String, database_name: String },
|
||||||
/// Use existing MongoDB connection
|
/// Use existing MongoDB connection
|
||||||
MongoDbFromClient(::mongodb::Client),
|
MongoDbFromClient(::mongodb::Client, String),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Database
|
/// Database
|
||||||
@@ -32,20 +34,34 @@ impl DatabaseInfo {
|
|||||||
Ok(match self {
|
Ok(match self {
|
||||||
DatabaseInfo::Auto => {
|
DatabaseInfo::Auto => {
|
||||||
if let Ok(uri) = std::env::var("MONGODB") {
|
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.connect().await?
|
||||||
}
|
}
|
||||||
DatabaseInfo::Reference => Database::Reference(Default::default()),
|
DatabaseInfo::Reference => Database::Reference(Default::default()),
|
||||||
DatabaseInfo::MongoDb(uri) => {
|
DatabaseInfo::MongoDb { uri, database_name } => {
|
||||||
let client = ::mongodb::Client::with_uri_str(uri)
|
let client = ::mongodb::Client::with_uri_str(uri)
|
||||||
.await
|
.await
|
||||||
.map_err(|_| "Failed to init db connection.".to_string())?;
|
.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!(
|
database_derived!(
|
||||||
#[cfg(feature = "mongodb")]
|
#[cfg(feature = "mongodb")]
|
||||||
/// MongoDB implementation
|
/// MongoDB implementation
|
||||||
pub struct MongoDb(pub ::mongodb::Client);
|
pub struct MongoDb(pub ::mongodb::Client, pub String);
|
||||||
);
|
);
|
||||||
|
|
||||||
impl Deref for MongoDb {
|
impl Deref for MongoDb {
|
||||||
@@ -27,7 +27,7 @@ impl Deref for MongoDb {
|
|||||||
impl MongoDb {
|
impl MongoDb {
|
||||||
/// Get the Revolt database
|
/// Get the Revolt database
|
||||||
pub fn db(&self) -> mongodb::Database {
|
pub fn db(&self) -> mongodb::Database {
|
||||||
self.database("revolt")
|
self.database(&self.1)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a collection by its name
|
/// Get a collection by its name
|
||||||
|
|||||||
@@ -51,12 +51,21 @@ macro_rules! auto_derived_partial {
|
|||||||
mod drivers;
|
mod drivers;
|
||||||
pub use drivers::*;
|
pub use drivers::*;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
macro_rules! database_test {
|
macro_rules! database_test {
|
||||||
( $name: expr ) => {
|
( | $db: ident | $test:expr ) => {
|
||||||
$crate::DatabaseInfo::Reference
|
let db = $crate::DatabaseInfo::Test(format!("{}:{}", file!().replace('/', "_"), line!()))
|
||||||
.connect()
|
.connect()
|
||||||
.await
|
.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_std::test]
|
||||||
async fn crud() {
|
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 bot = Bot {
|
||||||
let user_id = "user";
|
id: bot_id.to_string(),
|
||||||
let token = "my_token";
|
owner: user_id.to_string(),
|
||||||
|
token: token.to_string(),
|
||||||
let bot = Bot {
|
interactions_url: Some("some url".to_string()),
|
||||||
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),
|
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
};
|
||||||
vec![FieldsBot::Token, FieldsBot::InteractionsURL],
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let fetched_bot1 = db.fetch_bot(bot_id).await.unwrap();
|
db.insert_bot(&bot).await.unwrap();
|
||||||
let fetched_bot2 = db.fetch_bot_by_token(&fetched_bot1.token).await.unwrap();
|
db.update_bot(
|
||||||
let fetched_bots = db.fetch_bots_by_user(user_id).await.unwrap();
|
bot_id,
|
||||||
|
&crate::PartialBot {
|
||||||
|
public: Some(true),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
vec![FieldsBot::Token, FieldsBot::InteractionsURL],
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
assert!(!bot.public);
|
let fetched_bot1 = db.fetch_bot(bot_id).await.unwrap();
|
||||||
assert!(fetched_bot1.public);
|
let fetched_bot2 = db.fetch_bot_by_token(&fetched_bot1.token).await.unwrap();
|
||||||
assert!(bot.interactions_url.is_some());
|
let fetched_bots = db.fetch_bots_by_user(user_id).await.unwrap();
|
||||||
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!(!bot.public);
|
||||||
assert!(db.fetch_bot(bot_id).await.is_err());
|
assert!(fetched_bot1.public);
|
||||||
assert_eq!(0, db.get_number_of_bots_by_user(user_id).await.unwrap());
|
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