use std::borrow::Cow;
use crate::db::Db;
use crate::{Event, EventStatus, LooseDateTime, Priority, Todo, TodoStatus};
pub async fn setup_test_db() -> Db {
Db::open(None)
.await
.expect("Failed to create test database")
}
#[derive(Debug, Clone)]
pub struct TestEvent {
pub uid: String,
pub summary: String,
pub description: Option<String>,
pub start: Option<LooseDateTime>,
pub end: Option<LooseDateTime>,
pub status: Option<EventStatus>,
}
impl TestEvent {
pub fn new(uid: impl Into<String>, summary: impl Into<String>) -> Self {
Self {
uid: uid.into(),
summary: summary.into(),
description: None,
start: None,
end: None,
status: None,
}
}
pub fn with_description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
pub fn with_start(mut self, start: LooseDateTime) -> Self {
self.start = Some(start);
self
}
pub fn with_end(mut self, end: LooseDateTime) -> Self {
self.end = Some(end);
self
}
pub fn with_status(mut self, status: EventStatus) -> Self {
self.status = Some(status);
self
}
}
impl Event for TestEvent {
fn uid(&self) -> Cow<'_, str> {
Cow::Borrowed(&self.uid)
}
fn summary(&self) -> Cow<'_, str> {
Cow::Borrowed(&self.summary)
}
fn description(&self) -> Option<Cow<'_, str>> {
self.description.as_deref().map(Cow::Borrowed)
}
fn start(&self) -> Option<LooseDateTime> {
self.start.clone()
}
fn end(&self) -> Option<LooseDateTime> {
self.end.clone()
}
fn status(&self) -> Option<EventStatus> {
self.status
}
}
pub fn test_event(uid: impl Into<String>, summary: impl Into<String>) -> TestEvent {
TestEvent::new(uid, summary)
}
#[derive(Debug, Clone)]
pub struct TestTodo {
pub uid: String,
pub summary: String,
pub description: Option<String>,
pub due: Option<LooseDateTime>,
pub completed: Option<jiff::Zoned>,
pub percent_complete: Option<u8>,
pub priority: Priority,
pub status: TodoStatus,
}
impl TestTodo {
pub fn new(uid: impl Into<String>, summary: impl Into<String>) -> Self {
Self {
uid: uid.into(),
summary: summary.into(),
description: None,
due: None,
completed: None,
percent_complete: None,
priority: Priority::default(),
status: TodoStatus::default(),
}
}
pub fn with_description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
pub fn with_due(mut self, due: LooseDateTime) -> Self {
self.due = Some(due);
self
}
#[allow(dead_code)]
pub fn with_completed(mut self, completed: jiff::Zoned) -> Self {
self.completed = Some(completed);
self
}
pub fn with_percent_complete(mut self, percent: u8) -> Self {
self.percent_complete = Some(percent);
self
}
pub fn with_priority(mut self, priority: Priority) -> Self {
self.priority = priority;
self
}
pub fn with_status(mut self, status: TodoStatus) -> Self {
self.status = status;
self
}
}
impl Todo for TestTodo {
fn uid(&self) -> Cow<'_, str> {
Cow::Borrowed(&self.uid)
}
fn summary(&self) -> Cow<'_, str> {
Cow::Borrowed(&self.summary)
}
fn description(&self) -> Option<Cow<'_, str>> {
self.description.as_deref().map(Cow::Borrowed)
}
fn due(&self) -> Option<LooseDateTime> {
self.due.clone()
}
fn completed(&self) -> Option<jiff::Zoned> {
self.completed.clone()
}
fn percent_complete(&self) -> Option<u8> {
self.percent_complete
}
fn priority(&self) -> Priority {
self.priority
}
fn status(&self) -> TodoStatus {
self.status
}
}
pub fn test_todo(uid: impl Into<String>, summary: impl Into<String>) -> TestTodo {
TestTodo::new(uid, summary)
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn tests_utils_setup_creates_in_memory_database() {
let db = setup_test_db().await;
assert!(db.events.get("nonexistent").await.unwrap().is_none());
assert!(db.todos.get("nonexistent").await.unwrap().is_none());
}
#[test]
fn test_event_implements_event_trait() {
let event = test_event("test-uid", "Test Summary")
.with_description("Test description")
.with_status(EventStatus::Confirmed);
assert_eq!(event.uid(), "test-uid");
assert_eq!(event.summary(), "Test Summary");
assert_eq!(event.description(), Some(Cow::Borrowed("Test description")));
assert_eq!(event.status(), Some(EventStatus::Confirmed));
}
#[test]
fn test_todo_implements_todo_trait() {
let todo = test_todo("test-uid", "Test Summary")
.with_description("Test description")
.with_priority(Priority::P2)
.with_status(TodoStatus::InProcess);
assert_eq!(todo.uid(), "test-uid");
assert_eq!(todo.summary(), "Test Summary");
assert_eq!(todo.description(), Some(Cow::Borrowed("Test description")));
assert_eq!(todo.priority(), Priority::P2);
assert_eq!(todo.status(), TodoStatus::InProcess);
}
}