Skip to content

Commit ed2cfa3

Browse files
committed
A palpable test
Signed-off-by: itowlson <[email protected]>
1 parent 581d890 commit ed2cfa3

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
26+
// Override `bar` key
27+
ensure_ok!(store.set("bar", b"wow"));
28+
ensure_matches!(store.exists("bar"), Ok(true));
29+
ensure_matches!(store.get("bar"), Ok(Some(wow)) if wow == b"wow");
30+
ensure_matches!(keys(&store.list_keys(None)), Ok([bar]) if bar == "bar");
31+
32+
// Set another key
33+
ensure_ok!(store.set("qux", b"yay"));
34+
ensure_matches!(keys(&store.list_keys(None)), Ok(c) if c.len() == 2 && c.contains(&"bar".into()) && c.contains(&"qux".into()));
35+
36+
// Delete everything
37+
ensure_ok!(store.delete("bar"));
38+
ensure_ok!(store.delete("bar"));
39+
ensure_ok!(store.delete("qux"));
40+
ensure_matches!(store.exists("bar"), Ok(false));
41+
ensure_matches!(store.get("qux"), Ok(None));
42+
ensure_matches!(keys(&store.list_keys(None)), Ok(&[]));
43+
44+
Ok(())
45+
}
46+
}
47+
48+
fn keys<E>(res: &Result<KeyResponse, E>) -> Result<&[String], &E> {
49+
res.as_ref().map(|kr| kr.keys.as_slice())
50+
}

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)