use std::borrow::Borrow;
use p2panda_core::Hash;
use serde::{Deserialize, Serialize};
use crate::spaces::{SpacesMessageStore, SpacesStore};
use crate::{SqliteStore, tx_unwrap};
#[derive(Clone, Debug, Serialize, Deserialize)]
struct SpacesArgs;
type SpacesMessage = crate::spaces::SpacesMessage<SpacesArgs>;
#[derive(Clone, Debug, Serialize, Deserialize)]
struct SpacesExtensions {
args: SpacesArgs,
}
impl Borrow<SpacesArgs> for SpacesExtensions {
fn borrow(&self) -> &SpacesArgs {
&self.args
}
}
type SqliteSpacesStore = crate::spaces::SqliteSpacesStore<SpacesExtensions>;
type SpaceState = String;
#[tokio::test]
async fn verify_generics() {
let inner = SqliteStore::temporary().await;
let store = SqliteSpacesStore::new(inner);
let id = Hash::from_bytes([0; 32]);
let message: Option<SpacesMessage> = store.get_spaces_message(&id).await.unwrap();
assert!(message.is_none());
}
#[tokio::test]
async fn get_set_spaces_state() {
let inner = SqliteStore::temporary().await;
let store = SqliteSpacesStore::new(inner);
let space_state = String::from("Some important state");
let space_id = Hash::digest(b"test");
tx_unwrap!(store, {
store.set_space_state_tx(&space_id, &space_state).await
})
.unwrap();
let y: Option<SpaceState> =
tx_unwrap!(store, { store.get_space_state_tx(&space_id).await }).unwrap();
assert!(y.is_some());
let y = y.unwrap();
assert_eq!(y, space_state);
let has_space = <SqliteSpacesStore as SpacesStore<SpaceState>>::has_space(&store, &space_id)
.await
.unwrap();
assert!(has_space);
let non_existent_space_id = Hash::digest(b"nonono");
let y: Option<SpaceState> = tx_unwrap!(store, {
store.get_space_state_tx(&non_existent_space_id).await
})
.unwrap();
assert!(y.is_none());
let has_space =
<SqliteSpacesStore as SpacesStore<SpaceState>>::has_space(&store, &non_existent_space_id)
.await
.unwrap();
assert!(!has_space);
}
#[tokio::test]
async fn get_space_ids() {
let inner = SqliteStore::temporary().await;
let store = SqliteSpacesStore::new(inner);
let space_id_0 = Hash::digest(b"0");
let space_id_1 = Hash::digest(b"1");
let space_id_2 = Hash::digest(b"2");
let space_state = String::from("Some important state");
tx_unwrap!(store, {
store.set_space_state_tx(&space_id_0, &space_state).await
})
.unwrap();
tx_unwrap!(store, {
store.set_space_state_tx(&space_id_1, &space_state).await
})
.unwrap();
tx_unwrap!(store, {
store.set_space_state_tx(&space_id_2, &space_state).await
})
.unwrap();
let space_ids: Vec<Hash> = <SqliteSpacesStore as SpacesStore<SpaceState>>::space_ids(&store)
.await
.unwrap();
assert_eq!(space_ids.len(), 3);
assert!(space_ids.contains(&space_id_0));
assert!(space_ids.contains(&space_id_1));
assert!(space_ids.contains(&space_id_2));
}