From 0b2246cbd5c3c113ef1e0066ade62a71e874c901 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Sun, 29 Nov 2020 21:29:13 +0000 Subject: [PATCH] Unit tests for inserting and loading room info from db. --- src/db/rooms.rs | 38 ++++++++++++++++++++++++++++++++++++-- src/models.rs | 2 +- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/db/rooms.rs b/src/db/rooms.rs index 6d7380e..b028cbd 100644 --- a/src/db/rooms.rs +++ b/src/db/rooms.rs @@ -234,8 +234,8 @@ impl Rooms { Ok(()) } - pub fn get_room_info(&self, info: &RoomInfo) -> Result, DataError> { - let key = info.room_id.as_bytes(); + pub fn get_room_info(&self, room_id: &str) -> Result, DataError> { + let key = room_id.as_bytes(); let room_info: Option = self .roomid_roominfo @@ -377,6 +377,40 @@ mod tests { assert_eq!(HashSet::new(), rooms_for_user); } + #[test] + fn insert_room_info_works() { + let rooms = create_test_instance(); + + let info = RoomInfo { + room_id: matrix_sdk::identifiers::room_id!("!fakeroom:example.com") + .as_str() + .to_owned(), + room_name: "fake room name".to_owned(), + }; + + rooms + .insert_room_info(&info) + .expect("Could insert room info"); + + let found_info = rooms + .get_room_info("!fakeroom:example.com") + .expect("Error loading room info"); + + assert!(found_info.is_some()); + assert_eq!(info, found_info.unwrap()); + } + + #[test] + fn get_room_info_none_when_room_does_not_exist() { + let rooms = create_test_instance(); + + let found_info = rooms + .get_room_info("!fakeroom:example.com") + .expect("Error loading room info"); + + assert!(found_info.is_none()); + } + #[test] fn clear_info() { let rooms = create_test_instance(); diff --git a/src/models.rs b/src/models.rs index 3040a80..83802cb 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; /// RoomInfo has basic metadata about a room: its name, ID, etc. -#[derive(Serialize, Deserialize, Clone, Debug)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] pub struct RoomInfo { pub room_id: String, pub room_name: String,