From 571a1d54c6a000f27c6c1eb4c4b52f9aa5479fba Mon Sep 17 00:00:00 2001 From: Chris Goller Date: Tue, 5 Jan 2021 21:34:08 -0600 Subject: [PATCH] tests: unit tests for image and text Signed-off-by: Chris Goller --- Cargo.lock | 1 + Cargo.toml | 3 ++ src/data/ipfs_ops.rs | 95 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 4e0849c..d73958a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1514,6 +1514,7 @@ dependencies = [ "serde", "serde_json", "sled", + "tempfile", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index b86f9c5..bf976fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,3 +46,6 @@ infer = "0.3" ipfs-embed = { git = "https://github.com/ipfs-rust/ipfs-embed.git", rev = "a852c5f637dfb08a97d5a08a08cc86e81f5c9d96" } libipld = "0.8" # ipld-collections = "0.3.0" + +[dev-dependencies] +tempfile = "3.1" diff --git a/src/data/ipfs_ops.rs b/src/data/ipfs_ops.rs index de8d987..5d66d52 100644 --- a/src/data/ipfs_ops.rs +++ b/src/data/ipfs_ops.rs @@ -73,3 +73,98 @@ pub async fn load_file( Ok(data.content) } + +#[cfg(test)] +mod tests { + use super::*; + use crate::data::ipfs_client::IpfsClient; + + use async_std::{sync::Mutex, task::block_on}; + use tempfile::tempdir; + + use std::{error::Error, fs::File}; + use std::{io::Write, path::Path}; + + /// Helper to create file in a directory and return full path. + fn write_file

(dir: P, data: &[u8], file_name: &str) -> Result> + where + P: AsRef, + { + let path = dir.as_ref().join(file_name); + let mut file = File::create(&path)?; + file.write_all(data)?; + Ok(path) + } + + fn new_client() -> Result> { + block_on(async { + Ok(Arc::new(Mutex::new( + IpfsClient::new() + .await + .map_err(|e| Arc::try_unwrap(e).unwrap())?, + ))) + }) + } + + #[test] + fn test_store_load() -> Result<(), Box> { + let dir = tempdir()?; + let client_ref = new_client()?; + + struct Test { + name: &'static str, + data: &'static [u8], + file_name: &'static str, + expected: ContentItem, + }; + let tests = vec![ + Test { + name: "round-trip smallest possible gif", + data: b"GIF89a\x01\0\x01\0\0\0\0;", + file_name: "smallest.gif", + expected: ContentItem::Image( + ImageContent { + buffer: Box::new(*b"GIF89a\x01\0\x01\0\0\0\0;"), + }, + ImageMetadata { + size_bytes: 14, + mime_type: "image/gif".into(), + width_px: 1, + height_px: 1, + }, + ), + }, + Test { + name: "round-trip text file", + data: b"howdy", + file_name: "howdy.txt", + expected: ContentItem::Text( + TextContent { + string: "howdy".into(), + }, + TextMetadata { size_bytes: 5 }, + ), + }, + ]; + + for test in tests.into_iter() { + let client_ref = client_ref.clone(); + block_on(async { + let path = write_file(dir.path(), test.data, test.file_name)?; + let cid = store_file(path, client_ref.clone()) + .await + .map_err(|e| Arc::try_unwrap(e).unwrap())? + .unwrap(); + + let actual = load_file(cid.to_string(), client_ref) + .await + .map_err(|e| Arc::try_unwrap(e).unwrap())?; + + assert_eq!(test.expected, actual, "{}", test.name); + Ok(()) + }) + .map_err(|e: Box| e)?; + } + Ok(()) + } +}