[wip] unit tests for mock_client module
This commit is contained in:
parent
66f59b4b3b
commit
5358d8e68f
1 changed files with 167 additions and 14 deletions
|
|
@ -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();
|
#[test]
|
||||||
let cli_strings: HashSet<_> = (client.shares.borrow())
|
fn insert_share_double_errors() {
|
||||||
.keys()
|
let client = MockClient::default();
|
||||||
.map(ToString::to_string)
|
let share_ids: [_; 10] = std::array::from_fn(|_| add_share(&client));
|
||||||
.collect();
|
|
||||||
|
|
||||||
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"))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue