use p2panda_core::test_utils::TestLog;
use p2panda_core::{Hash, Operation, Topic};
use crate::operations::OperationStore;
use crate::{SqliteStore, Transaction};
#[tokio::test]
async fn insert_get_delete_operations() {
let store = SqliteStore::temporary().await;
let log = TestLog::new();
let operation_1 = log.operation::<()>(b"hey", ());
let operation_2 = log.operation::<()>(b"ho", ());
let operation_3 = log.operation::<()>(b"let's", ());
let operation_4 = log.operation::<()>(b"go!", ());
let permit = store.begin().await.unwrap();
assert!(
store
.insert_operation(&operation_1.hash, &operation_1, &log.id())
.await
.unwrap()
);
assert!(
!store
.insert_operation(&operation_1.hash, &operation_1, &log.id())
.await
.unwrap()
);
assert!(
store
.insert_operation(&operation_3.hash, &operation_3, &log.id())
.await
.unwrap()
);
assert!(
store
.insert_operation(&operation_4.hash, &operation_4, &log.id())
.await
.unwrap()
);
store.commit(permit).await.unwrap();
assert!(
OperationStore::<Operation<()>, Hash, Topic>::has_operation(&store, &operation_1.hash)
.await
.unwrap()
);
assert!(
!OperationStore::<Operation<()>, Hash, Topic>::has_operation(&store, &operation_2.hash)
.await
.unwrap()
);
assert_eq!(
OperationStore::<Operation<()>, Hash, Topic>::get_operation(&store, &operation_4.hash)
.await
.unwrap(),
Some(operation_4.clone())
);
assert_eq!(
OperationStore::<Operation<()>, Hash, Topic>::get_operation(&store, &operation_2.hash)
.await
.unwrap(),
None
);
let permit = store.begin().await.unwrap();
assert!(
OperationStore::<Operation<()>, Hash, Topic>::delete_operation(&store, &operation_4.hash)
.await
.unwrap(),
);
assert!(
!OperationStore::<Operation<()>, Hash, Topic>::delete_operation(&store, &operation_4.hash)
.await
.unwrap(),
);
store.commit(permit).await.unwrap();
}