Compare commits

..

2 Commits

Author SHA1 Message Date
projectmoon 7eee16961e Add tests for dbstate.
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details
2021-05-19 21:16:39 +00:00
projectmoon 43d8f9574f Allow 'upserts' in insert_room_info. Add a few more room db tests. 2021-05-19 21:06:28 +00:00
2 changed files with 134 additions and 14 deletions

View File

@ -51,6 +51,13 @@ impl Rooms for Database {
} }
async fn insert_room_info(&self, info: &RoomInfo) -> Result<(), DataError> { async fn insert_room_info(&self, info: &RoomInfo) -> Result<(), DataError> {
//Clear out old info first, because we want this to be an "upsert."
sqlx::query("DELETE FROM room_info where room_id = ?")
.bind(&info.room_id)
.execute(&self.conn)
.await
.ok();
sqlx::query(r#"INSERT INTO room_info (room_id, room_name) VALUES (?, ?)"#) sqlx::query(r#"INSERT INTO room_info (room_id, room_name) VALUES (?, ?)"#)
.bind(&info.room_id) .bind(&info.room_id)
.bind(&info.room_name) .bind(&info.room_name)
@ -199,25 +206,37 @@ mod tests {
} }
#[tokio::test(flavor = "multi_thread", worker_threads = 1)] #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn get_room_info_constraint_test() { async fn insert_room_info_updates_existing() {
let db = create_db().await; let db = create_db().await;
let info = RoomInfo { let info1 = RoomInfo {
room_id: "myroomid".to_string(), room_id: "myroomid".to_string(),
room_name: "myroomname".to_string(), room_name: "myroomname".to_string(),
}; };
db.insert_room_info(&info) db.insert_room_info(&info1)
.await .await
.expect("Could not insert room info."); .expect("Could not insert room info1.");
let second_attempt = db.insert_room_info(&info).await; let info2 = RoomInfo {
room_id: "myroomid".to_string(),
room_name: "myroomname2".to_string(),
};
assert!(second_attempt.is_err()); db.insert_room_info(&info2)
assert!(matches!( .await
second_attempt.err().unwrap(), .expect("Could not update room info after first insert");
DataError::SqlxError(sqlx::Error::Database(_))
)); let retrieved_info = db
.get_room_info("myroomid")
.await
.expect("Could not get room info");
assert!(retrieved_info.is_some());
let retrieved_info = retrieved_info.unwrap();
assert_eq!(retrieved_info.room_id, "myroomid");
assert_eq!(retrieved_info.room_name, "myroomname2");
} }
#[tokio::test(flavor = "multi_thread", worker_threads = 1)] #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
@ -286,6 +305,44 @@ mod tests {
assert_eq!(users_in_room.len(), 0); assert_eq!(users_in_room.len(), 0);
} }
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn clear_info_does_not_delete_other_rooms() {
let db = create_db().await;
let info1 = RoomInfo {
room_id: "myroomid".to_string(),
room_name: "myroomname".to_string(),
};
let info2 = RoomInfo {
room_id: "myroomid2".to_string(),
room_name: "myroomname2".to_string(),
};
db.insert_room_info(&info1)
.await
.expect("Could not insert room info1.");
db.insert_room_info(&info2)
.await
.expect("Could not insert room info2.");
db.add_user_to_room("myuser", &info1.room_id)
.await
.expect("Could not add user to room.");
db.clear_info(&info1.room_id)
.await
.expect("Could not clear room info1");
let room_info2 = db
.get_room_info(&info2.room_id)
.await
.expect("Could not get room info2.");
assert!(room_info2.is_some());
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)] #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn clear_info_test() { async fn clear_info_test() {
let db = create_db().await; let db = create_db().await;
@ -299,23 +356,23 @@ mod tests {
.await .await
.expect("Could not insert room info."); .expect("Could not insert room info.");
db.add_user_to_room("myuser", "myroom") db.add_user_to_room("myuser", &info.room_id)
.await .await
.expect("Could not add user to room."); .expect("Could not add user to room.");
db.clear_info("myroom") db.clear_info(&info.room_id)
.await .await
.expect("Could not clear room info"); .expect("Could not clear room info");
let users_in_room = db let users_in_room = db
.get_users_in_room("myroom") .get_users_in_room(&info.room_id)
.await .await
.expect("Could not get users in room."); .expect("Could not get users in room.");
assert_eq!(users_in_room.len(), 0); assert_eq!(users_in_room.len(), 0);
let room_info = db let room_info = db
.get_room_info("myroom") .get_room_info(&info.room_id)
.await .await
.expect("Could not get room info."); .expect("Could not get room info.");

View File

@ -13,6 +13,13 @@ impl DbState for Database {
} }
async fn set_device_id(&self, device_id: &str) -> Result<(), DataError> { async fn set_device_id(&self, device_id: &str) -> Result<(), DataError> {
// This will have to be updated if we ever add another column
// to this table!
sqlx::query("DELETE FROM bot_state")
.execute(&self.conn)
.await
.ok();
sqlx::query( sqlx::query(
r#"INSERT INTO bot_state r#"INSERT INTO bot_state
(device_id) (device_id)
@ -25,3 +32,59 @@ impl DbState for Database {
Ok(()) Ok(())
} }
} }
#[cfg(test)]
mod tests {
use super::super::DbState;
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_device_id() {
let db = create_db().await;
db.set_device_id("device_id")
.await
.expect("Could not set device ID");
let device_id = db.get_device_id().await.expect("Could not get device ID");
assert!(device_id.is_some());
assert_eq!(device_id.unwrap(), "device_id");
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn no_device_id_set_returns_none() {
let db = create_db().await;
let device_id = db.get_device_id().await.expect("Could not get device ID");
assert!(device_id.is_none());
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn can_update_device_id() {
let db = create_db().await;
db.set_device_id("device_id")
.await
.expect("Could not set device ID");
db.set_device_id("device_id2")
.await
.expect("Could not set device ID");
let device_id = db.get_device_id().await.expect("Could not get device ID");
assert!(device_id.is_some());
assert_eq!(device_id.unwrap(), "device_id2");
}
}