Move mocked test to own mod

This commit is contained in:
Mateusz Słodkowicz 2024-03-22 12:52:36 +01:00
parent 31ed4a0eaa
commit 54850ad139
Signed by: materus
GPG Key ID: 28D140BCA60B4FD1
1 changed files with 46 additions and 45 deletions

View File

@ -29,56 +29,57 @@ struct Err {
error_type: String, error_type: String,
} }
#[cfg(test)] #[cfg(test)]
fn mock_get_rates(code: &String) -> Result<Status, reqwest::Error> { mod test {
let response: ConversionRates = match code.as_str() { pub fn mock_get_rates(code: &String) -> Result<super::Status, reqwest::Error> {
"PLN" => serde_json::from_str(include_str!(concat!( let response: super::ConversionRates = match code.as_str() {
".", "PLN" => serde_json::from_str(include_str!(concat!(
crate::main_separator!(), ".",
"mock_data", crate::main_separator!(),
crate::main_separator!(), "mock_data",
"PLN.json" crate::main_separator!(),
))) "PLN.json"
.expect("Error when deserializng"), )))
"EUR" => serde_json::from_str(include_str!(concat!( .expect("Error when deserializng"),
".", "EUR" => serde_json::from_str(include_str!(concat!(
crate::main_separator!(), ".",
"mock_data", crate::main_separator!(),
crate::main_separator!(), "mock_data",
"EUR.json" crate::main_separator!(),
))) "EUR.json"
.expect("Error when deserializng"), )))
_ => { .expect("Error when deserializng"),
panic!("Unknown code") _ => {
} panic!("Unknown code")
}; }
cache::add_rates( };
response.time_next_update_unix, crate::cache::add_rates(
&response.base_code, response.time_next_update_unix,
&response.conversion_rates, &response.base_code,
) &response.conversion_rates,
.expect("Error while caching response"); )
Ok(Status::OK) .expect("Error while caching response");
} Ok(super::Status::OK)
#[cfg(test)] }
pub fn mock_get_currencies() -> Result<Status, reqwest::Error> { pub fn mock_get_currencies() -> Result<super::Status, reqwest::Error> {
let codes: CurrencyCodes = serde_json::from_str(include_str!(concat!( let codes: super::CurrencyCodes = serde_json::from_str(include_str!(concat!(
".", ".",
crate::main_separator!(), crate::main_separator!(),
"mock_data", "mock_data",
crate::main_separator!(), crate::main_separator!(),
"codes.json" "codes.json"
))) )))
.expect("Error when deserializng"); .expect("Error when deserializng");
for code in codes.supported_codes { for code in codes.supported_codes {
cache::add_code(code).expect("Error when adding code to cache"); crate::cache::add_code(code).expect("Error when adding code to cache");
}
return Ok(super::Status::OK);
} }
return Ok(Status::OK);
} }
pub fn get_rates(code: &String) -> Result<Status, reqwest::Error> { pub fn get_rates(code: &String) -> Result<Status, reqwest::Error> {
if cfg!(test) { if cfg!(test) {
#[cfg(test)] #[cfg(test)]
return mock_get_rates(code); return test::mock_get_rates(code);
} }
let response = reqwest::blocking::get(format!( let response = reqwest::blocking::get(format!(
"{}{}{}{}", "{}{}{}{}",
@ -111,7 +112,7 @@ pub fn get_rates(code: &String) -> Result<Status, reqwest::Error> {
pub fn get_currencies() -> Result<Status, reqwest::Error> { pub fn get_currencies() -> Result<Status, reqwest::Error> {
if cfg!(test) { if cfg!(test) {
#[cfg(test)] #[cfg(test)]
return mock_get_currencies(); return test::mock_get_currencies();
} }
let response = reqwest::blocking::get(format!( let response = reqwest::blocking::get(format!(
"{}{}{}", "{}{}{}",