[wip] unit tests for sharry module

This commit is contained in:
Jörn-Michael Miehe 2025-06-27 16:59:31 +00:00
parent bc4c15d17c
commit 903fbc3480
3 changed files with 121 additions and 6 deletions

View file

@ -93,6 +93,64 @@ impl TryFrom<String> for FileID {
mod tests {
use super::*;
#[test]
fn basic_traits_working() {
let inputs = vec![
"",
"abcd",
"12345",
"8woeurx09wp",
"6NHNiSVFhZF-dSGbX8iD8ib-Pdb7TbzpsvC-uBBSCyExxb",
];
for input in inputs {
{
// check AliasID
let _ = AliasID(input.to_owned());
let aid = AliasID::from(input.to_owned()); // "From" trait
assert_eq!(
aid.to_string(),
input,
"`impl Display for AliasID` expected: {:?}, got {:?}",
input,
aid.to_string(),
);
assert_eq!(
aid.as_ref(),
input.as_bytes(),
"`impl AsRef<[u8]> for AliasID` expected: {:?}, got {:?}",
input.as_bytes(),
aid.as_ref(),
);
}
{
// check ShareID
let _ = ShareID(input.to_owned());
let sid = ShareID::from(input.to_owned()); // "From" trait
assert_eq!(
sid.to_string(),
input,
"`impl Display for ShareID` expected: {:?}, got {:?}",
input,
sid.to_string(),
);
}
{
// check FileID
let fid = FileID(input.to_owned());
assert_eq!(
fid.to_string(),
input,
"`impl Display for FileID` expected: {:?}, got {:?}",
fid.to_string(),
input
);
}
}
}
#[test]
fn valid_urls_produce_expected_file_id() {
// a handful of validlooking URLs
@ -112,12 +170,11 @@ mod tests {
];
for (good, expected_fid) in cases {
let s = good.to_string();
let s = good.to_owned();
let file_id = FileID::try_from(s.clone()).expect("URL should parse successfully");
assert_eq!(
file_id.0, expected_fid,
"Expected `{}` → FileID({}), got {:?}",
good, expected_fid, file_id
"Expected `{good}` → FileID({expected_fid}), got {file_id:?}",
);
}
}
@ -138,7 +195,7 @@ mod tests {
];
for bad in bad_inputs {
let err = FileID::try_from(bad.to_string()).expect_err("URL should not parse");
let err = FileID::try_from(bad.to_owned()).expect_err("URL should not parse");
// make sure it's the Mismatch variant, and that it contains the original input
match err {
crate::Error::Mismatch { expected, actual } => {
@ -146,7 +203,7 @@ mod tests {
expected, "<proto>://<base>/api/v2/alias/upload/<share>/files/tus/<file>",
"Error should output expected format"
);
assert_eq!(actual, bad.to_string(), "Error should echo back the input");
assert_eq!(actual, bad.to_owned(), "Error should echo back the input");
}
_ => panic!("Expected Error::Mismatch for input `{bad}` but got {err:?}"),
}

View file

@ -1,7 +1,7 @@
#![allow(non_snake_case)]
use serde::{Deserialize, Serialize};
#[derive(Serialize, Debug)]
#[allow(non_snake_case)]
pub struct NewShareRequest {
name: String,
validity: u32,
@ -39,3 +39,29 @@ pub struct NotifyShareResponse {
pub success: bool,
pub message: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn nsreq_new_sets_fields_correctly() {
let name = "myname";
let desc = "desc";
let views = 7;
let req = NewShareRequest::new(name, Some(desc), views);
assert_eq!(req.name, name);
assert_eq!(req.validity, 0);
assert_eq!(req.description.as_deref(), Some(desc));
assert_eq!(req.maxViews, views);
assert!(req.password.is_none());
}
#[test]
fn nsreq_new_allows_none_description() {
let req = NewShareRequest::new("whatever", None::<&str>, 0);
assert!(req.description.is_none());
}
}

View file

@ -45,3 +45,35 @@ impl Uri {
self.endpoint(format_args!("alias/upload/{share_id}/files/tus/{file_id}"))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn basic_tests() {
let cases = vec![
("http", "example.com", "http://example.com"),
("https", "my-host:8080", "https://my-host:8080"),
("custom+scheme", "host", "custom+scheme://host"),
];
for (protocol, base_url, display) in cases {
let uri = Uri::new(protocol, base_url);
assert_eq!(
uri.to_string(),
display,
"`impl Display for Uri` expected: {:?}, got {:?}",
display,
uri.to_string(),
);
assert_eq!(
uri.as_ref(),
display.as_bytes(),
"`impl AsRef<[u8]> for Uri` expected: {:?}, got {:?}",
display.as_bytes(),
uri.as_ref(),
);
}
}
}