[wip] unit testing
- mock impl for `sharry::Client` and associated IDs
This commit is contained in:
parent
65f1e5d083
commit
51cf212bb6
3 changed files with 192 additions and 0 deletions
57
src/test_util/mock_client.rs
Normal file
57
src/test_util/mock_client.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
use crate::{
|
||||||
|
file,
|
||||||
|
sharry::{self, AliasID, FileID, ShareID, Uri},
|
||||||
|
test_util::mock_ids,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct MockClient;
|
||||||
|
|
||||||
|
impl sharry::Client for MockClient {
|
||||||
|
fn share_create(
|
||||||
|
&self,
|
||||||
|
uri: &Uri,
|
||||||
|
alias_id: &AliasID,
|
||||||
|
_: sharry::json::NewShareRequest,
|
||||||
|
) -> crate::Result<ShareID> {
|
||||||
|
mock_ids::check_alias(uri, alias_id)?;
|
||||||
|
|
||||||
|
Ok(true.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn share_notify(&self, uri: &Uri, alias_id: &AliasID, share_id: &ShareID) -> crate::Result<()> {
|
||||||
|
mock_ids::check_alias(uri, alias_id)?;
|
||||||
|
mock_ids::check_share(share_id)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn file_create(
|
||||||
|
&self,
|
||||||
|
uri: &Uri,
|
||||||
|
alias_id: &AliasID,
|
||||||
|
share_id: &ShareID,
|
||||||
|
_: &file::Checked,
|
||||||
|
) -> crate::Result<FileID> {
|
||||||
|
mock_ids::check_alias(uri, alias_id)?;
|
||||||
|
mock_ids::check_share(share_id)?;
|
||||||
|
|
||||||
|
Ok(true.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn file_patch(
|
||||||
|
&self,
|
||||||
|
uri: &Uri,
|
||||||
|
alias_id: &AliasID,
|
||||||
|
share_id: &ShareID,
|
||||||
|
chunk: &file::Chunk,
|
||||||
|
) -> crate::Result<()> {
|
||||||
|
mock_ids::check_alias(uri, alias_id)?;
|
||||||
|
mock_ids::check_file(share_id, chunk.get_file_id())?;
|
||||||
|
|
||||||
|
// TODO: `chunk` must align to a full MiB
|
||||||
|
|
||||||
|
// Ok(())
|
||||||
|
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
133
src/test_util/mock_ids.rs
Normal file
133
src/test_util/mock_ids.rs
Normal file
|
|
@ -0,0 +1,133 @@
|
||||||
|
use crate::sharry::{AliasID, FileID, ShareID, Uri};
|
||||||
|
|
||||||
|
const VALID_URI: &'static str = "scheme://valid.uri";
|
||||||
|
const VALID_ALIAS: &'static str = "valid-alias";
|
||||||
|
const VALID_SHARE: &'static str = "valid-share";
|
||||||
|
const VALID_FILE: &'static str = "valid-file";
|
||||||
|
|
||||||
|
pub fn check_uri(uri: &Uri) -> crate::Result<&Uri> {
|
||||||
|
if uri.to_string() != VALID_URI {
|
||||||
|
Err(uri.into())
|
||||||
|
} else {
|
||||||
|
Ok(uri)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn check_alias<'t>(uri: &Uri, alias_id: &'t AliasID) -> crate::Result<&'t AliasID> {
|
||||||
|
check_uri(uri)?;
|
||||||
|
|
||||||
|
if alias_id.as_ref() != VALID_ALIAS {
|
||||||
|
Err(alias_id.into())
|
||||||
|
} else {
|
||||||
|
Ok(alias_id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn check_share(share_id: &ShareID) -> crate::Result<&ShareID> {
|
||||||
|
if share_id.to_string() != VALID_SHARE {
|
||||||
|
Err(share_id.into())
|
||||||
|
} else {
|
||||||
|
Ok(share_id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn check_file<'t>(share_id: &ShareID, file_id: &'t FileID) -> crate::Result<&'t FileID> {
|
||||||
|
check_share(share_id)?;
|
||||||
|
|
||||||
|
if file_id.to_string() != VALID_FILE {
|
||||||
|
Err(file_id.into())
|
||||||
|
} else {
|
||||||
|
Ok(file_id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn make_invalid(valid: &str) -> String {
|
||||||
|
let invalid = valid.replace("valid", "invalid");
|
||||||
|
assert_ne!(valid, invalid);
|
||||||
|
|
||||||
|
invalid
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<bool> for Uri {
|
||||||
|
fn from(value: bool) -> Self {
|
||||||
|
match value {
|
||||||
|
true => Self::from(VALID_URI.to_string()),
|
||||||
|
false => Self::from(make_invalid(VALID_URI)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<bool> for AliasID {
|
||||||
|
fn from(value: bool) -> Self {
|
||||||
|
match value {
|
||||||
|
true => Self::from(VALID_ALIAS.to_string()),
|
||||||
|
false => Self::from(make_invalid(VALID_ALIAS)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<bool> for ShareID {
|
||||||
|
fn from(value: bool) -> Self {
|
||||||
|
match value {
|
||||||
|
true => Self::from(VALID_SHARE.to_string()),
|
||||||
|
false => Self::from(make_invalid(VALID_SHARE)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<bool> for FileID {
|
||||||
|
fn from(value: bool) -> Self {
|
||||||
|
match value {
|
||||||
|
true => Self::new_test(VALID_FILE),
|
||||||
|
false => Self::new_test(make_invalid(VALID_FILE)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::Parameter;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn true_makes_valids() {
|
||||||
|
assert!(check_uri(&true.into()).is_ok());
|
||||||
|
assert!(check_alias(&true.into(), &true.into()).is_ok());
|
||||||
|
assert!(check_share(&true.into()).is_ok());
|
||||||
|
assert!(check_file(&true.into(), &true.into()).is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn false_makes_invalids() {
|
||||||
|
// invalid Uri
|
||||||
|
let check = check_uri(&false.into()).expect_err("should be invalid");
|
||||||
|
let p = check.get_invalid_param().expect("should be InvalidParam");
|
||||||
|
assert!(matches!(p, Parameter::Uri(_)));
|
||||||
|
|
||||||
|
// invalid Uri, valid alias
|
||||||
|
let check = check_alias(&false.into(), &true.into()).expect_err("should be invalid");
|
||||||
|
let p = check.get_invalid_param().expect("should be InvalidParam");
|
||||||
|
assert!(matches!(p, Parameter::Uri(_)));
|
||||||
|
|
||||||
|
// valid Uri, invalid alias
|
||||||
|
let check = check_alias(&true.into(), &false.into()).expect_err("should be invalid");
|
||||||
|
let p = check.get_invalid_param().expect("should be InvalidParam");
|
||||||
|
assert!(matches!(p, Parameter::AliasID(_)));
|
||||||
|
|
||||||
|
// invalid share
|
||||||
|
let check = check_share(&false.into()).expect_err("should be invalid");
|
||||||
|
let p = check.get_invalid_param().expect("should be InvalidParam");
|
||||||
|
assert!(matches!(p, Parameter::ShareID(_)));
|
||||||
|
|
||||||
|
// invalid share, valid file
|
||||||
|
let check = check_file(&false.into(), &true.into()).expect_err("should be invalid");
|
||||||
|
let p = check.get_invalid_param().expect("should be InvalidParam");
|
||||||
|
assert!(matches!(p, Parameter::ShareID(_)));
|
||||||
|
|
||||||
|
// valid share, invalid file
|
||||||
|
let check = check_file(&true.into(), &false.into()).expect_err("should be invalid");
|
||||||
|
let p = check.get_invalid_param().expect("should be InvalidParam");
|
||||||
|
assert!(matches!(p, Parameter::FileID(_)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
#![cfg(test)]
|
#![cfg(test)]
|
||||||
|
|
||||||
pub mod data;
|
pub mod data;
|
||||||
|
mod mock_client;
|
||||||
|
mod mock_ids;
|
||||||
|
|
||||||
use std::{fmt, io::Write};
|
use std::{fmt, io::Write};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue