Skip to content

Commit

Permalink
fix: minor issues in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Jul 5, 2024
1 parent bedcde3 commit 6aefd00
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 12 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

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

21 changes: 20 additions & 1 deletion packages/dapi-grpc/src/mock/serde_mockable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,30 @@
//! /// ## Example
///
/// ```rust
/// struct SomeObject {
/// field: u32,
/// }
///
/// impl dapi_grpc::mock::Mockable for SomeObject {
/// fn mock_serialize(&self) -> Option<Vec<u8>> {
/// Some(self.field.to_be_bytes().to_vec())
/// }
///
/// fn mock_deserialize(bytes: &[u8]) -> Option<Self> {
/// if bytes.len() != 4 {
/// return None;
/// }
///
/// Some(SomeObject {
/// field: u32::from_be_bytes(bytes.try_into().expect("4 bytes")),
/// })
/// }
/// }
///
/// #[derive(serde::Serialize,serde::Deserialize)]
/// struct TestStruct {
/// #[serde(with="dapi_grpc::mock::serde_mockable")]
/// field: u32,
/// field: SomeObject,
/// }
/// ```
use super::Mockable;
Expand Down
6 changes: 5 additions & 1 deletion packages/rs-dapi-client/src/dapi_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub enum DapiClientError<TE: Mockable> {
/// The error happened on transport layer
#[error("transport error with {1}: {0}")]
Transport(
#[serde(with = "dapi_grpc::mock::serde_mockable")] TE,
#[cfg_attr(feature = "mocks", serde(with = "dapi_grpc::mock::serde_mockable"))] TE,
Address,
),
/// There are no valid DAPI addresses to use.
Expand Down Expand Up @@ -50,6 +50,8 @@ impl<TE: CanRetry + Mockable> CanRetry for DapiClientError<TE> {
}
}
}

#[cfg(feature = "mocks")]
#[derive(serde::Serialize, serde::Deserialize)]
struct TransportErrorData {
transport_error: Vec<u8>,
Expand All @@ -60,10 +62,12 @@ struct TransportErrorData {
///
/// We need to do manual serialization because of the generic type parameter which doesn't support serde derive.
impl<TE: Mockable> Mockable for DapiClientError<TE> {
#[cfg(feature = "mocks")]
fn mock_serialize(&self) -> Option<Vec<u8>> {
Some(serde_json::to_vec(self).expect("serialize DAPI client error"))
}

#[cfg(feature = "mocks")]
fn mock_deserialize(data: &[u8]) -> Option<Self> {
Some(serde_json::from_slice(data).expect("deserialize DAPI client error"))
}
Expand Down
9 changes: 6 additions & 3 deletions packages/rs-dapi-client/tests/local_platform_connectivity.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#[cfg(not(feature = "offline-testing"))]
mod tests {
use dapi_grpc::platform::v0::{
self as platform_proto, get_identity_response, GetIdentityResponse, ResponseMetadata,
use dapi_grpc::{
platform::v0::{
self as platform_proto, get_identity_response, GetIdentityResponse, ResponseMetadata,
},
tonic::transport::Uri,
};
use rs_dapi_client::{AddressList, DapiClient, DapiRequest, RequestSettings};

Expand All @@ -13,7 +16,7 @@ mod tests {
#[tokio::test]
async fn get_identity() {
let mut address_list = AddressList::new();
address_list.add_uri(rs_dapi_client::Uri::from_static("http://127.0.0.1:2443"));
address_list.add_uri(Uri::from_static("http://127.0.0.1:2443"));

let mut client = DapiClient::new(address_list, RequestSettings::default());
let request = platform_proto::GetIdentityRequest {
Expand Down
9 changes: 6 additions & 3 deletions packages/rs-dapi-client/tests/mock_dapi_client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use dapi_grpc::platform::v0::{GetIdentityRequest, GetIdentityResponse, Proof};

use rs_dapi_client::{mock::MockDapiClient, DapiRequest, DapiRequestExecutor, RequestSettings};
#[cfg(feature = "mocks")]
use {
dapi_grpc::platform::v0::{GetIdentityRequest, GetIdentityResponse, Proof},
rs_dapi_client::{mock::MockDapiClient, DapiRequest, DapiRequestExecutor, RequestSettings},
};

#[tokio::test]
#[cfg(feature = "mocks")]
async fn test_mock_get_identity_dapi_client() {
let mut dapi = MockDapiClient::new();

Expand Down
1 change: 0 additions & 1 deletion packages/rs-drive-abci/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ rand = "0.8.5"
tempfile = "3.3.0"
hex = "0.4.3"
indexmap = { version = "2.2.6", features = ["serde"] }
sha2 = "0.10.6"
dashcore-rpc = { git = "https://github.com/dashpay/rust-dashcore-rpc", tag = "v0.15.2" }
dpp = { path = "../rs-dpp", features = ["abci"] }
simple-signer = { path = "../simple-signer" }
Expand Down
1 change: 0 additions & 1 deletion packages/rs-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ dashcore-rpc = { git = "https://github.com/dashpay/rust-dashcore-rpc", tag = "v0
lru = { version = "0.12.3", optional = true }
bip37-bloom-filter = { git = "https://github.com/dashpay/rs-bip37-bloom-filter", branch = "develop" }
pollster = { version = "0.3.0" }
sha2 = "0.10.8"

[dev-dependencies]
tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread"] }
Expand Down

0 comments on commit 6aefd00

Please sign in to comment.