Some user variable tests.
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
projectmoon 2021-05-18 15:17:30 +00:00
parent e539dcac1f
commit 257f3a066c
1 changed files with 65 additions and 0 deletions

View File

@ -55,6 +55,10 @@ impl Variables for Database {
) )
.fetch_one(&self.conn) .fetch_one(&self.conn)
.await?; .await?;
// .map_err(|e| match e {
// sqlx::Error::RowNotFound => Err(DataError::KeyDoesNotExist(variable_name.clone())),
// _ => Err(e.into()),
// })?;
Ok(row.value) Ok(row.value)
} }
@ -100,3 +104,64 @@ impl Variables for Database {
Ok(()) Ok(())
} }
} }
#[cfg(test)]
mod tests {
use super::*;
async fn create_db() -> Database {
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
crate::db::sqlite::migrator::migrate(db_path.path().to_str().unwrap())
.await
.unwrap();
Database::new(db_path.path().to_str().unwrap())
.await
.unwrap()
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn set_and_get_variable_test() {
use super::super::Variables;
let db = create_db().await;
db.set_user_variable("myuser", "myroom", "myvariable", 1)
.await
.expect("Could not set variable");
let value = db
.get_user_variable("myuser", "myroom", "myvariable")
.await
.expect("Could not get variable");
assert_eq!(value, 1);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn get_missing_variable_test() {
use super::super::Variables;
let db = create_db().await;
let value = db.get_user_variable("myuser", "myroom", "myvariable").await;
println!("{:?}", value);
assert!(value.is_err());
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn get_other_user_variable_test() {
use super::super::Variables;
let db = create_db().await;
db.set_user_variable("myuser1", "myroom", "myvariable", 1)
.await
.expect("Could not set variable");
let value = db
.get_user_variable("myuser2", "myroom", "myvariable")
.await;
println!("{:?}", value);
assert!(value.is_err());
}
}