From 5358d8e68f1866afa474d41ef606e63b0c8ee4db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Mon, 14 Jul 2025 21:55:16 +0000 Subject: [PATCH] [wip] unit tests for `mock_client` module --- src/test_util/mock_client.rs | 181 ++++++++++++++++++++++++++++++++--- 1 file changed, 167 insertions(+), 14 deletions(-) diff --git a/src/test_util/mock_client.rs b/src/test_util/mock_client.rs index be143b6..9ed0f57 100644 --- a/src/test_util/mock_client.rs +++ b/src/test_util/mock_client.rs @@ -21,7 +21,7 @@ struct MockShare { files: HashMap, } -#[derive(Debug)] +#[derive(Debug, Default)] struct MockFile { size: u64, offset: u64, @@ -164,24 +164,177 @@ impl Client for MockClient { } } -#[test] -fn insert_share_works() { +// technically redundant, but kept for refactoring purposes +#[cfg(test)] +mod tests { use std::collections::HashSet; - let client = MockClient::default(); - let share_ids: [_; 8] = std::array::from_fn(|_| ShareID::from(true)); + use super::*; - for share_id in share_ids.as_ref() { - assert!(client.insert_share(share_id, MockShare::default()).is_ok()); + fn check_tostr_eq(left: L, right: R) + where + L: ExactSizeIterator, + L::Item: ToString, + R: ExactSizeIterator, + R::Item: ToString, + { + assert_eq!(left.len(), right.len()); + + let l_strings: HashSet<_> = left.map(|s| s.to_string()).collect(); + let r_strings: HashSet<_> = right.map(|s| s.to_string()).collect(); + + assert_eq!(l_strings, r_strings); } - assert_eq!(client.shares.borrow().len(), share_ids.len()); + fn add_share(client: &MockClient) -> ShareID { + let share_id = ShareID::from(true); + client + .insert_share(&share_id, MockShare::default()) + .unwrap(); - let sid_strings: HashSet<_> = share_ids.iter().map(ToString::to_string).collect(); - let cli_strings: HashSet<_> = (client.shares.borrow()) - .keys() - .map(ToString::to_string) - .collect(); + share_id + } - assert_eq!(sid_strings, cli_strings); + fn add_file(client: &MockClient, share_id: &ShareID) -> FileID { + let file_id = FileID::from(true); + client + .insert_file(share_id, &file_id, MockFile::default()) + .unwrap(); + + file_id + } + + #[test] + fn insert_share_works() { + let client = MockClient::default(); + let share_ids: [_; 10] = std::array::from_fn(|_| ShareID::from(true)); + + for share_id in share_ids.as_ref() { + assert!(client.insert_share(share_id, MockShare::default()).is_ok()); + } + + check_tostr_eq(client.shares.borrow().keys(), share_ids.iter()); + } + + #[test] + fn insert_share_double_errors() { + let client = MockClient::default(); + let share_ids: [_; 10] = std::array::from_fn(|_| add_share(&client)); + + for share_id in share_ids.as_ref() { + let res = client.insert_share(&share_id, MockShare::default()); + assert!(res.is_err()); + assert!( + matches!(res.unwrap_err(), Error::Response(s) if s.contains("can't insert share")) + ); + } + } + + #[test] + fn get_share_works() { + let client = MockClient::default(); + let share_ids: [_; 10] = std::array::from_fn(|_| add_share(&client)); + + for share_id in share_ids.as_ref() { + assert!(client.get_share(share_id).is_ok()); + } + } + + #[test] + fn get_share_nex_errors() { + let client = MockClient::default(); + add_share(&client); + let share_ids_nex: [_; 10] = std::array::from_fn(|_| ShareID::from(true)); + + for share_id_nex in share_ids_nex.as_ref() { + let res = client.get_share(share_id_nex); + + assert!(res.is_err()); + assert!( + matches!(res.unwrap_err(), Error::Response(s) if s.contains("can't find share")) + ); + } + } + + #[test] + fn insert_file_works() { + let client = MockClient::default(); + let share_id = add_share(&client); + let file_ids: [_; 10] = std::array::from_fn(|_| FileID::from(true)); + + for file_id in file_ids.as_ref() { + assert!( + client + .insert_file(&share_id, file_id, MockFile::default()) + .is_ok() + ); + } + + let shares = client.shares.borrow(); + let share = shares.get(&share_id.to_string()).unwrap(); + + check_tostr_eq(share.files.keys(), file_ids.iter()); + } + + #[test] + fn insert_file_nex_share_errors() { + let client = MockClient::default(); + add_share(&client); + + let share_id_nex = ShareID::default(); + let res = client.insert_file(&share_id_nex, &FileID::from(true), MockFile::default()); + assert!(res.is_err()); + assert!(matches!(res.unwrap_err(), Error::Response(s) if s.contains("can't find share"))); + } + + #[test] + fn insert_file_double_errors() { + let client = MockClient::default(); + let share_id = add_share(&client); + let file_ids: [_; 10] = std::array::from_fn(|_| add_file(&client, &share_id)); + + for file_id in file_ids.as_ref() { + let res = client.insert_file(&share_id, &file_id, MockFile::default()); + assert!(res.is_err()); + assert!( + matches!(res.unwrap_err(), Error::Response(s) if s.contains("can't insert file")) + ); + } + } + + #[test] + fn get_file_works() { + let client = MockClient::default(); + let share_id = add_share(&client); + let file_ids: [_; 10] = std::array::from_fn(|_| add_file(&client, &share_id)); + + for file_id in file_ids.as_ref() { + assert!(client.get_file(&share_id, file_id).is_ok()); + } + } + + #[test] + fn get_file_nex_errors() { + let client = MockClient::default(); + let share_id = add_share(&client); + add_file(&client, &share_id); + let file_ids_nex: [_; 10] = std::array::from_fn(|_| FileID::from(true)); + + for file_id_nex in file_ids_nex.as_ref() { + let share_id_nex = ShareID::from(true); + let res = client.get_file(&share_id_nex, file_id_nex); + + assert!(res.is_err()); + assert!( + matches!(res.unwrap_err(), Error::Response(s) if s.contains("can't find share")) + ); + + let res = client.get_file(&share_id, file_id_nex); + + assert!(res.is_err()); + assert!( + matches!(res.unwrap_err(), Error::Response(s) if s.contains("can't find file")) + ); + } + } }