De-asyncify database methods

This commit is contained in:
projectmoon 2020-10-24 13:28:19 +00:00 committed by ProjectMoon
parent 5410c53513
commit 4d9ad42bdd
3 changed files with 43 additions and 68 deletions

View File

@ -125,8 +125,7 @@ async fn calculate_dice_amount<'a>(pool: &'a DicePoolWithContext<'a>) -> Result<
.1 .1
.db .db
.variables .variables
.get_user_variables(&pool.1.room_id, &pool.1.username) .get_user_variables(&pool.1.room_id, &pool.1.username)?;
.await?;
let variables = &variables; let variables = &variables;
@ -586,7 +585,6 @@ mod tests {
db.variables db.variables
.set_user_variable(&ctx.room_id, &ctx.username, "myvariable", 10) .set_user_variable(&ctx.room_id, &ctx.username, "myvariable", 10)
.await
.expect("could not set myvariable to 10"); .expect("could not set myvariable to 10");
let amounts = vec![Amount { let amounts = vec![Amount {

View File

@ -120,12 +120,12 @@ impl Command for GetAllVariablesCommand {
} }
async fn execute(&self, ctx: &Context) -> Execution { async fn execute(&self, ctx: &Context) -> Execution {
let value = match ctx let result = ctx
.db .db
.variables .variables
.get_user_variables(&ctx.room_id, &ctx.username) .get_user_variables(&ctx.room_id, &ctx.username);
.await
{ let value = match result {
Ok(variables) => { Ok(variables) => {
let mut variable_list = variables let mut variable_list = variables
.into_iter() .into_iter()
@ -157,12 +157,12 @@ impl Command for GetVariableCommand {
async fn execute(&self, ctx: &Context) -> Execution { async fn execute(&self, ctx: &Context) -> Execution {
let name = &self.0; let name = &self.0;
let value = match ctx let result = ctx
.db .db
.variables .variables
.get_user_variable(&ctx.room_id, &ctx.username, name) .get_user_variable(&ctx.room_id, &ctx.username, name);
.await
{ let value = match result {
Ok(num) => format!("{} = {}", name, num), Ok(num) => format!("{} = {}", name, num),
Err(DataError::KeyDoesNotExist(_)) => format!("{} is not set", name), Err(DataError::KeyDoesNotExist(_)) => format!("{} is not set", name),
Err(e) => format!("error getting {}: {}", name, e), Err(e) => format!("error getting {}: {}", name, e),
@ -188,8 +188,7 @@ impl Command for SetVariableCommand {
let result = ctx let result = ctx
.db .db
.variables .variables
.set_user_variable(&ctx.room_id, &ctx.username, name, value) .set_user_variable(&ctx.room_id, &ctx.username, name, value);
.await;
let content = match result { let content = match result {
Ok(_) => format!("{} = {}", name, value), Ok(_) => format!("{} = {}", name, value),
@ -212,12 +211,12 @@ impl Command for DeleteVariableCommand {
async fn execute(&self, ctx: &Context) -> Execution { async fn execute(&self, ctx: &Context) -> Execution {
let name = &self.0; let name = &self.0;
let value = match ctx let result = ctx
.db .db
.variables .variables
.delete_user_variable(&ctx.room_id, &ctx.username, name) .delete_user_variable(&ctx.room_id, &ctx.username, name);
.await
{ let value = match result {
Ok(()) => format!("{} now unset", name), Ok(()) => format!("{} now unset", name),
Err(DataError::KeyDoesNotExist(_)) => format!("{} is not currently set", name), Err(DataError::KeyDoesNotExist(_)) => format!("{} is not currently set", name),
Err(e) => format!("error deleting {}: {}", name, e), Err(e) => format!("error deleting {}: {}", name, e),

View File

@ -67,7 +67,7 @@ fn alter_room_variable_count(
} }
impl Variables { impl Variables {
pub async fn get_user_variables( pub fn get_user_variables(
&self, &self,
room_id: &str, room_id: &str,
username: &str, username: &str,
@ -93,11 +93,7 @@ impl Variables {
variables.map(|entries| entries.into_iter().collect()) variables.map(|entries| entries.into_iter().collect())
} }
pub async fn get_variable_count( pub fn get_variable_count(&self, room_id: &str, username: &str) -> Result<i32, DataError> {
&self,
room_id: &str,
username: &str,
) -> Result<i32, DataError> {
let key = room_variable_count_key(room_id, username); let key = room_variable_count_key(room_id, username);
if let Some(raw_value) = self.0.get(&key)? { if let Some(raw_value) = self.0.get(&key)? {
convert_i32(&raw_value) convert_i32(&raw_value)
@ -106,7 +102,7 @@ impl Variables {
} }
} }
pub async fn get_user_variable( pub fn get_user_variable(
&self, &self,
room_id: &str, room_id: &str,
username: &str, username: &str,
@ -121,7 +117,7 @@ impl Variables {
} }
} }
pub async fn set_user_variable( pub fn set_user_variable(
&self, &self,
room_id: &str, room_id: &str,
username: &str, username: &str,
@ -147,7 +143,7 @@ impl Variables {
.map_err(|e| e.into()) .map_err(|e| e.into())
} }
pub async fn delete_user_variable( pub fn delete_user_variable(
&self, &self,
room_id: &str, room_id: &str,
username: &str, username: &str,
@ -185,8 +181,8 @@ mod tests {
//Room Variable count tests //Room Variable count tests
#[tokio::test] #[test]
async fn alter_room_variable_count_test() { fn alter_room_variable_count_test() {
let variables = create_test_instance(); let variables = create_test_instance();
let alter_count = |amount: i32| { let alter_count = |amount: i32| {
@ -201,24 +197,23 @@ mod tests {
.expect("got transaction failure"); .expect("got transaction failure");
}; };
async fn get_count(variables: &Variables) -> i32 { fn get_count(variables: &Variables) -> i32 {
variables variables
.get_variable_count("room", "username") .get_variable_count("room", "username")
.await
.expect("could not get variable count") .expect("could not get variable count")
} }
//addition //addition
alter_count(5); alter_count(5);
assert_eq!(5, get_count(&variables).await); assert_eq!(5, get_count(&variables));
//subtraction //subtraction
alter_count(-3); alter_count(-3);
assert_eq!(2, get_count(&variables).await); assert_eq!(2, get_count(&variables));
} }
#[tokio::test] #[test]
async fn alter_room_variable_count_cannot_go_below_0_test() { fn alter_room_variable_count_cannot_go_below_0_test() {
let variables = create_test_instance(); let variables = create_test_instance();
variables variables
@ -233,58 +228,51 @@ mod tests {
let count = variables let count = variables
.get_variable_count("room", "username") .get_variable_count("room", "username")
.await
.expect("could not get variable count"); .expect("could not get variable count");
assert_eq!(0, count); assert_eq!(0, count);
} }
#[tokio::test] #[test]
async fn empty_db_reports_0_room_variable_count_test() { fn empty_db_reports_0_room_variable_count_test() {
let variables = create_test_instance(); let variables = create_test_instance();
let count = variables let count = variables
.get_variable_count("room", "username") .get_variable_count("room", "username")
.await
.expect("could not get variable count"); .expect("could not get variable count");
assert_eq!(0, count); assert_eq!(0, count);
} }
#[tokio::test] #[test]
async fn set_user_variable_increments_count() { fn set_user_variable_increments_count() {
let variables = create_test_instance(); let variables = create_test_instance();
variables variables
.set_user_variable("room", "username", "myvariable", 5) .set_user_variable("room", "username", "myvariable", 5)
.await
.expect("could not insert variable"); .expect("could not insert variable");
let count = variables let count = variables
.get_variable_count("room", "username") .get_variable_count("room", "username")
.await
.expect("could not get variable count"); .expect("could not get variable count");
assert_eq!(1, count); assert_eq!(1, count);
} }
#[tokio::test] #[test]
async fn update_user_variable_does_not_increment_count() { fn update_user_variable_does_not_increment_count() {
let variables = create_test_instance(); let variables = create_test_instance();
variables variables
.set_user_variable("room", "username", "myvariable", 5) .set_user_variable("room", "username", "myvariable", 5)
.await
.expect("could not insert variable"); .expect("could not insert variable");
variables variables
.set_user_variable("room", "username", "myvariable", 10) .set_user_variable("room", "username", "myvariable", 10)
.await
.expect("could not update variable"); .expect("could not update variable");
let count = variables let count = variables
.get_variable_count("room", "username") .get_variable_count("room", "username")
.await
.expect("could not get variable count"); .expect("could not get variable count");
assert_eq!(1, count); assert_eq!(1, count);
@ -292,61 +280,51 @@ mod tests {
// Set/get/delete variable tests // Set/get/delete variable tests
#[tokio::test] #[test]
async fn set_and_get_variable_test() { fn set_and_get_variable_test() {
let variables = create_test_instance(); let variables = create_test_instance();
variables variables
.set_user_variable("room", "username", "myvariable", 5) .set_user_variable("room", "username", "myvariable", 5)
.await
.expect("could not insert variable"); .expect("could not insert variable");
let value = variables let value = variables
.get_user_variable("room", "username", "myvariable") .get_user_variable("room", "username", "myvariable")
.await
.expect("could not get value"); .expect("could not get value");
assert_eq!(5, value); assert_eq!(5, value);
} }
#[tokio::test] #[test]
async fn delete_variable_test() { fn delete_variable_test() {
let variables = create_test_instance(); let variables = create_test_instance();
variables variables
.set_user_variable("room", "username", "myvariable", 5) .set_user_variable("room", "username", "myvariable", 5)
.await
.expect("could not insert variable"); .expect("could not insert variable");
variables variables
.delete_user_variable("room", "username", "myvariable") .delete_user_variable("room", "username", "myvariable")
.await
.expect("could not delete value"); .expect("could not delete value");
let result = variables let result = variables.get_user_variable("room", "username", "myvariable");
.get_user_variable("room", "username", "myvariable")
.await;
assert!(result.is_err()); assert!(result.is_err());
assert!(matches!(result, Err(DataError::KeyDoesNotExist(_)))); assert!(matches!(result, Err(DataError::KeyDoesNotExist(_))));
} }
#[tokio::test] #[test]
async fn get_missing_variable_returns_key_does_not_exist() { fn get_missing_variable_returns_key_does_not_exist() {
let variables = create_test_instance(); let variables = create_test_instance();
let result = variables let result = variables.get_user_variable("room", "username", "myvariable");
.get_user_variable("room", "username", "myvariable")
.await;
assert!(result.is_err()); assert!(result.is_err());
assert!(matches!(result, Err(DataError::KeyDoesNotExist(_)))); assert!(matches!(result, Err(DataError::KeyDoesNotExist(_))));
} }
#[tokio::test] #[test]
async fn remove_missing_variable_returns_key_does_not_exist() { fn remove_missing_variable_returns_key_does_not_exist() {
let variables = create_test_instance(); let variables = create_test_instance();
let result = variables let result = variables.delete_user_variable("room", "username", "myvariable");
.delete_user_variable("room", "username", "myvariable")
.await;
assert!(result.is_err()); assert!(result.is_err());
assert!(matches!(result, Err(DataError::KeyDoesNotExist(_)))); assert!(matches!(result, Err(DataError::KeyDoesNotExist(_))));