Commit af26d6e0 authored by Éloïs's avatar Éloïs
Browse files

ref: separate each command in a dedicated file

parent 715c6390
Pipeline #10520 passed with stages
in 12 minutes and 10 seconds
......@@ -13,6 +13,10 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
pub mod balance;
pub mod current_ud;
pub mod members_count;
use crate::*;
#[derive(StructOpt)]
......@@ -28,162 +32,3 @@ pub(crate) enum Command {
/// Get current number of WoT members
MembersCount,
}
pub(crate) fn balance<W: Write>(
client: &Client,
out: &mut W,
pubkey_or_script: &str,
ud_unit: bool,
) -> anyhow::Result<()> {
let request_body = Balance::build_query(balance::Variables {
script: pubkey_or_script.to_owned(),
with_ud: ud_unit,
});
let balance::ResponseData {
balance: balance::BalanceBalance { amount },
current_ud: current_ud_opt,
} = client.send_gql_query(&request_body)?;
if let Some(balance::BalanceCurrentUd { amount: ud_amount }) = current_ud_opt {
writeln!(
out,
"The balance of account '{}' is {:.2} UDĞ1 !",
pubkey_or_script,
amount as f64 / ud_amount as f64,
)?;
} else {
writeln!(
out,
"The balance of account '{}' is {}.{:02} Ğ1 !",
pubkey_or_script,
amount / 100,
amount % 100
)?;
}
Ok(())
}
pub(crate) fn current_ud<W: Write>(client: &Client, out: &mut W) -> anyhow::Result<()> {
let request_body = CurrentUd::build_query(current_ud::Variables);
if let current_ud::ResponseData {
current_ud: Some(current_ud::CurrentUdCurrentUd { amount }),
} = client.send_gql_query(&request_body)?
{
let int_part = amount / 100;
let dec_part = amount % 100;
writeln!(
out,
"The current UD value is {}.{:02} Ğ1 !",
int_part, dec_part
)?;
} else {
writeln!(out, "server with empty blockchain")?;
}
Ok(())
}
pub(crate) fn members_count<W: Write>(client: &Client, out: &mut W) -> anyhow::Result<()> {
let request_body = MembersCount::build_query(members_count::Variables);
let members_count::ResponseData {
current_block: members_count::MembersCountCurrentBlock { members_count },
} = client.send_gql_query(&request_body)?;
writeln!(
out,
"There is currently {} members in Ğ1 WoT!",
members_count
)?;
Ok(())
}
// below are tests
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_balance() -> anyhow::Result<()> {
let mut client = Client::default();
client
.expect_send_gql_query::<graphql_client::QueryBody<balance::Variables>, _>()
.returning(|_| {
Ok(balance::ResponseData {
balance: balance::BalanceBalance { amount: 2_046 },
current_ud: None,
})
});
let mut out = Vec::new();
balance(&client, &mut out, "toto", false)?;
let output = std::str::from_utf8(&out)?;
assert_eq!(output, "The balance of account 'toto' is 20.46 Ğ1 !\n");
Ok(())
}
#[test]
fn test_balance_with_ud_unit() -> anyhow::Result<()> {
let mut client = Client::default();
client
.expect_send_gql_query::<graphql_client::QueryBody<balance::Variables>, _>()
.returning(|_| {
Ok(balance::ResponseData {
balance: balance::BalanceBalance { amount: 2_046 },
current_ud: Some(balance::BalanceCurrentUd { amount: 1_023 }),
})
});
let mut out = Vec::new();
balance(&client, &mut out, "toto", true)?;
let output = std::str::from_utf8(&out)?;
assert_eq!(output, "The balance of account 'toto' is 2.00 UDĞ1 !\n");
Ok(())
}
#[test]
fn test_current_ud() -> anyhow::Result<()> {
let mut client = Client::default();
client
.expect_send_gql_query::<graphql_client::QueryBody<current_ud::Variables>, _>()
.returning(|_| {
Ok(current_ud::ResponseData {
current_ud: Some(current_ud::CurrentUdCurrentUd { amount: 1_023 }),
})
});
let mut out = Vec::new();
current_ud(&client, &mut out)?;
let output = std::str::from_utf8(&out)?;
assert_eq!(output, "The current UD value is 10.23 Ğ1 !\n");
Ok(())
}
#[test]
fn test_member_count() -> anyhow::Result<()> {
let mut client = Client::default();
client
.expect_send_gql_query::<graphql_client::QueryBody<members_count::Variables>, _>()
.returning(|_| {
Ok(members_count::ResponseData {
current_block: members_count::MembersCountCurrentBlock {
members_count: 10_000,
},
})
});
let mut out = Vec::new();
members_count(&client, &mut out)?;
let output = std::str::from_utf8(&out)?;
assert_eq!(output, "There is currently 10000 members in Ğ1 WoT!\n");
Ok(())
}
}
// Copyright (C) 2020 Éloïs SANCHEZ.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::*;
pub(crate) fn balance<W: Write>(
client: &Client,
out: &mut W,
pubkey_or_script: &str,
ud_unit: bool,
) -> anyhow::Result<()> {
let request_body = Balance::build_query(balance::Variables {
script: pubkey_or_script.to_owned(),
with_ud: ud_unit,
});
let balance::ResponseData {
balance: balance::BalanceBalance { amount },
current_ud: current_ud_opt,
} = client.send_gql_query(&request_body)?;
if let Some(balance::BalanceCurrentUd { amount: ud_amount }) = current_ud_opt {
writeln!(
out,
"The balance of account '{}' is {:.2} UDĞ1 !",
pubkey_or_script,
amount as f64 / ud_amount as f64,
)?;
} else {
writeln!(
out,
"The balance of account '{}' is {}.{:02} Ğ1 !",
pubkey_or_script,
amount / 100,
amount % 100
)?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_balance() -> anyhow::Result<()> {
let mut client = Client::default();
client
.expect_send_gql_query::<graphql_client::QueryBody<balance::Variables>, _>()
.returning(|_| {
Ok(balance::ResponseData {
balance: balance::BalanceBalance { amount: 2_046 },
current_ud: None,
})
});
let mut out = Vec::new();
balance(&client, &mut out, "toto", false)?;
let output = std::str::from_utf8(&out)?;
assert_eq!(output, "The balance of account 'toto' is 20.46 Ğ1 !\n");
Ok(())
}
#[test]
fn test_balance_with_ud_unit() -> anyhow::Result<()> {
let mut client = Client::default();
client
.expect_send_gql_query::<graphql_client::QueryBody<balance::Variables>, _>()
.returning(|_| {
Ok(balance::ResponseData {
balance: balance::BalanceBalance { amount: 2_046 },
current_ud: Some(balance::BalanceCurrentUd { amount: 1_023 }),
})
});
let mut out = Vec::new();
balance(&client, &mut out, "toto", true)?;
let output = std::str::from_utf8(&out)?;
assert_eq!(output, "The balance of account 'toto' is 2.00 UDĞ1 !\n");
Ok(())
}
}
// Copyright (C) 2020 Éloïs SANCHEZ.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::*;
pub(crate) fn current_ud<W: Write>(client: &Client, out: &mut W) -> anyhow::Result<()> {
let request_body = CurrentUd::build_query(current_ud::Variables);
if let current_ud::ResponseData {
current_ud: Some(current_ud::CurrentUdCurrentUd { amount }),
} = client.send_gql_query(&request_body)?
{
let int_part = amount / 100;
let dec_part = amount % 100;
writeln!(
out,
"The current UD value is {}.{:02} Ğ1 !",
int_part, dec_part
)?;
} else {
writeln!(out, "server with empty blockchain")?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_current_ud() -> anyhow::Result<()> {
let mut client = Client::default();
client
.expect_send_gql_query::<graphql_client::QueryBody<current_ud::Variables>, _>()
.returning(|_| {
Ok(current_ud::ResponseData {
current_ud: Some(current_ud::CurrentUdCurrentUd { amount: 1_023 }),
})
});
let mut out = Vec::new();
current_ud(&client, &mut out)?;
let output = std::str::from_utf8(&out)?;
assert_eq!(output, "The current UD value is 10.23 Ğ1 !\n");
Ok(())
}
}
// Copyright (C) 2020 Éloïs SANCHEZ.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::*;
pub(crate) fn members_count<W: Write>(client: &Client, out: &mut W) -> anyhow::Result<()> {
let request_body = MembersCount::build_query(members_count::Variables);
let members_count::ResponseData {
current_block: members_count::MembersCountCurrentBlock { members_count },
} = client.send_gql_query(&request_body)?;
writeln!(
out,
"There is currently {} members in Ğ1 WoT!",
members_count
)?;
Ok(())
}
// below are tests
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_member_count() -> anyhow::Result<()> {
let mut client = Client::default();
client
.expect_send_gql_query::<graphql_client::QueryBody<members_count::Variables>, _>()
.returning(|_| {
Ok(members_count::ResponseData {
current_block: members_count::MembersCountCurrentBlock {
members_count: 10_000,
},
})
});
let mut out = Vec::new();
members_count(&client, &mut out)?;
let output = std::str::from_utf8(&out)?;
assert_eq!(output, "There is currently 10000 members in Ğ1 WoT!\n");
Ok(())
}
}
......@@ -32,7 +32,7 @@ use crate::client::Client;
#[cfg(test)]
use crate::client::MockClient as Client;
use crate::commands::Command;
use commands::{balance, current_ud, members_count};
use commands::{balance::balance, current_ud::current_ud, members_count::members_count};
use graphql_client::GraphQLQuery;
use graphql_client::Response;
use std::io::Write;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment