From 5e899cd962bb192763ee7da1cc590acf924dd3a7 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Tue, 18 May 2021 15:37:20 +0000 Subject: [PATCH] Return key not found error if value not found for user. --- src/db/sqlite/variables.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/db/sqlite/variables.rs b/src/db/sqlite/variables.rs index eda8962..622415b 100644 --- a/src/db/sqlite/variables.rs +++ b/src/db/sqlite/variables.rs @@ -53,14 +53,11 @@ impl Variables for Database { room_id, variable_name ) - .fetch_one(&self.conn) + .fetch_optional(&self.conn) .await?; - // .map_err(|e| match e { - // sqlx::Error::RowNotFound => Err(DataError::KeyDoesNotExist(variable_name.clone())), - // _ => Err(e.into()), - // })?; - Ok(row.value) + row.map(|r| r.value) + .ok_or_else(|| DataError::KeyDoesNotExist(variable_name.to_string())) } async fn set_user_variable( @@ -144,8 +141,11 @@ mod tests { let value = db.get_user_variable("myuser", "myroom", "myvariable").await; - println!("{:?}", value); assert!(value.is_err()); + assert!(matches!( + value.err().unwrap(), + DataError::KeyDoesNotExist(_) + )); } #[tokio::test(flavor = "multi_thread", worker_threads = 1)] @@ -161,7 +161,10 @@ mod tests { .get_user_variable("myuser2", "myroom", "myvariable") .await; - println!("{:?}", value); assert!(value.is_err()); + assert!(matches!( + value.err().unwrap(), + DataError::KeyDoesNotExist(_) + )); } }