[wip] unit tests for file module

- testing for `Uploading`
This commit is contained in:
Jörn-Michael Miehe 2025-07-12 01:31:28 +00:00
parent 47ad51f860
commit 6167ebc98a

View file

@ -66,7 +66,7 @@ impl Uploading {
Some(self) Some(self)
} else { } else {
warn!("attempted to rewind twice"); warn!("attempted to rewind with no `previous_offset`");
None None
} }
} }
@ -142,7 +142,10 @@ mod tests {
use crate::{ use crate::{
sharry::{Client, json::NewShareRequest}, sharry::{Client, json::NewShareRequest},
test_util::{MockClient, create_file, data::cases}, test_util::{
MockClient, create_file,
data::{DATA_LENGTHS_BAD, cases, cases_with},
},
}; };
use super::*; use super::*;
@ -169,7 +172,7 @@ mod tests {
#[test] #[test]
fn basic_tests() { fn basic_tests() {
fn check_members(upl: &Uploading, path: &PathBuf, size: u64) { fn check_members(upl: &Uploading, path: &PathBuf, size: u64) {
assert_eq!(&upl.path, path); assert_eq!(upl.path, *path);
assert_eq!(upl.size, size); assert_eq!(upl.size, size);
assert_eq!(upl.offset, 0); assert_eq!(upl.offset, 0);
assert!(upl.previous_offset.is_none()); assert!(upl.previous_offset.is_none());
@ -194,23 +197,75 @@ mod tests {
unsafe { Uploading::new_unchecked(upl.path, upl.size, upl.hash, upl.file_id) }; unsafe { Uploading::new_unchecked(upl.path, upl.size, upl.hash, upl.file_id) };
check_members(&upl, &path, size); 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` // `stop`
let chk = upl.stop(); let chk = upl.stop();
assert_eq!(chk.get_name(), path.file_name().unwrap().to_str().unwrap()); assert_eq!(chk.get_name(), path.file_name().unwrap().to_str().unwrap());
assert_eq!(chk.get_size(), size); assert_eq!(chk.get_size(), size);
} }
} }
#[test]
fn check_eof_works() {
for (content, size) in cases() {
let (upl, _share_id, file) = create_uploading(content);
let path = file.path().canonicalize().unwrap();
let eof = upl.check_eof();
if size > 0 {
assert!(eof.is_ok());
} else {
assert!(eof.is_err());
assert_eq!(eof.unwrap_err(), path);
};
}
}
#[test]
fn check_read_works() {
for (content, size, extra) in cases_with(DATA_LENGTHS_BAD) {
let (mut upl, _share_id, _file) = create_uploading(content);
// use oversized buffer
let mut buf = vec![0; (size + extra) as usize];
let chunk_res = upl.read(&mut buf);
if size > 0 {
assert_eq!(upl.previous_offset, Some(0));
assert_eq!(upl.offset, size);
let chunk = chunk_res.unwrap();
assert_eq!(chunk.get_offset(), 0);
assert_eq!(chunk.get_length(), size);
assert_eq!(chunk.get_file_id().to_string(), upl.file_id.to_string());
assert_eq!(chunk.get_data(), content);
} else {
assert!(chunk_res.is_err());
};
}
}
#[test]
fn check_rewind_works() {
let cases = cases()
// ignore "empty" testcase
.filter(|&(_, size)| size > 0)
// remove "size"
.map(|(content, _)| content);
for content in cases {
let (mut upl, _share_id, _file) = create_uploading(content);
// read 1 byte and rewind
upl.read(&mut [0]).unwrap();
upl = upl.rewind().unwrap();
// check: read has been rewound
assert_eq!(upl.offset, 0);
assert_eq!(upl.previous_offset, None);
// check: attempting to rewind again will destroy
assert!(upl.rewind().is_none());
}
}
} }