Skip to content

Commit

Permalink
feat(sails, lient-gen): Support mocking generated client (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
vobradovich authored Jul 25, 2024
1 parent 87fb50a commit f97efdd
Show file tree
Hide file tree
Showing 31 changed files with 553 additions and 256 deletions.
85 changes: 83 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ itertools = "0.12"
lalrpop = { version = "0.20", default-features = false }
lalrpop-util = "0.20"
logos = "0.13"
mockall = "0.12"
parity-scale-codec = { version = "3.6", default-features = false }
prettyplease = "0.2"
primitive-types = { version = "0.12", default-features = false }
Expand Down
4 changes: 4 additions & 0 deletions examples/demo/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ version = "0.1.0"
edition = "2021"

[dependencies]
mockall = { workspace = true, optional = true }
sails-rs.workspace = true

[build-dependencies]
demo = { path = "../app" }
sails-client-gen.workspace = true
sails-idl-gen.workspace = true

[features]
with_mocks = ["sails-rs/mockall", "dep:mockall"]
1 change: 1 addition & 0 deletions examples/demo/client/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ fn main() {
sails_client_gen::generate_client_from_idl(
&idl_file_path,
PathBuf::from(env::var("OUT_DIR").unwrap()).join("demo_client.rs"),
Some("with_mocks"),
)
.unwrap();
}
1 change: 1 addition & 0 deletions examples/no-svcs-prog/wasm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fn main() {
sails_client_gen::generate_client_from_idl(
&idl_file_path,
PathBuf::from(env::var("OUT_DIR").unwrap()).join("no_svcs_prog.rs"),
None,
)
.unwrap();
}
4 changes: 4 additions & 0 deletions examples/proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ demo-client = { path = "../demo/client" }
sails-idl-gen = { workspace = true, optional = true }
sails-rs.workspace = true

[dev-dependencies]
demo-client = { path = "../demo/client", features = ["with_mocks"] }
tokio = { workspace = true, features = ["rt", "macros"] }

[features]
idl-gen = ["sails-idl-gen"]
7 changes: 6 additions & 1 deletion examples/proxy/proxy.idl
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
type TupleStruct = struct {
bool,
};

constructor {
New : ();
};

service ThisThatCaller {
CallThis : (this_that_addr: actor_id) -> u32;
CallDoThis : (p1: u32, p2: str, p3: struct { opt h160, nat8 }, p4: TupleStruct, this_that_addr: actor_id) -> struct { str, u32 };
query QueryThis : (this_that_addr: actor_id) -> u32;
};

84 changes: 81 additions & 3 deletions examples/proxy/src/this_that/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use demo_client::traits::ThisThat;
use sails_rs::{calls::Query, prelude::*};
use demo_client::{traits::ThisThat, TupleStruct};
use sails_rs::{calls::*, prelude::*};

#[derive(Clone)]
pub struct ThisThatCaller<ThisThatClient> {
Expand All @@ -15,7 +15,85 @@ where
Self { this_that }
}

pub async fn call_this(&mut self, this_that_addr: ActorId) -> u32 {
pub async fn call_do_this(
&mut self,
p1: u32,
p2: String,
p3: (Option<H160>, NonZeroU8),
p4: TupleStruct,
this_that_addr: ActorId,
) -> (String, u32) {
self.this_that
.do_this(p1, p2, p3, p4)
.send_recv(this_that_addr)
.await
.unwrap()
}

pub async fn query_this(&self, this_that_addr: ActorId) -> u32 {
self.this_that.this().recv(this_that_addr).await.unwrap()
}
}

#[cfg(test)]
mod tests {
use super::*;
use demo_client::mockall::MockThisThat;
use sails_rs::mockall::*;

#[tokio::test]
async fn this_that_caller_query_this() {
// arrange
const ACTOR_ID: u64 = 11;
let mut mock_this_that = MockThisThat::<()>::new();
mock_this_that.expect_this().returning(|| {
let mut mock_query_this = MockQuery::new();
mock_query_this
.expect_recv()
.with(predicate::eq(ActorId::from(ACTOR_ID)))
.times(1)
.returning(move |_| Ok(42));
mock_query_this
});

// act
let this_that_caller = ThisThatCaller::new(mock_this_that);
let resp = this_that_caller.query_this(ACTOR_ID.into()).await;

// assert
assert_eq!(42, resp);
}

#[tokio::test]
async fn this_that_caller_call_do_this() {
// arrange
const ACTOR_ID: u64 = 11;
let mut mock_this_that = MockThisThat::<()>::new();
mock_this_that
.expect_do_this()
.returning(move |p1, p2, _p3, _p4| {
let mut mock_call_do_this = MockCall::new();
mock_call_do_this
.expect_send_recv()
.with(predicate::eq(ActorId::from(ACTOR_ID)))
.times(1)
.returning(move |_| Ok((p2.clone(), p1)));
mock_call_do_this
});

// act
let mut this_that_caller = ThisThatCaller::new(mock_this_that);
let resp = this_that_caller
.call_do_this(
42,
"test".to_owned(),
(None, NonZeroU8::MAX),
TupleStruct(true),
ACTOR_ID.into(),
)
.await;

// assert
assert_eq!(("test".to_owned(), 42), resp);
}
}
11 changes: 9 additions & 2 deletions examples/rmrk/resource/app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@
name = "rmrk-resource-app"
version = "0.1.0"
edition = "2021"
resolver = "2"

[dependencies]
gstd.workspace = true
parity-scale-codec = { workspace = true, features = ["derive"] }
mockall = { workspace = true, optional = true }
sails-rs.workspace = true
scale-info = { workspace = true, features = ["derive"] }

[build-dependencies]
git-download.workspace = true
sails-client-gen.workspace = true

[dev-dependencies]
rmrk-resource-app = { path = ".", features = ["mockall"] }
tokio = { workspace = true, features = ["rt", "macros"] }

[features]
mockall = ["sails-rs/mockall", "dep:mockall"]
3 changes: 2 additions & 1 deletion examples/rmrk/resource/app/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ fn main() {
let idl_file_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
.join("..\\..\\catalog\\wasm\\rmrk-catalog.idl");

sails_client_gen::generate_client_from_idl(idl_file_path, client_rs_file_path).unwrap();
sails_client_gen::generate_client_from_idl(idl_file_path, client_rs_file_path, Some("mockall"))
.unwrap();
}
4 changes: 4 additions & 0 deletions examples/rmrk/resource/app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#![no_std]

#[cfg(feature = "mockall")]
#[cfg(not(target_arch = "wasm32"))]
pub extern crate std;

use sails_rs::gstd::{calls::GStdRemoting, gprogram, groute, GStdExecContext};
use services::ResourceStorage;

Expand Down
6 changes: 4 additions & 2 deletions examples/rmrk/resource/app/src/services/errors.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use sails_rs::{Decode, Encode, Result as RtlResult, TypeInfo};
use sails_rs::prelude::*;

pub type Result<T, E = Error> = RtlResult<T, E>;
pub type Result<T, E = Error> = sails_rs::Result<T, E>;

#[derive(Encode, Decode, TypeInfo, Debug)]
#[codec(crate = sails_rs::scale_codec)]
#[scale_info(crate = sails_rs::scale_info)]
pub enum Error {
NotAuthorized,
ZeroResourceId,
Expand Down
Loading

0 comments on commit f97efdd

Please sign in to comment.