Skip to content

Commit 110e234

Browse files
committed
A palpable test
Signed-off-by: itowlson <[email protected]>
1 parent 82fcbd1 commit 110e234

File tree

7 files changed

+96
-1
lines changed

7 files changed

+96
-1
lines changed

crates/key-value/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl wasi_keyvalue::store::HostBucket for KeyValueDispatch {
218218
cursor: Option<u64>,
219219
) -> anyhow::Result<Result<wasi_keyvalue::store::KeyResponse, wasi_keyvalue::store::Error>>
220220
{
221-
if cursor.is_some() {
221+
if cursor.unwrap_or_default() != 0 {
222222
anyhow::bail!("list_keys: cursor not supported");
223223
}
224224

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
spin_manifest_version = 2
2+
3+
[application]
4+
name = "key-value-wasi"
5+
authors = ["Fermyon Engineering <[email protected]>"]
6+
version = "0.1.0"
7+
8+
[[trigger.http]]
9+
route = "/"
10+
component = "test"
11+
12+
[component.test]
13+
source = "%{source=key-value-wasi}"
14+
key_value_stores = ["default"]

tests/test-components/components/Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "key-value-wasi"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["cdylib"]
8+
9+
[dependencies]
10+
helper = { path = "../../helper" }
11+
wit-bindgen = "0.16.0"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Key Value
2+
3+
Tests the key/value interface.
4+
5+
## Expectations
6+
7+
This test component expects the following to be true:
8+
* It is given permission to open a connection to the "default" store.
9+
* It does not have permission to access a store named "forbidden".
10+
* It is empty
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use helper::{ensure_matches, ensure_ok};
2+
3+
use bindings::wasi::keyvalue::store::{Error, open, KeyResponse};
4+
5+
helper::define_component!(Component);
6+
7+
impl Component {
8+
fn main() -> Result<(), String> {
9+
10+
ensure_matches!(open("forbidden"), Err(Error::AccessDenied));
11+
12+
let store = ensure_ok!(open("default"));
13+
14+
// Ensure nothing set in `bar` key
15+
ensure_ok!(store.delete("bar"));
16+
ensure_matches!(store.exists("bar"), Ok(false));
17+
ensure_matches!(store.get("bar"), Ok(None));
18+
ensure_matches!(keys(&store.list_keys(None)), Ok(&[]));
19+
20+
// Set `bar` key
21+
ensure_ok!(store.set("bar", b"baz"));
22+
ensure_matches!(store.exists("bar"), Ok(true));
23+
ensure_matches!(store.get("bar"), Ok(Some(v)) if v == b"baz");
24+
ensure_matches!(keys(&store.list_keys(None)), Ok([bar]) if bar == "bar");
25+
ensure_matches!(keys(&store.list_keys(Some(0))), Ok([bar]) if bar == "bar");
26+
27+
// Override `bar` key
28+
ensure_ok!(store.set("bar", b"wow"));
29+
ensure_matches!(store.exists("bar"), Ok(true));
30+
ensure_matches!(store.get("bar"), Ok(Some(wow)) if wow == b"wow");
31+
ensure_matches!(keys(&store.list_keys(None)), Ok([bar]) if bar == "bar");
32+
33+
// Set another key
34+
ensure_ok!(store.set("qux", b"yay"));
35+
ensure_matches!(keys(&store.list_keys(None)), Ok(c) if c.len() == 2 && c.contains(&"bar".into()) && c.contains(&"qux".into()));
36+
37+
// Delete everything
38+
ensure_ok!(store.delete("bar"));
39+
ensure_ok!(store.delete("bar"));
40+
ensure_ok!(store.delete("qux"));
41+
ensure_matches!(store.exists("bar"), Ok(false));
42+
ensure_matches!(store.get("qux"), Ok(None));
43+
ensure_matches!(keys(&store.list_keys(None)), Ok(&[]));
44+
45+
Ok(())
46+
}
47+
}
48+
49+
fn keys<E>(res: &Result<KeyResponse, E>) -> Result<&[String], &E> {
50+
res.as_ref().map(|kr| kr.keys.as_slice())
51+
}

wit/world.wit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ world platform {
3131
world platform-rc20231018 {
3232
include wasi:cli/reactor@0.2.0-rc-2023-10-18;
3333
import wasi:http/outgoing-handler@0.2.0-rc-2023-10-18;
34+
import wasi:keyvalue/store@0.2.0-draft;
3435
import llm;
3536
import redis;
3637
import mqtt;

0 commit comments

Comments
 (0)