adds kordophone-db
This commit is contained in:
69
kordophone-db/src/lib.rs
Normal file
69
kordophone-db/src/lib.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
pub mod models;
|
||||
pub mod chat_database;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use microrm::prelude::Queryable;
|
||||
|
||||
use crate::{chat_database::{self, ChatDatabase}, models::conversation::{Conversation, ConversationBuilder, Participant}};
|
||||
|
||||
#[test]
|
||||
fn test_database_init() {
|
||||
let _ = ChatDatabase::new_in_memory().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_conversation() {
|
||||
let db = ChatDatabase::new_in_memory().unwrap();
|
||||
|
||||
let test_conversation = Conversation::builder()
|
||||
.guid("test")
|
||||
.unread_count(2)
|
||||
.display_name("Test Conversation")
|
||||
.build();
|
||||
|
||||
let id = db.insert_conversation(test_conversation.clone()).unwrap();
|
||||
|
||||
// Try to fetch with id now
|
||||
let conversation = db.get_conversation_by_id(id).unwrap().unwrap();
|
||||
assert_eq!(conversation.guid, "test");
|
||||
|
||||
// Modify the conversation and update it
|
||||
let modified_conversation = test_conversation.into_builder()
|
||||
.display_name("Modified Conversation")
|
||||
.build();
|
||||
|
||||
db.insert_conversation(modified_conversation.clone()).unwrap();
|
||||
|
||||
// Make sure we still only have one conversation.
|
||||
let all_conversations = db.all_conversations().unwrap();
|
||||
assert_eq!(all_conversations.len(), 1);
|
||||
|
||||
// And make sure the display name was updated
|
||||
let conversation = db.get_conversation_by_id(id).unwrap().unwrap();
|
||||
assert_eq!(conversation.display_name.unwrap(), "Modified Conversation");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_conversation_participants() {
|
||||
let db = ChatDatabase::new_in_memory().unwrap();
|
||||
|
||||
let participants: Vec<String> = vec!["one".into(), "two".into()];
|
||||
let conversation = ConversationBuilder::new()
|
||||
.display_name("Test")
|
||||
.participant_display_names(participants.clone())
|
||||
.build();
|
||||
|
||||
let id = db.insert_conversation(conversation).unwrap();
|
||||
|
||||
let read_conversation = db.get_conversation_by_id(id).unwrap().unwrap();
|
||||
let read_participants: Vec<String> = read_conversation.participant_display_names
|
||||
.get()
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|p| p.wrapped().display_name)
|
||||
.collect();
|
||||
|
||||
assert_eq!(participants, read_participants);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user