[Fix] Stability
This commit is contained in:
parent
e1dd86ec02
commit
8160f8d0cb
44 changed files with 796 additions and 1296 deletions
|
|
@ -1,7 +1,5 @@
|
|||
use json::{JsonValue, object::Object};
|
||||
|
||||
use crate::terms_getter::Type;
|
||||
use iota_util::file_util::load_file;
|
||||
use iota_util::crypto_helper::hex_hash;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
#[allow(unused)]
|
||||
|
|
@ -23,15 +21,24 @@ impl Doc {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_raw(doc_type: Type, content: String, timestamp: u64) -> Doc {
|
||||
Doc::new(
|
||||
timestamp.to_string(),
|
||||
hex_hash(&content),
|
||||
doc_type,
|
||||
timestamp,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn equals_some(&self, other: &Option<Self>) -> bool {
|
||||
if let Some(other) = other {
|
||||
self.get_version() == other.get_version() && self.get_hash() == other.get_hash()
|
||||
self.equals(other)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
pub fn equals(&self, other: &Self) -> bool {
|
||||
self.get_version() == other.get_version() && self.get_hash() == other.get_hash()
|
||||
self.get_hash() == other.get_hash()
|
||||
}
|
||||
|
||||
pub fn get_version(&self) -> String {
|
||||
|
|
@ -41,34 +48,36 @@ impl Doc {
|
|||
self.hash.clone()
|
||||
}
|
||||
pub fn get_time(&self) -> u64 {
|
||||
self.timestamp.clone()
|
||||
self.timestamp
|
||||
}
|
||||
pub fn get_content(&self) -> String {
|
||||
load_file(
|
||||
format!("docs/{}/", self.doc_type.to_str()).as_str(),
|
||||
format!("{}.md", self.version).as_str(),
|
||||
)
|
||||
#[cfg(test)]
|
||||
fn timestamp(&self) -> u64 {
|
||||
self.timestamp
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_json(&self) -> JsonValue {
|
||||
let mut json = JsonValue::new_object();
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::Doc;
|
||||
use crate::terms_getter::Type;
|
||||
use iota_util::crypto_helper::hex_hash;
|
||||
|
||||
let _ = json.insert("version", self.version.clone());
|
||||
let _ = json.insert("hash", self.hash.clone());
|
||||
let _ = json.insert("unix", self.timestamp.clone());
|
||||
#[test]
|
||||
fn raw_documents_use_a_local_timestamp_and_content_hash() {
|
||||
let content = "# EULA\n".to_owned();
|
||||
let document = Doc::from_raw(Type::EULA, content.clone(), 123);
|
||||
|
||||
json
|
||||
assert_eq!(document.doc_type, Type::EULA);
|
||||
assert_eq!(document.get_version(), "123");
|
||||
assert_eq!(document.timestamp(), 123);
|
||||
assert_eq!(document.get_hash(), hex_hash(&content));
|
||||
}
|
||||
pub fn from_json(doc_type: Type, json: Object) -> Option<Self> {
|
||||
let hash = json.get("hash")?.as_str()?.to_string();
|
||||
let version = json.get("version")?.as_str()?.to_string();
|
||||
let timestamp = json.get("unix")?.as_u64()?;
|
||||
|
||||
Some(Doc {
|
||||
version,
|
||||
hash,
|
||||
doc_type,
|
||||
timestamp,
|
||||
})
|
||||
#[test]
|
||||
fn matching_documents_ignore_the_fetch_timestamp() {
|
||||
let earlier = Doc::from_raw(Type::EULA, "# EULA\n".to_owned(), 123);
|
||||
let later = Doc::from_raw(Type::EULA, "# EULA\n".to_owned(), 456);
|
||||
|
||||
assert!(earlier.equals(&later));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue