From 257f3a066c2b401ac235c27b5f5fa1472dcd4784 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Tue, 18 May 2021 15:17:30 +0000 Subject: [PATCH] Some user variable tests. --- src/db/sqlite/variables.rs | 65 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/db/sqlite/variables.rs b/src/db/sqlite/variables.rs index c35ee14..eda8962 100644 --- a/src/db/sqlite/variables.rs +++ b/src/db/sqlite/variables.rs @@ -55,6 +55,10 @@ impl Variables for Database { ) .fetch_one(&self.conn) .await?; + // .map_err(|e| match e { + // sqlx::Error::RowNotFound => Err(DataError::KeyDoesNotExist(variable_name.clone())), + // _ => Err(e.into()), + // })?; Ok(row.value) } @@ -100,3 +104,64 @@ impl Variables for Database { 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()); + } +}