#[cfg(feature = "postgres")]
mod postgres_tests {
use crate::fixtures::get_or_init_postgres_fixture;
use cloacina::context::Context;
use cloacina::dal::DAL;
use serial_test::serial;
use tracing::debug;
#[tokio::test]
#[serial]
async fn test_context_db_operations() {
let fixture = get_or_init_postgres_fixture().await;
let mut fixture = fixture.lock().unwrap_or_else(|e| e.into_inner());
fixture.initialize().await;
let database = fixture.get_database();
let dal = DAL::new(database);
let mut context = Context::<i32>::new();
context.insert("test_key", 42).unwrap();
context.insert("another_key", 100).unwrap();
let context_id = dal
.context()
.create(&context)
.await
.unwrap()
.expect("Context should not be empty");
debug!("Created context with id: {:?}", context_id);
let loaded_context: Context<i32> = dal.context().read(context_id).await.unwrap();
assert_eq!(loaded_context.get("test_key"), Some(&42));
assert_eq!(loaded_context.get("another_key"), Some(&100));
let mut updated_context = loaded_context.clone_data();
updated_context.update("test_key", 43).unwrap();
dal.context()
.update(context_id, &updated_context)
.await
.unwrap();
let final_context: Context<i32> = dal.context().read(context_id).await.unwrap();
assert_eq!(final_context.get("test_key"), Some(&43));
assert_eq!(final_context.get("another_key"), Some(&100));
dal.context().delete(context_id).await.unwrap();
}
}
#[cfg(feature = "sqlite")]
mod sqlite_tests {
use crate::fixtures::get_or_init_sqlite_fixture;
use cloacina::context::Context;
use cloacina::dal::DAL;
use serial_test::serial;
use tracing::debug;
#[tokio::test]
#[serial]
async fn test_context_db_operations() {
let fixture = get_or_init_sqlite_fixture().await;
let mut fixture = fixture.lock().unwrap_or_else(|e| e.into_inner());
fixture.initialize().await;
let database = fixture.get_database();
let dal = DAL::new(database);
let mut context = Context::<i32>::new();
context.insert("test_key", 42).unwrap();
context.insert("another_key", 100).unwrap();
let context_id = dal
.context()
.create(&context)
.await
.unwrap()
.expect("Context should not be empty");
debug!("Created context with id: {:?} (SQLite)", context_id);
let loaded_context: Context<i32> = dal.context().read(context_id).await.unwrap();
assert_eq!(loaded_context.get("test_key"), Some(&42));
assert_eq!(loaded_context.get("another_key"), Some(&100));
let mut updated_context = loaded_context.clone_data();
updated_context.update("test_key", 43).unwrap();
dal.context()
.update(context_id, &updated_context)
.await
.unwrap();
let final_context: Context<i32> = dal.context().read(context_id).await.unwrap();
assert_eq!(final_context.get("test_key"), Some(&43));
assert_eq!(final_context.get("another_key"), Some(&100));
dal.context().delete(context_id).await.unwrap();
}
}