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