Add tests #5
+17
-10
@@ -41,7 +41,11 @@ impl NumberParser {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn save_file(&self, numbers: Vec<textsender_models::contact::Contact>, filename: &str) {
|
||||
pub async fn save_file(
|
||||
&self,
|
||||
numbers: Vec<textsender_models::contact::Contact>,
|
||||
filename: &str,
|
||||
) {
|
||||
// let filename = String::from("numbers.json");
|
||||
match serde_json::to_string(&numbers) {
|
||||
Ok(json_string) => match std::fs::write(&filename, json_string) {
|
||||
@@ -167,6 +171,7 @@ impl NumberParser {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use textsender_models::contact::Contact;
|
||||
|
||||
@@ -174,14 +179,14 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_file_dump() {
|
||||
let filename = String::from(TEST_NUMBERS_0);
|
||||
let file_exists = {
|
||||
let path = std::path::Path::new(filename.as_str());
|
||||
let path = std::path::Path::new(TEST_NUMBERS_0);
|
||||
path.exists()
|
||||
};
|
||||
|
||||
assert_eq!(true, file_exists, "File does not exists {filename:?}");
|
||||
assert_eq!(true, file_exists, "File does not exists {TEST_NUMBERS_0:?}");
|
||||
|
||||
let filename = String::from(TEST_NUMBERS_0);
|
||||
let prsr = super::NumberParser { filepath: filename };
|
||||
let original: Vec<Contact> = vec![
|
||||
Contact {
|
||||
@@ -222,8 +227,10 @@ mod tests {
|
||||
Ok(contacts) => {
|
||||
assert!((contacts.len() > 0), "No contacts has been parsed");
|
||||
|
||||
let phone_numbers_1: std::collections::HashSet<_> = contacts.iter().map(|p| p.phone_number.clone()).collect();
|
||||
let phone_numbers_2: std::collections::HashSet<_> = original.iter().map(|p| p.phone_number.clone()).collect();
|
||||
let phone_numbers_1: std::collections::HashSet<_> =
|
||||
contacts.iter().map(|p| p.phone_number.clone()).collect();
|
||||
let phone_numbers_2: std::collections::HashSet<_> =
|
||||
original.iter().map(|p| p.phone_number.clone()).collect();
|
||||
assert_eq!(phone_numbers_1, phone_numbers_2, "No match");
|
||||
}
|
||||
Err(err) => {
|
||||
@@ -234,9 +241,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_save_file() {
|
||||
let filename = String::from(TEST_NUMBERS_0);
|
||||
|
||||
let prsr = super::NumberParser { filepath: filename };
|
||||
let prsr = super::NumberParser {
|
||||
filepath: String::from(TEST_NUMBERS_0),
|
||||
};
|
||||
let results = async_std::task::block_on(prsr.file_dump());
|
||||
assert!(results.is_ok(), "File dump not successful");
|
||||
let test_output_file: &str = "test-numbers.json";
|
||||
@@ -244,7 +251,7 @@ mod tests {
|
||||
let contacts = results.unwrap();
|
||||
async_std::task::block_on(prsr.save_file(contacts, test_output_file));
|
||||
let file_exists = {
|
||||
let path = std::path::Path::new("numbers.json");
|
||||
let path = std::path::Path::new(test_output_file);
|
||||
path.exists()
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user