fixing formatting and cargo clippy warnings

This commit is contained in:
Lorenzo Tucci 2023-01-11 14:52:42 +01:00
parent 925c7bc179
commit 9d1a39b2ff
No known key found for this signature in database
GPG key ID: D98C4FA2CDF590A0
13 changed files with 374 additions and 209 deletions

View file

@ -1,37 +1,39 @@
use testcontainers::clients::Cli;
use testcontainers::{Container, Docker, RunArgs};
use anyhow::{bail, Context as AnyContext, Result};
use futures::Future;
use swap::api::{Context, Config};
use swap::api::request::{Request, Params, Method, Shutdown};
use std::sync::Arc;
use tokio::time::{interval, timeout};
use std::time::Duration;
use jsonrpsee::ws_client::WsClientBuilder;
use jsonrpsee_core::{client::ClientT, params::ObjectParams};
use jsonrpsee::{rpc_params, RpcModule};
use jsonrpsee_core::client::ClientT;
use jsonrpsee_core::params::ObjectParams;
use jsonrpsee_types::error::CallError;
use tokio::sync::broadcast;
use swap::cli::command::{Bitcoin, Monero};
use std::collections::HashMap;
use uuid::Uuid;
use serde_json::{json, Value};
use sequential_test::sequential;
use serde_json::{json, Value};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use swap::api::request::{Method, Params, Request, Shutdown};
use swap::api::{Config, Context};
use swap::cli::command::{Bitcoin, Monero};
use testcontainers::clients::Cli;
use testcontainers::{Container, Docker, RunArgs};
use tokio::sync::broadcast;
use tokio::time::{interval, timeout};
use uuid::Uuid;
#[cfg(test)]
// to be replaced with actual "real" testing values
// need to create some kind of swap database and bitcoin environment with some funds
// need to create some kind of swap database and bitcoin environment with some
// funds
const SERVER_ADDRESS: &str = "127.0.0.1:1234";
const BITCOIN_ADDR: &str = "tb1qr3em6k3gfnyl8r7q0v7t4tlnyxzgxma3lressv";
const MONERO_ADDR: &str = "53gEuGZUhP9JMEBZoGaFNzhwEgiG7hwQdMCqFxiyiTeFPmkbt1mAoNybEUvYBKHcnrSgxnVWgZsTvRBaHBNXPa8tHiCU51a";
const SELLER: &str = "/ip4/127.0.0.1/tcp/9939/p2p/12D3KooWCdMKjesXMJz1SiZ7HgotrxuqhQJbP5sgBm2BwP1cqThi";
const SELLER: &str =
"/ip4/127.0.0.1/tcp/9939/p2p/12D3KooWCdMKjesXMJz1SiZ7HgotrxuqhQJbP5sgBm2BwP1cqThi";
const SWAP_ID: &str = "ea030832-3be9-454f-bb98-5ea9a788406b";
pub async fn initialize_context() -> (Arc<Context>, Request) {
let (is_testnet, debug, json) = (true, false, false);
//let data_dir = data::data_dir_from(None, is_testnet).unwrap();
// let data_dir = data::data_dir_from(None, is_testnet).unwrap();
let server_address = None;
let (tx, _) = broadcast::channel(1);
@ -60,12 +62,13 @@ pub async fn initialize_context() -> (Arc<Context>, Request) {
json,
server_address,
tx,
).await.unwrap();
)
.await
.unwrap();
(Arc::new(context), request)
}
#[tokio::test]
#[sequential]
pub async fn can_start_server() {
@ -92,7 +95,10 @@ pub async fn get_bitcoin_balance() {
tokio::time::sleep(Duration::from_secs(3)).await;
let client = WsClientBuilder::default().build(&url).await.unwrap();
let response: HashMap<String, i32> = client.request("get_bitcoin_balance", rpc_params!["id"]).await.unwrap();
let response: HashMap<String, i32> = client
.request("get_bitcoin_balance", rpc_params!["id"])
.await
.unwrap();
assert_eq!(response, HashMap::from([("balance".to_string(), 0)]));
ctx.shutdown.send(());
@ -113,7 +119,8 @@ pub async fn get_history() {
let client = WsClientBuilder::default().build(&url).await.unwrap();
let mut params = ObjectParams::new();
let response: HashMap<String, Vec<(Uuid, String)>> = client.request("get_history", params).await.unwrap();
let response: HashMap<String, Vec<(Uuid, String)>> =
client.request("get_history", params).await.unwrap();
let swaps: Vec<(Uuid, String)> = Vec::new();
assert_eq!(response, HashMap::from([("swaps".to_string(), swaps)]));
@ -136,9 +143,13 @@ pub async fn get_raw_history() {
let mut params = ObjectParams::new();
let raw_history: HashMap<Uuid, String> = HashMap::new();
let response: HashMap<String, HashMap<Uuid, String>> = client.request("get_raw_history", params).await.unwrap();
let response: HashMap<String, HashMap<Uuid, String>> =
client.request("get_raw_history", params).await.unwrap();
assert_eq!(response, HashMap::from([("raw_history".to_string(), raw_history)]));
assert_eq!(
response,
HashMap::from([("raw_history".to_string(), raw_history)])
);
ctx.shutdown.send(());
}
@ -159,7 +170,8 @@ pub async fn get_seller() {
let response: Result<HashMap<String, String>, _> = client.request("get_seller", params).await;
// We should ideally match the expected error and panic if it's different one, but the request returns a custom error (to investigate)
// We should ideally match the expected error and panic if it's different one,
// but the request returns a custom error (to investigate)
// Err(jsonrpsee_core::Error::Call(CallError::InvalidParams(e))) => (),
// Err(e) => panic!("ErrorType was not ParseError but {e:?}"),
@ -185,7 +197,10 @@ pub async fn get_seller() {
match response {
Ok(hash) => (),
Err(e) => panic!("Expected a HashMap with correct params, got an error: {}", e),
Err(e) => panic!(
"Expected a HashMap with correct params, got an error: {}",
e
),
}
ctx.shutdown.send(());
}
@ -205,7 +220,8 @@ pub async fn get_swap_start_date() {
let client = WsClientBuilder::default().build(&url).await.unwrap();
let mut params = ObjectParams::new();
let response: Result<HashMap<String, String>, _> = client.request("get_swap_start_date", params).await;
let response: Result<HashMap<String, String>, _> =
client.request("get_swap_start_date", params).await;
match response {
Err(e) => (),
@ -215,7 +231,8 @@ pub async fn get_swap_start_date() {
let mut params = ObjectParams::new();
params.insert("swap_id", "invalid_swap");
let response: Result<HashMap<String, String>, _> = client.request("get_swap_start_date", params).await;
let response: Result<HashMap<String, String>, _> =
client.request("get_swap_start_date", params).await;
match response {
Err(e) => (),
@ -225,7 +242,8 @@ pub async fn get_swap_start_date() {
let mut params = ObjectParams::new();
params.insert("swap_id", SWAP_ID);
let response: Result<HashMap<String, String>, _> = client.request("get_swap_start_date", params).await;
let response: Result<HashMap<String, String>, _> =
client.request("get_swap_start_date", params).await;
match response {
Ok(hash) => (),
@ -249,7 +267,8 @@ pub async fn resume_swap() {
let client = WsClientBuilder::default().build(&url).await.unwrap();
let mut params = ObjectParams::new();
let response: Result<HashMap<String, String>, _> = client.request("get_swap_start_date", params).await;
let response: Result<HashMap<String, String>, _> =
client.request("get_swap_start_date", params).await;
match response {
Err(e) => (),
@ -259,7 +278,8 @@ pub async fn resume_swap() {
let mut params = ObjectParams::new();
params.insert("swap_id", "invalid_swap");
let response: Result<HashMap<String, String>, _> = client.request("get_swap_start_date", params).await;
let response: Result<HashMap<String, String>, _> =
client.request("get_swap_start_date", params).await;
match response {
Err(e) => (),
@ -269,7 +289,8 @@ pub async fn resume_swap() {
let mut params = ObjectParams::new();
params.insert("swap_id", SWAP_ID);
let response: Result<HashMap<String, String>, _> = client.request("get_swap_start_date", params).await;
let response: Result<HashMap<String, String>, _> =
client.request("get_swap_start_date", params).await;
match response {
Ok(hash) => (),