#![allow(dead_code)]
use chrono::NaiveDate;
use rustodo::models::{Priority, Recurrence, Task};
use rustodo::storage::Storage;
use rustodo::storage::memory::InMemoryStorage;
pub struct TestEnv {
storage: InMemoryStorage,
}
impl TestEnv {
pub fn new() -> Self {
Self {
storage: InMemoryStorage::default(),
}
}
pub fn with_tasks(tasks: Vec<Task>) -> Self {
Self {
storage: InMemoryStorage::with_tasks(tasks),
}
}
pub fn storage(&self) -> &InMemoryStorage {
&self.storage
}
pub fn load_tasks(&self) -> Vec<Task> {
self.storage
.load()
.expect("Failed to load tasks in test")
.into_iter()
.filter(|t| !t.is_deleted())
.collect()
}
pub fn load_all_tasks(&self) -> Vec<Task> {
self.storage.load().expect("Failed to load tasks in test")
}
pub fn save_tasks(&self, tasks: &[Task]) {
self.storage
.save(tasks)
.expect("Failed to save tasks in test")
}
pub fn task_count(&self) -> usize {
self.storage
.load()
.expect("Failed to load tasks in test")
.iter()
.filter(|t| !t.is_deleted())
.count()
}
pub fn raw_task_count(&self) -> usize {
self.storage.len()
}
pub fn is_empty(&self) -> bool {
self.task_count() == 0
}
}
impl Default for TestEnv {
fn default() -> Self {
Self::new()
}
}
pub fn simple_task(text: &str) -> Task {
Task::new(text.to_string(), Priority::Medium, vec![], None, None, None)
}
pub fn task_with_priority(text: &str, priority: Priority) -> Task {
Task::new(text.to_string(), priority, vec![], None, None, None)
}
pub fn task_with_tags(text: &str, tags: Vec<&str>) -> Task {
Task::new(
text.to_string(),
Priority::Medium,
tags.into_iter().map(|s| s.to_string()).collect(),
None,
None,
None,
)
}
pub fn task_with_due(text: &str, due_date: NaiveDate) -> Task {
Task::new(
text.to_string(),
Priority::Medium,
vec![],
None,
Some(due_date),
None,
)
}
pub fn recurring_task(text: &str, due_date: NaiveDate, recurrence: Recurrence) -> Task {
Task::new(
text.to_string(),
Priority::Medium,
vec![],
None,
Some(due_date),
Some(recurrence),
)
}
pub fn custom_task(
text: &str,
priority: Priority,
tags: Vec<&str>,
due_date: Option<NaiveDate>,
recurrence: Option<Recurrence>,
) -> Task {
Task::new(
text.to_string(),
priority,
tags.into_iter().map(|s| s.to_string()).collect(),
None,
due_date,
recurrence,
)
}
pub fn today() -> NaiveDate {
chrono::Local::now().naive_local().date()
}
pub fn yesterday() -> NaiveDate {
today() - chrono::Duration::days(1)
}
pub fn tomorrow() -> NaiveDate {
today() + chrono::Duration::days(1)
}
pub fn days_from_now(days: i64) -> NaiveDate {
today() + chrono::Duration::days(days)
}
pub fn days_ago(days: i64) -> NaiveDate {
today() - chrono::Duration::days(days)
}