[wip] unit tests for file module
- testing for `Checked`
This commit is contained in:
parent
cb5873b732
commit
b2c032d846
1 changed files with 29 additions and 11 deletions
|
|
@ -115,20 +115,27 @@ impl FileTrait for Checked {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use tempfile::TempDir;
|
use tempfile::{NamedTempFile, TempDir};
|
||||||
|
|
||||||
use crate::test_util::{
|
use crate::test_util::{
|
||||||
MockClient, create_file,
|
MockClient, check_trait, create_file,
|
||||||
data::{HASHES_STD_GOOD, cases, data},
|
data::{HASHES_STD_GOOD, cases, data},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
fn create_checked(content: &[u8]) -> (Checked, NamedTempFile) {
|
||||||
|
let file = create_file(content);
|
||||||
|
let chk = Checked::new(file.path()).expect("creating `Checked` should succeed");
|
||||||
|
|
||||||
|
// return both, so the `NamedTempFile` is not auto-deleted here
|
||||||
|
(chk, file)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn new_on_existing_file_works() {
|
fn new_on_existing_file_works() {
|
||||||
for (content, size) in cases() {
|
for (content, size) in cases() {
|
||||||
let file = create_file(content);
|
let (chk, file) = create_checked(content);
|
||||||
let chk = Checked::new(file.path()).expect("creating `Checked` should succeed");
|
|
||||||
|
|
||||||
let path = file
|
let path = file
|
||||||
.path()
|
.path()
|
||||||
|
|
@ -145,6 +152,19 @@ mod tests {
|
||||||
file.path().file_name().expect("`file_name` should succeed")
|
file.path().file_name().expect("`file_name` should succeed")
|
||||||
);
|
);
|
||||||
assert_eq!(chk.get_size(), size);
|
assert_eq!(chk.get_size(), size);
|
||||||
|
|
||||||
|
check_trait(
|
||||||
|
chk.as_ref(),
|
||||||
|
path.as_os_str().as_encoded_bytes(),
|
||||||
|
"AsRef<u8>",
|
||||||
|
"Checked",
|
||||||
|
);
|
||||||
|
|
||||||
|
// new_direct
|
||||||
|
let chk = Checked::new_direct(chk.path, chk.size, chk.hash);
|
||||||
|
assert_eq!(chk.path, path);
|
||||||
|
assert_eq!(chk.size, size);
|
||||||
|
assert!(chk.hash.is_none());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -179,8 +199,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn hashing_works() {
|
fn hashing_works() {
|
||||||
for (content, hash) in data().zip(HASHES_STD_GOOD) {
|
for (content, hash) in data().zip(HASHES_STD_GOOD) {
|
||||||
let file = create_file(content);
|
let (mut chk, _file) = create_checked(content);
|
||||||
let mut chk = Checked::new(file.path()).expect("creating `Checked` should succeed");
|
|
||||||
|
|
||||||
chk.hash(drop).expect("`hash` should succeed");
|
chk.hash(drop).expect("`hash` should succeed");
|
||||||
// `FileTrait`
|
// `FileTrait`
|
||||||
|
|
@ -193,10 +212,10 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn hashing_again_errors() {
|
fn hashing_again_errors() {
|
||||||
for content in data() {
|
for content in data() {
|
||||||
let file = create_file(content);
|
let (mut chk, _file) = create_checked(content);
|
||||||
let mut chk = Checked::new(file.path()).expect("creating `Checked` should succeed");
|
|
||||||
|
|
||||||
chk.hash(drop).expect("`hash` should succeed");
|
// fake hash
|
||||||
|
chk.hash = Some(String::default());
|
||||||
let err = chk.hash(drop).expect_err("`hash` twice should fail");
|
let err = chk.hash(drop).expect_err("`hash` twice should fail");
|
||||||
|
|
||||||
assert!(err.is_mismatch("unhashed file", chk.path.display().to_string()));
|
assert!(err.is_mismatch("unhashed file", chk.path.display().to_string()));
|
||||||
|
|
@ -209,8 +228,7 @@ mod tests {
|
||||||
let share_id = client.add_share();
|
let share_id = client.add_share();
|
||||||
|
|
||||||
for content in data() {
|
for content in data() {
|
||||||
let file = create_file(content);
|
let (chk, _file) = create_checked(content);
|
||||||
let chk = Checked::new(file.path()).expect("creating `Checked` should succeed");
|
|
||||||
|
|
||||||
assert!(
|
assert!(
|
||||||
chk.start_upload(
|
chk.start_upload(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue