use std::sync::Arc;
use tempfile::NamedTempFile;
use things3_core::{CacheConfig, ThingsCache, ThingsDatabase};
#[cfg(any(feature = "export-csv", feature = "export-opml"))]
use things3_core::{DataExporter, ExportData, ExportFormat};
#[cfg(feature = "test-utils")]
use things3_core::test_utils::{create_mock_tasks, create_test_database};
#[tokio::test]
#[cfg(feature = "test-utils")]
async fn memory_test_database_connection() {
let temp_file = NamedTempFile::new().unwrap();
let db_path = temp_file.path();
create_test_database(db_path).await.unwrap();
for _ in 0..10 {
let _db = ThingsDatabase::new(db_path).await.unwrap();
}
}
#[tokio::test]
#[cfg(feature = "test-utils")]
async fn memory_test_large_query_results() {
let temp_file = NamedTempFile::new().unwrap();
let db_path = temp_file.path();
create_test_database(db_path).await.unwrap();
let db = ThingsDatabase::new(db_path).await.unwrap();
let _inbox = db.get_inbox(Some(1000)).await.unwrap();
let _today = db.get_today(Some(1000)).await.unwrap();
let _projects = db.get_projects(Some(1000)).await.unwrap();
}
#[tokio::test]
async fn memory_test_cache_operations() {
let config = CacheConfig {
max_capacity: 1000,
..Default::default()
};
let _cache = ThingsCache::new(&config);
}
#[tokio::test]
async fn memory_test_multiple_caches() {
let config = CacheConfig::default();
let mut caches = Vec::new();
for _ in 0..10 {
caches.push(ThingsCache::new(&config));
}
assert_eq!(caches.len(), 10);
drop(caches);
}
#[tokio::test]
#[cfg(feature = "test-utils")]
async fn memory_test_repeated_operations() {
let temp_file = NamedTempFile::new().unwrap();
let db_path = temp_file.path();
create_test_database(db_path).await.unwrap();
let db = ThingsDatabase::new(db_path).await.unwrap();
for _ in 0..100 {
let _inbox = db.get_inbox(Some(10)).await.unwrap();
}
}
#[test]
#[cfg(all(
feature = "test-utils",
any(feature = "export-csv", feature = "export-opml")
))]
fn memory_test_export_operations() {
let tasks = create_mock_tasks();
let exporter = DataExporter::new_default();
for _ in 0..10 {
let data = ExportData::new(tasks.clone(), vec![], vec![]);
let _json = exporter.export(&data, ExportFormat::Json).unwrap();
#[cfg(feature = "export-csv")]
{
let _csv = exporter.export(&data, ExportFormat::Csv).unwrap();
}
let _markdown = exporter.export(&data, ExportFormat::Markdown).unwrap();
}
}
#[tokio::test]
#[cfg(feature = "test-utils")]
async fn memory_test_arc_wrapped_database() {
let temp_file = NamedTempFile::new().unwrap();
let db_path = temp_file.path();
create_test_database(db_path).await.unwrap();
let db = Arc::new(ThingsDatabase::new(db_path).await.unwrap());
let mut handles = Vec::new();
for _ in 0..10 {
let db_clone = Arc::clone(&db);
handles.push(db_clone);
}
assert_eq!(Arc::strong_count(&db), 11);
drop(handles);
assert_eq!(Arc::strong_count(&db), 1);
}
#[tokio::test]
#[cfg(feature = "test-utils")]
async fn memory_test_health_checks() {
let temp_file = NamedTempFile::new().unwrap();
let db_path = temp_file.path();
create_test_database(db_path).await.unwrap();
let db = ThingsDatabase::new(db_path).await.unwrap();
for _ in 0..100 {
let _is_connected = db.is_connected().await;
}
}
#[tokio::test]
async fn memory_test_cache_stats() {
let cache = ThingsCache::new_default();
for _ in 0..1000 {
let _stats = cache.get_stats();
}
}
#[tokio::test]
#[cfg(feature = "test-utils")]
async fn memory_test_search_operations() {
let temp_file = NamedTempFile::new().unwrap();
let db_path = temp_file.path();
create_test_database(db_path).await.unwrap();
let db = ThingsDatabase::new(db_path).await.unwrap();
let queries = vec!["test", "project", "task", "important"];
for query in queries {
for _ in 0..10 {
let _results = db.search_tasks(query).await.unwrap();
}
}
}
#[tokio::test]
async fn memory_test_error_cleanup() {
let nonexistent_path = std::path::PathBuf::from("/nonexistent/path/to/database.db");
for _ in 0..10 {
let _result = ThingsDatabase::new(&nonexistent_path).await;
}
}
#[tokio::test]
#[cfg(feature = "test-utils")]
async fn memory_test_database_pool() {
let temp_file = NamedTempFile::new().unwrap();
let db_path = temp_file.path();
create_test_database(db_path).await.unwrap();
let db = ThingsDatabase::new(db_path).await.unwrap();
let mut tasks = Vec::new();
for _ in 0..5 {
let db_clone = db.clone();
tasks.push(tokio::spawn(
async move { db_clone.get_inbox(Some(10)).await },
));
}
for task in tasks {
let _ = task.await.unwrap();
}
}