Skip to content

Commit

Permalink
fix: single type
Browse files Browse the repository at this point in the history
  • Loading branch information
dbcfd committed Feb 6, 2024
1 parent d39ed04 commit d1664c4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ publish = false
[dependencies]
anyhow = "1"
#ceramic-event = { git = "https://github.com/ceramicnetwork/rust-ceramic", branch = "main" }
#ceramic-event = { path = "/Users/dbrowning/code/3box/rust-ceramic/event", default-features = false }
ceramic-event = { git = "https://github.com/ceramicnetwork/rust-ceramic", branch = "feat/wasi" }
ceramic-event = { path = "/Users/dbrowning/code/3box/rust-ceramic/event", default-features = false }
#ceramic-event = { git = "https://github.com/ceramicnetwork/rust-ceramic", branch = "feat/wasi" }
json-patch = { version = "1.2.0", features = ["diff"] }
reqwest = { version = "0.11.14", features = ["json"], optional = true }
schemars = "0.8.12"
Expand Down
7 changes: 2 additions & 5 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use crate::query::FilterQuery;
use ceramic_event::{
Base64String, Base64UrlString, Jws, MultiBase32String, MultiBase36String, StreamId,
StreamIdType,
};
use ceramic_event::{Base64String, Base64UrlString, Jws, MultiBase32String, MultiBase36String, StreamId, StreamIdType};
use serde::{Deserialize, Serialize};
use serde_json::Value;

Expand All @@ -25,7 +22,7 @@ pub struct BlockData<T: Serialize> {
/// Header for block
pub header: BlockHeader,
/// Data for block
#[serde(skip_serializing_if = "Option::is_none")]
//#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<T>,
/// Signature for block
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
54 changes: 41 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ pub mod api;
mod model_definition;
mod query;

use ceramic_event::{
Base64String, Cid, DagCborEncoded, EventArgs, Jws, MultiBase36String, Signer, StreamId,
StreamIdType,
};
use ceramic_event::{Base64String, Cid, DeterministicInitEvent, EventArgs, Jws, MultiBase36String, Signer, StreamId, StreamIdType};
use serde::Serialize;
use std::str::FromStr;

Expand Down Expand Up @@ -147,12 +144,12 @@ impl<S: Signer> CeramicHttpClient<S> {
pub async fn create_single_instance_request(
&self,
model_id: &StreamId,
) -> anyhow::Result<api::CreateRequest<DagCborEncoded>> {
) -> anyhow::Result<api::CreateRequest<DeterministicInitEvent>> {
if !model_id.is_model() {
anyhow::bail!("StreamId was not a model");
}
let args = EventArgs::new_with_parent(&self.signer, model_id);
let commit = args.init()?;
let _commit = args.init()?;
let controllers: Vec<_> = args.controllers().map(|c| c.id.clone()).collect();
let model = Base64String::from(model_id.to_vec()?);
Ok(api::CreateRequest {
Expand All @@ -165,7 +162,7 @@ impl<S: Signer> CeramicHttpClient<S> {
},
linked_block: None,
jws: None,
data: Some(commit.encoded),
data: None,
cacao_block: None,
},
})
Expand Down Expand Up @@ -630,19 +627,50 @@ pub mod tests {
cli.create_model(&model).await.unwrap()
}

pub async fn create_single_model(cli: &CeramicRemoteHttpClient<JwkSigner>) -> StreamId {
let model = ModelDefinition::new::<Ball>("TestBall", ModelAccountRelation::Single).unwrap();
cli.create_model(&model).await.unwrap()
}

#[tokio::test]
async fn should_create_model() {
let ceramic = CeramicRemoteHttpClient::new(signer().await, ceramic_url());
let model = ModelDefinition::new::<Ball>("TestBall", ModelAccountRelation::List).unwrap();
ceramic.create_model(&model).await.unwrap();
}

// #[tokio::test]
// async fn should_create_single_instance() {
// let ceramic = CeramicRemoteHttpClient::new(did(), &did_private_key(), ceramic_url());
// let model = create_model(&ceramic).await;
// ceramic.create_single_instance(&model).await.unwrap();
// }
#[tokio::test]
async fn should_create_single_instance() {
let ceramic = CeramicRemoteHttpClient::new(signer().await, ceramic_url());
let model = create_single_model(&ceramic).await;
ceramic.create_single_instance(&model).await.unwrap();
}

#[tokio::test]
async fn should_create_and_update_single_instance() {
let ceramic = CeramicRemoteHttpClient::new(signer().await, ceramic_url());
let model = create_single_model(&ceramic).await;
let stream_id = ceramic.create_single_instance(&model).await.unwrap();

tokio::time::sleep(Duration::from_secs(1)).await;

let patch = json_patch::Patch(vec![json_patch::PatchOperation::Replace(
ReplaceOperation {
path: "/".to_string(),
value: serde_json::json!({
"creator": ceramic.client().signer().id().id.clone(),
"radius": 1,
"red": 2,
"green": 3,
"blue": 4,
}),
},
)]);
let post_resp = ceramic.update(&model, &stream_id, patch).await.unwrap();
assert_eq!(post_resp.stream_id, stream_id);
let post_resp: Ball = serde_json::from_value(post_resp.state.unwrap().content).unwrap();
assert_eq!(post_resp.red, 5);
}

#[tokio::test]
async fn should_create_and_update_list() {
Expand Down

0 comments on commit d1664c4

Please sign in to comment.