Added retrival function.
This commit is contained in:
parent
c0e4a5fad7
commit
0ffb30d322
@ -112,13 +112,38 @@ impl Entry {
|
||||
})
|
||||
}
|
||||
|
||||
async fn get(filename: &str) -> Result<Self, DBError> {
|
||||
let filepath = Path::new(filename);
|
||||
let content = match read(filepath).await {
|
||||
Ok(text) => text,
|
||||
Err(err) => {
|
||||
let mut error = DBError::new("read error");
|
||||
error.add_source(err);
|
||||
return Err(error);
|
||||
}
|
||||
};
|
||||
let data = match DataType::from_bytes(&mut content.iter()) {
|
||||
Ok(raw) => raw,
|
||||
Err(err) => {
|
||||
let mut error = DBError::new("read error");
|
||||
error.add_source(err);
|
||||
return Err(error);
|
||||
}
|
||||
};
|
||||
Ok(Self {
|
||||
data: data,
|
||||
filename: filename.to_string(),
|
||||
last_used: Cell::new(Instant::now()),
|
||||
})
|
||||
}
|
||||
|
||||
fn elapsed(&self) -> Duration {
|
||||
self.last_used.get().elapsed()
|
||||
}
|
||||
|
||||
async fn get(&self) -> Result<DataType, DBError> {
|
||||
fn data(&self) -> DataType {
|
||||
self.last_used.set(Instant::now());
|
||||
Ok(self.data.clone())
|
||||
self.data.clone()
|
||||
}
|
||||
|
||||
async fn update(&mut self, data: DataType) -> Result<(), DBError> {
|
||||
@ -296,7 +321,7 @@ mod entry {
|
||||
Duration::from_secs(1) > item.elapsed(),
|
||||
"last_used should have been now."
|
||||
);
|
||||
let output = item.get().await.unwrap();
|
||||
let output = item.data();
|
||||
assert_eq!(
|
||||
data.list(["database"].to_vec()).unwrap(),
|
||||
output.list(["database"].to_vec()).unwrap()
|
||||
@ -357,7 +382,7 @@ mod entry {
|
||||
let item = Entry::new(filename.to_string(), data).await.unwrap();
|
||||
item.last_used
|
||||
.set(Instant::now() - Duration::from_secs(300));
|
||||
item.get().await.unwrap();
|
||||
item.data();
|
||||
assert!(
|
||||
Duration::from_secs(1) > item.elapsed(),
|
||||
"last_used should have been reset."
|
||||
@ -381,7 +406,7 @@ mod entry {
|
||||
Duration::from_secs(1) > item.elapsed(),
|
||||
"last_used should have been reset."
|
||||
);
|
||||
let output = item.get().await.unwrap();
|
||||
let output = item.data();
|
||||
assert_eq!(
|
||||
data.list(["database"].to_vec()).unwrap(),
|
||||
output.list(["database"].to_vec()).unwrap()
|
||||
@ -414,6 +439,75 @@ mod entry {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn retrieve() {
|
||||
let dir = tempdir().unwrap();
|
||||
let mut data = DataType::new("store").unwrap();
|
||||
data.add("database", "something_old", "3.14159").unwrap();
|
||||
let filepath = dir.path().join("existing");
|
||||
let filename = filepath.to_str().unwrap();
|
||||
let item = Entry::new(filename.to_string(), data.clone())
|
||||
.await
|
||||
.unwrap();
|
||||
let output = Entry::get(filename).await.unwrap();
|
||||
assert_eq!(
|
||||
output.data().list(["database"].to_vec()).unwrap(),
|
||||
data.list(["database"].to_vec()).unwrap()
|
||||
);
|
||||
assert_eq!(output.filename, filename);
|
||||
assert!(
|
||||
Duration::from_secs(1) > item.elapsed(),
|
||||
"last_used should have been reset."
|
||||
);
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn retrieve_file_missing() -> Result<(), DBError> {
|
||||
let dir = tempdir().unwrap();
|
||||
let filepath = dir.path().join("justnotthere");
|
||||
let filename = filepath.to_str().unwrap();
|
||||
match Entry::get(filename).await {
|
||||
Ok(_) => Err(DBError::new("should have returned an error")),
|
||||
Err(err) => {
|
||||
assert_eq!(err.to_string(), "read error");
|
||||
assert!(err.source().is_some(), "Error should have a source.");
|
||||
assert!(
|
||||
err.source()
|
||||
.unwrap()
|
||||
.to_string()
|
||||
.contains("could not read file"),
|
||||
"Source Error Message: {}",
|
||||
err.source().unwrap().to_string()
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn retrieve_corrupt_file() -> Result<(), DBError> {
|
||||
let dir = tempdir().unwrap();
|
||||
let filepath = dir.path().join("garbage");
|
||||
let filename = filepath.to_str().unwrap();
|
||||
write(&filepath, b"jhsdfghlsdf").await.unwrap();
|
||||
match Entry::get(filename).await {
|
||||
Ok(_) => Err(DBError::new("should have returned an error")),
|
||||
Err(err) => {
|
||||
assert_eq!(err.to_string(), "read error");
|
||||
assert!(err.source().is_some(), "Error should have a source.");
|
||||
assert!(
|
||||
err.source()
|
||||
.unwrap()
|
||||
.to_string()
|
||||
.contains("file corruption"),
|
||||
"Source Error Message: {}",
|
||||
err.source().unwrap().to_string()
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
Loading…
Reference in New Issue
Block a user