[wip] unit tests for file module
- test value generation for `sharry::{id, uri}`
- testing for `Chunk`
This commit is contained in:
parent
4ff1f34b08
commit
4ad9e28bc7
3 changed files with 49 additions and 12 deletions
|
|
@ -55,11 +55,32 @@ impl<'t> Chunk<'t> {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
// use super::*;
|
use crate::test_util::{
|
||||||
|
check_trait,
|
||||||
|
data::{DATA_LENGTHS_BAD, cases_with},
|
||||||
|
};
|
||||||
|
|
||||||
// #[test]
|
use super::*;
|
||||||
// fn basic_tests() {
|
|
||||||
// let mut foo = [0u8; 10];
|
#[test]
|
||||||
// let fid = sharry::FileID("fid".to_string());
|
fn basic_tests() {
|
||||||
// }
|
for (data, len, mock_offset) in cases_with(DATA_LENGTHS_BAD) {
|
||||||
|
let fid = sharry::FileID::default();
|
||||||
|
let chunk = Chunk::new_direct(fid, mock_offset, data);
|
||||||
|
|
||||||
|
let repr_expect = format!(
|
||||||
|
"Chunk {{ file_id: {:?}, offset: {:?}, data.len(): {:?}, .. }}",
|
||||||
|
chunk.file_id,
|
||||||
|
chunk.offset,
|
||||||
|
chunk.data.len()
|
||||||
|
);
|
||||||
|
check_trait(format!("{chunk:?}"), repr_expect, "Debug", "Chunk");
|
||||||
|
|
||||||
|
assert_eq!(chunk.get_file_id().to_string(), "fid");
|
||||||
|
assert_eq!(chunk.get_offset(), mock_offset);
|
||||||
|
assert_eq!(chunk.get_data(), data);
|
||||||
|
assert_eq!(chunk.get_length(), len);
|
||||||
|
assert_eq!(chunk.get_behind(), mock_offset + len);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,9 +28,11 @@ impl From<String> for AliasID {
|
||||||
///
|
///
|
||||||
/// - impl `From<String>` and `Clone` as this is just a String
|
/// - impl `From<String>` and `Clone` as this is just a String
|
||||||
/// - impl `serde` for cachefile handling
|
/// - impl `serde` for cachefile handling
|
||||||
|
/// - impl `Default` while testing for value generation
|
||||||
/// - impl `Display` for formatting compatibility
|
/// - impl `Display` for formatting compatibility
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct ShareID(pub(super) String);
|
#[cfg_attr(test, derive(Default))]
|
||||||
|
pub struct ShareID(String);
|
||||||
|
|
||||||
impl fmt::Display for ShareID {
|
impl fmt::Display for ShareID {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
|
@ -48,10 +50,20 @@ impl From<String> for ShareID {
|
||||||
///
|
///
|
||||||
/// - impl `Clone` as this is just a String
|
/// - impl `Clone` as this is just a String
|
||||||
/// - impl `serde` for cachefile handling
|
/// - impl `serde` for cachefile handling
|
||||||
|
/// - impl `Default` while testing for value generation
|
||||||
/// - impl `Display` for formatting compatibility
|
/// - impl `Display` for formatting compatibility
|
||||||
/// - impl `TryFrom<String>` for extracting from matching a "PATCH" uri
|
/// - impl `TryFrom<String>` for extracting from matching a "PATCH" uri
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct FileID(pub(super) String);
|
#[cfg_attr(test, derive(Default))]
|
||||||
|
pub struct FileID(String);
|
||||||
|
|
||||||
|
impl FileID {
|
||||||
|
#[cfg(test)]
|
||||||
|
/// create a new `FileID` for testing purposes
|
||||||
|
pub fn new_test(value: impl Into<String>) -> Self {
|
||||||
|
Self(value.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Display for FileID {
|
impl fmt::Display for FileID {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
|
@ -133,6 +145,9 @@ mod tests {
|
||||||
// check FileID
|
// check FileID
|
||||||
let fid = FileID(input.to_string());
|
let fid = FileID(input.to_string());
|
||||||
check_trait(fid.to_string(), input, "Display", "FileID");
|
check_trait(fid.to_string(), input, "Display", "FileID");
|
||||||
|
|
||||||
|
let fid_test = FileID::new_test(input);
|
||||||
|
assert_eq!(fid_test.0, fid.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ use serde::{Deserialize, Serialize};
|
||||||
/// - impl `Display` for formatting compatibility
|
/// - impl `Display` for formatting compatibility
|
||||||
/// - impl `AsRef<[u8]>` for hashing with `blake2b_simd`
|
/// - impl `AsRef<[u8]>` for hashing with `blake2b_simd`
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
#[cfg_attr(test, derive(Default))]
|
||||||
pub struct Uri(String);
|
pub struct Uri(String);
|
||||||
|
|
||||||
impl fmt::Display for Uri {
|
impl fmt::Display for Uri {
|
||||||
|
|
@ -182,7 +183,7 @@ mod tests {
|
||||||
("12345", "/api/v2/12345"),
|
("12345", "/api/v2/12345"),
|
||||||
];
|
];
|
||||||
|
|
||||||
let uri = Uri("".to_string());
|
let uri = Uri::default();
|
||||||
for (path, expected) in cases {
|
for (path, expected) in cases {
|
||||||
assert_eq!(&expected, &uri.endpoint(format_args!("{path}")));
|
assert_eq!(&expected, &uri.endpoint(format_args!("{path}")));
|
||||||
}
|
}
|
||||||
|
|
@ -190,9 +191,9 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_pub_endpoints() {
|
fn test_pub_endpoints() {
|
||||||
let uri = Uri("".to_string());
|
let uri = Uri::default();
|
||||||
let share_id = ShareID("sid".to_string());
|
let share_id = ShareID::default();
|
||||||
let file_id = FileID("fid".to_string());
|
let file_id = FileID::default();
|
||||||
|
|
||||||
assert_eq!("/api/v2/alias/upload/new", uri.share_create());
|
assert_eq!("/api/v2/alias/upload/new", uri.share_create());
|
||||||
assert_eq!("/api/v2/alias/mail/notify/sid", uri.share_notify(&share_id));
|
assert_eq!("/api/v2/alias/mail/notify/sid", uri.share_notify(&share_id));
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue