[wip] unit tests for file module
- test for `Checked::start_upload`
This commit is contained in:
parent
3257a97351
commit
d4cc102a0f
4 changed files with 65 additions and 33 deletions
|
|
@ -115,12 +115,10 @@ impl FileTrait for Checked {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
// tests for `Checked::start_upload` omitted, as they require a `sharry::Client`
|
||||
|
||||
use tempfile::TempDir;
|
||||
|
||||
use crate::test_util::{
|
||||
create_file,
|
||||
MockClient, create_file,
|
||||
data::{HASHES_STD_GOOD, cases, data},
|
||||
};
|
||||
|
||||
|
|
@ -204,4 +202,25 @@ mod tests {
|
|||
assert!(err.is_mismatch("unhashed file", chk.path.display().to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn start_upload_works() {
|
||||
let client = MockClient::default();
|
||||
let share_id = client.add_share();
|
||||
|
||||
for content in data() {
|
||||
let file = create_file(content);
|
||||
let chk = Checked::new(file.path()).expect("creating `Checked` should succeed");
|
||||
|
||||
assert!(
|
||||
chk.start_upload(
|
||||
&client,
|
||||
&sharry::Uri::from(true),
|
||||
&sharry::AliasID::from(true),
|
||||
&share_id
|
||||
)
|
||||
.is_ok()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,6 @@ use std::{
|
|||
collections::{HashMap, hash_map::Entry},
|
||||
};
|
||||
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
Error, Result, error_response,
|
||||
file::{self, FileTrait},
|
||||
|
|
@ -15,7 +13,6 @@ use super::mock_ids::CheckID;
|
|||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct MockClient {
|
||||
use_uuid: bool,
|
||||
shares: RefCell<HashMap<String, MockShare>>,
|
||||
}
|
||||
|
||||
|
|
@ -30,36 +27,36 @@ struct MockFile {
|
|||
offset: u64,
|
||||
}
|
||||
|
||||
impl From<bool> for MockClient {
|
||||
fn from(value: bool) -> Self {
|
||||
impl From<&file::Checked> for MockFile {
|
||||
fn from(value: &file::Checked) -> Self {
|
||||
Self {
|
||||
use_uuid: value,
|
||||
..Default::default()
|
||||
size: value.get_size(),
|
||||
offset: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MockClient {
|
||||
fn insert_share(&self, share_id: &ShareID) -> Result<()> {
|
||||
fn insert_share(&self, share_id: &ShareID, share: MockShare) -> Result<()> {
|
||||
let mut shares = self.shares.borrow_mut();
|
||||
|
||||
let Entry::Vacant(entry) = shares.entry(share_id.to_string()) else {
|
||||
return Err(error_response!("can't insert share {share_id:?}!"));
|
||||
};
|
||||
|
||||
entry.insert(MockShare::default());
|
||||
entry.insert(share);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn insert_file(&self, share_id: &ShareID, file_id: &FileID, size: u64) -> Result<()> {
|
||||
fn insert_file(&self, share_id: &ShareID, file_id: &FileID, file: MockFile) -> Result<()> {
|
||||
let mut share = self.get_share(share_id)?;
|
||||
|
||||
let Entry::Vacant(entry) = share.files.entry(file_id.to_string()) else {
|
||||
return Err(error_response!("can't insert file {file_id:?}!"));
|
||||
};
|
||||
|
||||
entry.insert(MockFile { size, offset: 0 });
|
||||
entry.insert(file);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -96,6 +93,14 @@ impl MockClient {
|
|||
share.files.get_mut(file_id).expect("checked but None!")
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn add_share(&self) -> ShareID {
|
||||
let share_id = ShareID::from(true);
|
||||
self.insert_share(&share_id, MockShare::default())
|
||||
.expect("should never fail");
|
||||
|
||||
share_id
|
||||
}
|
||||
}
|
||||
|
||||
impl Client for MockClient {
|
||||
|
|
@ -107,12 +112,8 @@ impl Client for MockClient {
|
|||
) -> Result<ShareID> {
|
||||
(uri, alias_id).check()?;
|
||||
|
||||
let share_id = if self.use_uuid {
|
||||
Uuid::now_v1(&[4, 8, 15, 16, 23, 42]).to_string().into()
|
||||
} else {
|
||||
true.into()
|
||||
};
|
||||
self.insert_share(&share_id)?;
|
||||
let share_id = true.into();
|
||||
self.insert_share(&share_id, MockShare::default())?;
|
||||
|
||||
Ok(share_id)
|
||||
}
|
||||
|
|
@ -136,13 +137,8 @@ impl Client for MockClient {
|
|||
(uri, alias_id).check()?;
|
||||
share_id.check()?;
|
||||
|
||||
let file_id = if self.use_uuid {
|
||||
let id = Uuid::now_v1(&[4, 8, 15, 16, 23, 42]);
|
||||
FileID::new_test(id)
|
||||
} else {
|
||||
true.into()
|
||||
};
|
||||
self.insert_file(share_id, &file_id, file.get_size())?;
|
||||
let file_id = true.into();
|
||||
self.insert_file(share_id, &file_id, file.into())?;
|
||||
|
||||
Ok(file_id)
|
||||
}
|
||||
|
|
@ -157,7 +153,6 @@ impl Client for MockClient {
|
|||
(uri, alias_id).check()?;
|
||||
(share_id, chunk.get_file_id()).check()?;
|
||||
|
||||
// TODO: `chunk` must align to a full MiB
|
||||
let file = self.get_file(share_id, chunk.get_file_id())?;
|
||||
|
||||
if chunk.get_length() == 0 {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
Result,
|
||||
sharry::{AliasID, FileID, ShareID, Uri},
|
||||
|
|
@ -10,11 +12,25 @@ const VALID_FILE: &str = "valid-file";
|
|||
|
||||
fn make_invalid(valid: &str) -> String {
|
||||
let invalid = valid.replace("valid", "invalid");
|
||||
assert_ne!(valid, invalid);
|
||||
|
||||
assert_ne!(invalid, valid);
|
||||
|
||||
invalid
|
||||
}
|
||||
|
||||
fn make_valid(valid: &str) -> String {
|
||||
let invalid = make_invalid(valid);
|
||||
|
||||
let valid = {
|
||||
let id = Uuid::now_v1(&[4, 8, 15, 16, 23, 42]);
|
||||
valid.replace("valid", &id.to_string())
|
||||
};
|
||||
|
||||
assert_ne!(valid, invalid);
|
||||
|
||||
valid
|
||||
}
|
||||
|
||||
pub trait CheckID {
|
||||
fn check(self) -> Result<()>;
|
||||
}
|
||||
|
|
@ -56,7 +72,7 @@ impl CheckID for (&ShareID, &FileID) {
|
|||
impl From<bool> for Uri {
|
||||
fn from(value: bool) -> Self {
|
||||
if value {
|
||||
Self::from(VALID_URI.to_string())
|
||||
Self::from(make_valid(VALID_URI))
|
||||
} else {
|
||||
Self::from(make_invalid(VALID_URI))
|
||||
}
|
||||
|
|
@ -66,7 +82,7 @@ impl From<bool> for Uri {
|
|||
impl From<bool> for AliasID {
|
||||
fn from(value: bool) -> Self {
|
||||
if value {
|
||||
Self::from(VALID_ALIAS.to_string())
|
||||
Self::from(make_valid(VALID_ALIAS))
|
||||
} else {
|
||||
Self::from(make_invalid(VALID_ALIAS))
|
||||
}
|
||||
|
|
@ -76,7 +92,7 @@ impl From<bool> for AliasID {
|
|||
impl From<bool> for ShareID {
|
||||
fn from(value: bool) -> Self {
|
||||
if value {
|
||||
Self::from(VALID_SHARE.to_string())
|
||||
Self::from(make_valid(VALID_SHARE))
|
||||
} else {
|
||||
Self::from(make_invalid(VALID_SHARE))
|
||||
}
|
||||
|
|
@ -86,7 +102,7 @@ impl From<bool> for ShareID {
|
|||
impl From<bool> for FileID {
|
||||
fn from(value: bool) -> Self {
|
||||
if value {
|
||||
Self::new_test(VALID_FILE)
|
||||
Self::new_test(make_valid(VALID_FILE))
|
||||
} else {
|
||||
Self::new_test(make_invalid(VALID_FILE))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ pub mod data;
|
|||
mod mock_client;
|
||||
mod mock_ids;
|
||||
|
||||
pub use mock_client::MockClient;
|
||||
|
||||
use std::{fmt, io::Write};
|
||||
|
||||
use tempfile::NamedTempFile;
|
||||
|
|
|
|||
Loading…
Reference in a new issue