Skip to content

Commit

Permalink
Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
virtualdesigner committed Sep 24, 2023
1 parent a1db788 commit 0510b6a
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 12 deletions.
41 changes: 40 additions & 1 deletion src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub async fn calculate_currency_total(selected_currencies: Map<String, Coin>) ->
}

#[tauri::command]
pub fn calculate_exchange_rates(selected_currencies: Map<String, Coin>) -> Map<String, f32> {
pub async fn calculate_exchange_rates(selected_currencies: Map<String, Coin>) -> Map<String, f32> {
let currencies: Vec<(String, Coin)> = selected_currencies.into_iter().collect();
let mut exchange: Map<String, f32> = Map::new();

Expand All @@ -46,4 +46,43 @@ pub fn calculate_exchange_rates(selected_currencies: Map<String, Coin>) -> Map<S
}

return exchange;
}

#[cfg(test)]
mod tests {
use super::*;

#[tokio::test]
async fn test_exchange_rate_calculation() {
let rates: Map<String, Coin> = Map::from([(String::from("USD"), Coin {
conversion_rate: 1.0,
existing_amount: 2.0,
is_selected: true
}), (String::from("INR"), Coin {
conversion_rate: 0.01,
existing_amount: 10.0,
is_selected: true
})]);

let expected_result = Map::from([(String::from("INR/USD"), 100.0), (String::from("USD/INR"), 0.01)]);
let result = calculate_exchange_rates(rates).await;
assert_eq!(result, expected_result);
}

#[tokio::test]
async fn test_total_calculation() {
let rates: Map<String, Coin> = Map::from([(String::from("USD"), Coin {
conversion_rate: 1.0,
existing_amount: 2.0,
is_selected: true
}), (String::from("INR"), Coin {
conversion_rate: 0.01,
existing_amount: 10.0,
is_selected: true
})]);

let expected_result = Map::from([(String::from("INR"), 10.02), (String::from("USD"), 1002.0)]);
let result = calculate_currency_total(rates).await;
assert_eq!(result, expected_result);
}
}
89 changes: 78 additions & 11 deletions src-tauri/src/rates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub async fn construct_rates() -> Result<Map<String, f32>, RateConstructErrors>
Ok(rates)
}

fn create_or_use_rates_file(file_name: &str) -> File {
fn create_or_use_rates_file(file_name: &str, empty: bool) -> File {
if let Some(proj_dirs) = ProjectDirs::from("com", "ark-builders", "ark-rate-desktop") {
let path = proj_dirs.cache_dir();
// must panic if failed to create/use the rates file
Expand All @@ -53,20 +53,22 @@ fn create_or_use_rates_file(file_name: &str) -> File {
File::options()
.read(true)
.write(true)
.truncate(empty)
.open(&path)
.unwrap_or_else(|_| File::create(&path).unwrap())
} else {
panic!("No valid home directory path could be retrieved from the operating system.");
}
}

fn write_rates_to_file(rates: &Map<String, f32>, mut file: File) {
let bytes = serde_json::to_string(&rates).expect("Must not fail to serialize the json into a string");
let _ = file.set_len(0);
fn write_rates_to_file(rates: &Map<String, f32>, mut file: &File) {
let bytes = serde_json::to_string(rates).expect("Must not fail to serialize the json into a string");
file.set_len(0).unwrap();
file.write_all(bytes.as_bytes()).unwrap();
file.flush().unwrap();
}

fn read_rates_from_file(mut file: File) -> Result<Map<String, f32>, RateConstructErrors> {
fn read_rates_from_file(mut file: &File) -> Result<Map<String, f32>, RateConstructErrors> {
let mut unparsed_rates = String::new();
file.read_to_string(&mut unparsed_rates)?;
let parsed_rates = serde_json::from_str::<Map<String, f32>>(&unparsed_rates)?;
Expand All @@ -75,20 +77,85 @@ fn read_rates_from_file(mut file: File) -> Result<Map<String, f32>, RateConstruc

pub async fn get_parsed_rates() -> Map<String, f32> {
let parsed_rates: Map<String, f32>;
let rates_file = create_or_use_rates_file("rates");
let result = construct_rates().await;

match result {
Ok(rates) => {
parsed_rates = rates;
write_rates_to_file(&parsed_rates, rates_file);
let rates_file = create_or_use_rates_file("rates", true);
write_rates_to_file(&parsed_rates, &rates_file);
}
Err(err) => {
dbg!(err, &rates_file);
parsed_rates = read_rates_from_file(rates_file)
Err(_) => {
let rates_file = create_or_use_rates_file("rates", false);
parsed_rates = read_rates_from_file(&rates_file)
.expect("Failed to retrieve rates which is required")
}
}

parsed_rates
}
}

#[cfg(test)]
mod tests {
use std::io::Seek;

use super::*;

fn create_or_use_file(path: &str, empty: bool) -> File {
File::options()
.read(true)
.write(true)
.truncate(empty)
.open(path)
.unwrap_or_else(|_| File::create(path).unwrap())
}

fn add_string_to_file(data: &str, file: &mut File) {
file.set_len(0).unwrap();
file.write_all(data.as_bytes()).unwrap();
file.flush().unwrap();
}

#[test]
fn test_write_rates_to_file() {
let rates: Map<String, f32> = Map::from([(String::from("USD"), 1.0), (String::from("INR"), 0.01)]);
let mut file = create_or_use_file("write-rates", true);

write_rates_to_file(&rates, &file);

file.rewind().unwrap();
let mut file_data = String::new();
file.read_to_string(&mut file_data).unwrap();
assert_eq!(file_data.is_empty(), false);
}

#[test]
fn test_read_rates_from_file() {
let mut file = create_or_use_file("read-rates", true);
let rates_string = "{ \"USD\": 1.0, \"INR\": 0.01 }";
let rates = serde_json::from_str::<Map<String, f32>>(rates_string).unwrap();
add_string_to_file(rates_string, &mut file);
file.rewind().unwrap();

let parsed_rates = read_rates_from_file(&file).unwrap();
assert_eq!(rates, parsed_rates);
}

#[test]
fn test_parse_fiat_rates() {
let unparsed_rates = "{ \"rates\": {\"USD\": 1.0, \"INR\": 0.01} }";
let parsed_rates: Map<String, f32> = Map::from([(String::from("USD"), 1.0), (String::from("INR"), 0.01)]);

let result = parse_fiat_rates(String::from(unparsed_rates)).unwrap();
assert_eq!(parsed_rates, result);
}

#[test]
fn test_parse_crypto_rates() {
let unparsed_rates = "[ { \"symbol\": \"USD\", \"current_price\": 1 }, {\"symbol\": \"INR\", \"current_price\": 100} ]";
let parsed_rates: Map<String, f32> = Map::from([(String::from("USD"), 1.0), (String::from("INR"), 0.01)]);

let result = parse_crypto_rates(String::from(unparsed_rates)).unwrap();
assert_eq!(parsed_rates, result);
}
}

0 comments on commit 0510b6a

Please sign in to comment.