[wip] unit tests for file module

- testing for `Uploading`
This commit is contained in:
Jörn-Michael Miehe 2025-07-10 12:59:38 +00:00
parent ab76563b83
commit bd9aa77d6c
3 changed files with 90 additions and 17 deletions

View file

@ -116,9 +116,12 @@ impl FileTrait for Checked {
mod tests {
use tempfile::{NamedTempFile, TempDir};
use crate::test_util::{
use crate::{
sharry::{Client, json::NewShareRequest},
test_util::{
MockClient, check_trait, create_file,
data::{HASHES_STD_GOOD, cases, data},
},
};
use super::*;
@ -224,18 +227,18 @@ mod tests {
#[test]
fn start_upload_works() {
let client = MockClient::default();
let share_id = client.add_share();
let uri = sharry::Uri::from(true);
let alias_id = sharry::AliasID::from(true);
let share_id = client
.share_create(&uri, &alias_id, NewShareRequest::new("share", 0))
.expect("");
for content in data() {
let (chk, _file) = create_checked(content);
assert!(
chk.start_upload(
&client,
&sharry::Uri::from(true),
&sharry::AliasID::from(true),
&share_id
)
chk.start_upload( &client, &uri, &alias_id, &share_id )
.is_ok()
);
}

View file

@ -14,7 +14,6 @@ use super::{Checked, Chunk, FileTrait};
/// Description of a `file::Checked` that is actively being uploaded
///
/// - impl `serde` for cachefile handling
/// - impl `Into<Checked>` to abort an upload
#[derive(Serialize, Deserialize, Debug)]
pub struct Uploading {
/// canonical path to a regular file
@ -136,3 +135,82 @@ impl FileTrait for Uploading {
super::check_hash(&self.path, self.size, self.hash.as_deref(), on_progress)
}
}
#[cfg(test)]
mod tests {
use tempfile::NamedTempFile;
use crate::{
sharry::{Client, json::NewShareRequest},
test_util::{MockClient, create_file, data::cases},
};
use super::*;
fn create_uploading(content: &[u8]) -> (Uploading, sharry::ShareID, NamedTempFile) {
let client = MockClient::default();
let uri = sharry::Uri::from(true);
let alias_id = sharry::AliasID::from(true);
let share_id = client
.share_create(&uri, &alias_id, NewShareRequest::new("share", 0))
.expect("");
let file = create_file(content);
let upl = Checked::new(file.path())
.unwrap()
.start_upload(&client, &uri, &alias_id, &share_id)
.unwrap();
// return all, so the `NamedTempFile` is not auto-deleted here
(upl, share_id, file)
}
#[test]
fn basic_tests() {
fn check_members(upl: &Uploading, path: &PathBuf, size: u64) {
assert_eq!(&upl.path, path);
assert_eq!(upl.size, size);
assert_eq!(upl.offset, 0);
assert!(upl.previous_offset.is_none());
assert!(upl.hash.is_none());
}
for (content, size) in cases() {
let (upl, _share_id, file) = create_uploading(content);
let path = file.path().canonicalize().unwrap();
check_members(&upl, &path, size);
// `get_offset`
assert_eq!(upl.get_offset(), upl.offset);
// `FileTrait`
assert_eq!(upl.get_name(), file.path().file_name().unwrap());
assert_eq!(upl.get_size(), size);
assert!(upl.check_hash(drop).is_err());
// `new_unchecked`
let upl =
unsafe { Uploading::new_unchecked(upl.path, upl.size, upl.hash, upl.file_id) };
check_members(&upl, &path, size);
// TODO into separate test
// // `check_eof`
// let upl = if size == 0 {
// upl
// } else {
// let eof = upl.check_eof();
// assert!(eof.is_ok());
// let upl = eof.unwrap();
// check_members(&upl, &path, size);
// upl
// };
// `stop`
let chk = upl.stop();
assert_eq!(chk.get_name(), path.file_name().unwrap().to_str().unwrap());
assert_eq!(chk.get_size(), size);
}
}
}

View file

@ -93,14 +93,6 @@ 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 {