Skip to content

Commit

Permalink
Fix key-value test
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Levick <[email protected]>
  • Loading branch information
rylev committed Oct 5, 2023
1 parent 91f0c1f commit 77bff00
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
58 changes: 42 additions & 16 deletions crates/key-value-sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ impl Store for SqliteStore {
#[cfg(test)]
mod test {
use super::*;
use spin_core::wasmtime::component::Resource;
use spin_key_value::{DelegatingStoreManager, KeyValueDispatch};
use spin_world::key_value::Host;
use spin_world::key_value::HostStore;

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn all() -> Result<()> {
Expand All @@ -162,7 +163,7 @@ mod test {
);

assert!(matches!(
kv.exists(42, "bar".to_owned()).await?,
kv.exists(Resource::new_own(42), "bar".to_owned()).await?,
Err(Error::InvalidStore)
));

Expand All @@ -176,41 +177,66 @@ mod test {
));

let store = kv.open("default".to_owned()).await??;
let rep = store.rep();

assert!(!kv.exists(store, "bar".to_owned()).await??);
assert!(
!kv.exists(Resource::new_own(rep), "bar".to_owned())
.await??
);

assert!(matches!(
kv.get(store, "bar".to_owned()).await?,
kv.get(Resource::new_own(rep), "bar".to_owned()).await?,
Err(Error::NoSuchKey)
));

kv.set(store, "bar".to_owned(), b"baz".to_vec()).await??;
kv.set(Resource::new_own(rep), "bar".to_owned(), b"baz".to_vec())
.await??;

assert!(kv.exists(store, "bar".to_owned()).await??);
assert!(
kv.exists(Resource::new_own(rep), "bar".to_owned())
.await??
);

assert_eq!(b"baz" as &[_], &kv.get(store, "bar".to_owned()).await??);
assert_eq!(
b"baz" as &[_],
&kv.get(Resource::new_own(rep), "bar".to_owned()).await??
);

kv.set(store, "bar".to_owned(), b"wow".to_vec()).await??;
kv.set(Resource::new_own(rep), "bar".to_owned(), b"wow".to_vec())
.await??;

assert_eq!(b"wow" as &[_], &kv.get(store, "bar".to_owned()).await??);
assert_eq!(
b"wow" as &[_],
&kv.get(Resource::new_own(rep), "bar".to_owned()).await??
);

assert_eq!(&["bar".to_owned()] as &[_], &kv.get_keys(store).await??);
assert_eq!(
&["bar".to_owned()] as &[_],
&kv.get_keys(Resource::new_own(rep)).await??
);

kv.delete(store, "bar".to_owned()).await??;
kv.delete(Resource::new_own(rep), "bar".to_owned())
.await??;

assert!(!kv.exists(store, "bar".to_owned()).await??);
assert!(
!kv.exists(Resource::new_own(rep), "bar".to_owned())
.await??
);

assert_eq!(&[] as &[String], &kv.get_keys(store).await??);
assert_eq!(
&[] as &[String],
&kv.get_keys(Resource::new_own(rep)).await??
);

assert!(matches!(
kv.get(store, "bar".to_owned()).await?,
kv.get(Resource::new_own(rep), "bar".to_owned()).await?,
Err(Error::NoSuchKey)
));

kv.close(store).await?;
kv.drop(Resource::new_own(rep))?;

assert!(matches!(
kv.exists(store, "bar".to_owned()).await?,
kv.exists(Resource::new_own(rep), "bar".to_owned()).await?,
Err(Error::InvalidStore)
));

Expand Down
1 change: 1 addition & 0 deletions crates/key-value/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ impl Default for KeyValueDispatch {

#[async_trait]
impl key_value::Host for KeyValueDispatch {}

#[async_trait]
impl key_value::HostStore for KeyValueDispatch {
async fn open(&mut self, name: String) -> Result<Result<Resource<key_value::Store>, Error>> {
Expand Down

0 comments on commit 77bff00

Please sign in to comment.