Compare commits

...

2 commits

Author SHA1 Message Date
5358d8e68f [wip] unit tests for mock_client module 2025-07-14 21:55:16 +00:00
66f59b4b3b [wip] unit tests
- re-added redundant `tests` submodules for good form
2025-07-14 20:58:28 +00:00
3 changed files with 238 additions and 73 deletions

View file

@ -21,7 +21,7 @@ struct MockShare {
files: HashMap<String, MockFile>, files: HashMap<String, MockFile>,
} }
#[derive(Debug)] #[derive(Debug, Default)]
struct MockFile { struct MockFile {
size: u64, size: u64,
offset: u64, offset: u64,
@ -164,24 +164,177 @@ impl Client for MockClient {
} }
} }
#[test] // technically redundant, but kept for refactoring purposes
fn insert_share_works() { #[cfg(test)]
mod tests {
use std::collections::HashSet; use std::collections::HashSet;
use super::*;
fn check_tostr_eq<L, R>(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);
}
fn add_share(client: &MockClient) -> ShareID {
let share_id = ShareID::from(true);
client
.insert_share(&share_id, MockShare::default())
.unwrap();
share_id
}
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 client = MockClient::default();
let share_ids: [_; 8] = std::array::from_fn(|_| ShareID::from(true)); let share_ids: [_; 10] = std::array::from_fn(|_| ShareID::from(true));
for share_id in share_ids.as_ref() { for share_id in share_ids.as_ref() {
assert!(client.insert_share(share_id, MockShare::default()).is_ok()); assert!(client.insert_share(share_id, MockShare::default()).is_ok());
} }
assert_eq!(client.shares.borrow().len(), share_ids.len()); check_tostr_eq(client.shares.borrow().keys(), share_ids.iter());
}
let sid_strings: HashSet<_> = share_ids.iter().map(ToString::to_string).collect();
let cli_strings: HashSet<_> = (client.shares.borrow()) #[test]
.keys() fn insert_share_double_errors() {
.map(ToString::to_string) let client = MockClient::default();
.collect(); let share_ids: [_; 10] = std::array::from_fn(|_| add_share(&client));
assert_eq!(sid_strings, cli_strings); 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"))
);
}
}
} }

View file

@ -109,6 +109,11 @@ impl From<bool> for FileID {
} }
} }
// technically redundant, but kept for refactoring purposes
#[cfg(test)]
mod tests {
use super::*;
#[test] #[test]
fn true_makes_valids() { fn true_makes_valids() {
let uri = Uri::from(true); let uri = Uri::from(true);
@ -174,3 +179,4 @@ fn false_makes_invalids() {
test_check((&share_id_i, &file_id), is_share_id_i); test_check((&share_id_i, &file_id), is_share_id_i);
test_check((&share_id, &file_id_i), is_file_id_i); test_check((&share_id, &file_id_i), is_file_id_i);
} }
}

View file

@ -29,8 +29,14 @@ pub fn create_file(data: &[u8]) -> NamedTempFile {
tmp tmp
} }
// technically redundant, but kept for refactoring purposes
#[cfg(test)]
mod tests {
use super::*;
#[test] #[test]
#[should_panic = "`impl foo for bar` expected: 1, actual: 0"] #[should_panic = "`impl foo for bar` expected: 1, actual: 0"]
fn make_check_trait_panic() { fn make_check_trait_panic() {
check_trait(0, 1, "foo", "bar"); check_trait(0, 1, "foo", "bar");
} }
}