From 6167ebc98aaeb90005e344111ae9f86104204cad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Sat, 12 Jul 2025 01:31:28 +0000 Subject: [PATCH] [wip] unit tests for `file` module - testing for `Uploading` --- src/file/uploading.rs | 87 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 71 insertions(+), 16 deletions(-) diff --git a/src/file/uploading.rs b/src/file/uploading.rs index ebe6966..14eba21 100644 --- a/src/file/uploading.rs +++ b/src/file/uploading.rs @@ -66,7 +66,7 @@ impl Uploading { Some(self) } else { - warn!("attempted to rewind twice"); + warn!("attempted to rewind with no `previous_offset`"); None } } @@ -142,7 +142,10 @@ mod tests { use crate::{ 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::*; @@ -169,7 +172,7 @@ mod tests { #[test] fn basic_tests() { 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.offset, 0); 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) }; 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); } } + + #[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()); + } + } }