mirror of https://github.com/zkat/cacache-rs.git
test(get): add examples and tests for get::data* family
This commit is contained in:
parent
d08629cf55
commit
a040a743a8
114
src/get.rs
114
src/get.rs
|
|
@ -107,6 +107,23 @@ where
|
||||||
|
|
||||||
/// Reads the entire contents of a cache file into a bytes vector, looking the
|
/// Reads the entire contents of a cache file into a bytes vector, looking the
|
||||||
/// data up by key.
|
/// data up by key.
|
||||||
|
///
|
||||||
|
/// ## Example
|
||||||
|
/// ```no_run
|
||||||
|
/// # use async_std::prelude::*;
|
||||||
|
/// # use async_std::task;
|
||||||
|
/// # fn main() -> Result<(), cacache::Error> {
|
||||||
|
/// # task::block_on(async {
|
||||||
|
/// # example().await.unwrap();
|
||||||
|
/// # });
|
||||||
|
/// # Ok(())
|
||||||
|
/// # }
|
||||||
|
/// #
|
||||||
|
/// # async fn example() -> Result<(), cacache::Error> {
|
||||||
|
/// let data = cacache::get::data("./my-cache", "my-key").await?;
|
||||||
|
/// # Ok(())
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
pub async fn data<P, K>(cache: P, key: K) -> Result<Vec<u8>, Error>
|
pub async fn data<P, K>(cache: P, key: K) -> Result<Vec<u8>, Error>
|
||||||
where
|
where
|
||||||
P: AsRef<Path>,
|
P: AsRef<Path>,
|
||||||
|
|
@ -121,6 +138,24 @@ where
|
||||||
|
|
||||||
/// Reads the entire contents of a cache file into a bytes vector, looking the
|
/// Reads the entire contents of a cache file into a bytes vector, looking the
|
||||||
/// data up by its content address.
|
/// data up by its content address.
|
||||||
|
///
|
||||||
|
/// ## Example
|
||||||
|
/// ```no_run
|
||||||
|
/// # use async_std::prelude::*;
|
||||||
|
/// # use async_std::task;
|
||||||
|
/// # fn main() -> Result<(), cacache::Error> {
|
||||||
|
/// # task::block_on(async {
|
||||||
|
/// # example().await.unwrap();
|
||||||
|
/// # });
|
||||||
|
/// # Ok(())
|
||||||
|
/// # }
|
||||||
|
/// #
|
||||||
|
/// # async fn example() -> Result<(), cacache::Error> {
|
||||||
|
/// let sri = cacache::put::data("./my-cache", "my-key", b"hello").await?;
|
||||||
|
/// let data = cacache::get::data_hash("./my-cache", &sri).await?;
|
||||||
|
/// # Ok(())
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
pub async fn data_hash<P>(cache: P, sri: &Integrity) -> Result<Vec<u8>, Error>
|
pub async fn data_hash<P>(cache: P, sri: &Integrity) -> Result<Vec<u8>, Error>
|
||||||
where
|
where
|
||||||
P: AsRef<Path>,
|
P: AsRef<Path>,
|
||||||
|
|
@ -242,8 +277,17 @@ where
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads the entire contents of a cache file into a bytes vector, looking the
|
/// Reads the entire contents of a cache file synchronously into a bytes
|
||||||
/// data up by key.
|
/// vector, looking the data up by key.
|
||||||
|
///
|
||||||
|
/// ## Example
|
||||||
|
/// ```no_run
|
||||||
|
/// # fn main() -> Result<(), cacache::Error> {
|
||||||
|
/// # use std::io::Read;
|
||||||
|
/// let data = cacache::get::data_sync("./my-cache", "my-key")?;
|
||||||
|
/// # Ok(())
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
pub fn data_sync<P, K>(cache: P, key: K) -> Result<Vec<u8>, Error>
|
pub fn data_sync<P, K>(cache: P, key: K) -> Result<Vec<u8>, Error>
|
||||||
where
|
where
|
||||||
P: AsRef<Path>,
|
P: AsRef<Path>,
|
||||||
|
|
@ -256,8 +300,18 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads the entire contents of a cache file into a bytes vector, looking the
|
/// Reads the entire contents of a cache file synchronously into a bytes
|
||||||
/// data up by its content address.
|
/// vector, looking the data up by its content address.
|
||||||
|
///
|
||||||
|
/// ## Example
|
||||||
|
/// ```no_run
|
||||||
|
/// # fn main() -> Result<(), cacache::Error> {
|
||||||
|
/// # use std::io::Read;
|
||||||
|
/// let sri = cacache::put::data_sync("./my-cache", "my-key", b"hello")?;
|
||||||
|
/// let data = cacache::get::data_hash_sync("./my-cache", &sri)?;
|
||||||
|
/// # Ok(())
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
pub fn data_hash_sync<P>(cache: P, sri: &Integrity) -> Result<Vec<u8>, Error>
|
pub fn data_hash_sync<P>(cache: P, sri: &Integrity) -> Result<Vec<u8>, Error>
|
||||||
where
|
where
|
||||||
P: AsRef<Path>,
|
P: AsRef<Path>,
|
||||||
|
|
@ -322,7 +376,7 @@ mod tests {
|
||||||
handle.read_to_string(&mut str).await.unwrap();
|
handle.read_to_string(&mut str).await.unwrap();
|
||||||
handle.check().unwrap();
|
handle.check().unwrap();
|
||||||
assert_eq!(str, String::from("hello world"));
|
assert_eq!(str, String::from("hello world"));
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -339,7 +393,7 @@ mod tests {
|
||||||
handle.read_to_string(&mut str).await.unwrap();
|
handle.read_to_string(&mut str).await.unwrap();
|
||||||
handle.check().unwrap();
|
handle.check().unwrap();
|
||||||
assert_eq!(str, String::from("hello world"));
|
assert_eq!(str, String::from("hello world"));
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -369,4 +423,52 @@ mod tests {
|
||||||
handle.check().unwrap();
|
handle.check().unwrap();
|
||||||
assert_eq!(str, String::from("hello world"));
|
assert_eq!(str, String::from("hello world"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_data() {
|
||||||
|
task::block_on(async {
|
||||||
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
|
let dir = tmp.path().to_owned();
|
||||||
|
crate::put::data(&dir, "my-key", b"hello world")
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let data = crate::get::data(&dir, "my-key").await.unwrap();
|
||||||
|
assert_eq!(data, b"hello world");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_data_hash() {
|
||||||
|
task::block_on(async {
|
||||||
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
|
let dir = tmp.path().to_owned();
|
||||||
|
let sri = crate::put::data(&dir, "my-key", b"hello world")
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let data = crate::get::data_hash(&dir, &sri).await.unwrap();
|
||||||
|
assert_eq!(data, b"hello world");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_data_sync() {
|
||||||
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
|
let dir = tmp.path().to_owned();
|
||||||
|
crate::put::data_sync(&dir, "my-key", b"hello world").unwrap();
|
||||||
|
|
||||||
|
let data = crate::get::data_sync(&dir, "my-key").unwrap();
|
||||||
|
assert_eq!(data, b"hello world");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_data_hash_sync() {
|
||||||
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
|
let dir = tmp.path().to_owned();
|
||||||
|
let sri = crate::put::data_sync(&dir, "my-key", b"hello world").unwrap();
|
||||||
|
|
||||||
|
let data = crate::get::data_hash_sync(&dir, &sri).unwrap();
|
||||||
|
assert_eq!(data, b"hello world");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue