reorg: move tests to separate module
This commit is contained in:
@@ -3,122 +3,4 @@ pub mod chat_database;
|
|||||||
pub mod schema;
|
pub mod schema;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests;
|
||||||
use crate::{
|
|
||||||
chat_database::ChatDatabase,
|
|
||||||
models::{
|
|
||||||
conversation::{Conversation, ConversationBuilder},
|
|
||||||
participant::Participant
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_database_init() {
|
|
||||||
let _ = ChatDatabase::new_in_memory().unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_add_conversation() {
|
|
||||||
let mut db = ChatDatabase::new_in_memory().unwrap();
|
|
||||||
|
|
||||||
let guid = "test";
|
|
||||||
let test_conversation = Conversation::builder()
|
|
||||||
.guid(guid)
|
|
||||||
.unread_count(2)
|
|
||||||
.display_name("Test Conversation")
|
|
||||||
.build();
|
|
||||||
|
|
||||||
db.insert_conversation(test_conversation.clone()).unwrap();
|
|
||||||
|
|
||||||
// Try to fetch with id now
|
|
||||||
let conversation = db.get_conversation_by_guid(guid).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_guid(guid).unwrap().unwrap();
|
|
||||||
assert_eq!(conversation.display_name.unwrap(), "Modified Conversation");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_conversation_participants() {
|
|
||||||
let mut db = ChatDatabase::new_in_memory().unwrap();
|
|
||||||
|
|
||||||
let participants: Vec<Participant> = vec!["one".into(), "two".into()];
|
|
||||||
|
|
||||||
let guid = uuid::Uuid::new_v4().to_string();
|
|
||||||
let conversation = ConversationBuilder::new()
|
|
||||||
.guid(&guid)
|
|
||||||
.display_name("Test")
|
|
||||||
.participants(participants.clone())
|
|
||||||
.build();
|
|
||||||
|
|
||||||
db.insert_conversation(conversation).unwrap();
|
|
||||||
|
|
||||||
let read_conversation = db.get_conversation_by_guid(&guid).unwrap().unwrap();
|
|
||||||
let read_participants = read_conversation.participants;
|
|
||||||
|
|
||||||
assert_eq!(participants, read_participants);
|
|
||||||
|
|
||||||
// Try making another conversation with the same participants
|
|
||||||
let conversation = ConversationBuilder::new()
|
|
||||||
.display_name("A Different Test")
|
|
||||||
.participants(participants.clone())
|
|
||||||
.build();
|
|
||||||
|
|
||||||
db.insert_conversation(conversation).unwrap();
|
|
||||||
|
|
||||||
let read_conversation = db.get_conversation_by_guid(&guid).unwrap().unwrap();
|
|
||||||
let read_participants: Vec<Participant> = read_conversation.participants;
|
|
||||||
|
|
||||||
assert_eq!(participants, read_participants);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_all_conversations_with_participants() {
|
|
||||||
let mut db = ChatDatabase::new_in_memory().unwrap();
|
|
||||||
|
|
||||||
// Create two conversations with different participants
|
|
||||||
let participants1: Vec<Participant> = vec!["one".into(), "two".into()];
|
|
||||||
let participants2: Vec<Participant> = vec!["three".into(), "four".into()];
|
|
||||||
|
|
||||||
let guid1 = uuid::Uuid::new_v4().to_string();
|
|
||||||
let conversation1 = ConversationBuilder::new()
|
|
||||||
.guid(&guid1)
|
|
||||||
.display_name("Test 1")
|
|
||||||
.participants(participants1.clone())
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let guid2 = uuid::Uuid::new_v4().to_string();
|
|
||||||
let conversation2 = ConversationBuilder::new()
|
|
||||||
.guid(&guid2)
|
|
||||||
.display_name("Test 2")
|
|
||||||
.participants(participants2.clone())
|
|
||||||
.build();
|
|
||||||
|
|
||||||
// Insert both conversations
|
|
||||||
db.insert_conversation(conversation1).unwrap();
|
|
||||||
db.insert_conversation(conversation2).unwrap();
|
|
||||||
|
|
||||||
// Get all conversations and verify the results
|
|
||||||
let all_conversations = db.all_conversations().unwrap();
|
|
||||||
assert_eq!(all_conversations.len(), 2);
|
|
||||||
|
|
||||||
// Find and verify each conversation's participants
|
|
||||||
let conv1 = all_conversations.iter().find(|c| c.guid == guid1).unwrap();
|
|
||||||
let conv2 = all_conversations.iter().find(|c| c.guid == guid2).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(conv1.participants, participants1);
|
|
||||||
assert_eq!(conv2.participants, participants2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
117
kordophone-db/src/tests/mod.rs
Normal file
117
kordophone-db/src/tests/mod.rs
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
use crate::{
|
||||||
|
chat_database::ChatDatabase,
|
||||||
|
models::{
|
||||||
|
conversation::{Conversation, ConversationBuilder},
|
||||||
|
participant::Participant
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_database_init() {
|
||||||
|
let _ = ChatDatabase::new_in_memory().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_add_conversation() {
|
||||||
|
let mut db = ChatDatabase::new_in_memory().unwrap();
|
||||||
|
|
||||||
|
let guid = "test";
|
||||||
|
let test_conversation = Conversation::builder()
|
||||||
|
.guid(guid)
|
||||||
|
.unread_count(2)
|
||||||
|
.display_name("Test Conversation")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
db.insert_conversation(test_conversation.clone()).unwrap();
|
||||||
|
|
||||||
|
// Try to fetch with id now
|
||||||
|
let conversation = db.get_conversation_by_guid(guid).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_guid(guid).unwrap().unwrap();
|
||||||
|
assert_eq!(conversation.display_name.unwrap(), "Modified Conversation");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_conversation_participants() {
|
||||||
|
let mut db = ChatDatabase::new_in_memory().unwrap();
|
||||||
|
|
||||||
|
let participants: Vec<Participant> = vec!["one".into(), "two".into()];
|
||||||
|
|
||||||
|
let guid = uuid::Uuid::new_v4().to_string();
|
||||||
|
let conversation = ConversationBuilder::new()
|
||||||
|
.guid(&guid)
|
||||||
|
.display_name("Test")
|
||||||
|
.participants(participants.clone())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
db.insert_conversation(conversation).unwrap();
|
||||||
|
|
||||||
|
let read_conversation = db.get_conversation_by_guid(&guid).unwrap().unwrap();
|
||||||
|
let read_participants = read_conversation.participants;
|
||||||
|
|
||||||
|
assert_eq!(participants, read_participants);
|
||||||
|
|
||||||
|
// Try making another conversation with the same participants
|
||||||
|
let conversation = ConversationBuilder::new()
|
||||||
|
.display_name("A Different Test")
|
||||||
|
.participants(participants.clone())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
db.insert_conversation(conversation).unwrap();
|
||||||
|
|
||||||
|
let read_conversation = db.get_conversation_by_guid(&guid).unwrap().unwrap();
|
||||||
|
let read_participants: Vec<Participant> = read_conversation.participants;
|
||||||
|
|
||||||
|
assert_eq!(participants, read_participants);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_all_conversations_with_participants() {
|
||||||
|
let mut db = ChatDatabase::new_in_memory().unwrap();
|
||||||
|
|
||||||
|
// Create two conversations with different participants
|
||||||
|
let participants1: Vec<Participant> = vec!["one".into(), "two".into()];
|
||||||
|
let participants2: Vec<Participant> = vec!["three".into(), "four".into()];
|
||||||
|
|
||||||
|
let guid1 = uuid::Uuid::new_v4().to_string();
|
||||||
|
let conversation1 = ConversationBuilder::new()
|
||||||
|
.guid(&guid1)
|
||||||
|
.display_name("Test 1")
|
||||||
|
.participants(participants1.clone())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let guid2 = uuid::Uuid::new_v4().to_string();
|
||||||
|
let conversation2 = ConversationBuilder::new()
|
||||||
|
.guid(&guid2)
|
||||||
|
.display_name("Test 2")
|
||||||
|
.participants(participants2.clone())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// Insert both conversations
|
||||||
|
db.insert_conversation(conversation1).unwrap();
|
||||||
|
db.insert_conversation(conversation2).unwrap();
|
||||||
|
|
||||||
|
// Get all conversations and verify the results
|
||||||
|
let all_conversations = db.all_conversations().unwrap();
|
||||||
|
assert_eq!(all_conversations.len(), 2);
|
||||||
|
|
||||||
|
// Find and verify each conversation's participants
|
||||||
|
let conv1 = all_conversations.iter().find(|c| c.guid == guid1).unwrap();
|
||||||
|
let conv2 = all_conversations.iter().find(|c| c.guid == guid2).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(conv1.participants, participants1);
|
||||||
|
assert_eq!(conv2.participants, participants2);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user