From 5f47f015c478fd575b566ad670582b8dea2954c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Kr=C3=BCger?= Date: Tue, 2 Jan 2024 11:18:05 +0100 Subject: [PATCH] chore: Add some tests --- Cargo.lock | 7 +++++ car-mirror/Cargo.toml | 1 + car-mirror/src/traits.rs | 65 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 6cecc61..679fab7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -404,6 +404,7 @@ dependencies = [ "serde", "serde_ipld_dagcbor", "test-strategy", + "testresult", "thiserror", "tokio", "tracing", @@ -1869,6 +1870,12 @@ dependencies = [ "syn 2.0.28", ] +[[package]] +name = "testresult" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52e045f5cf9ad69772c1c9652f5567a75df88bbb5a1310a64e53cab140c5c459" + [[package]] name = "textwrap" version = "0.16.0" diff --git a/car-mirror/Cargo.toml b/car-mirror/Cargo.toml index 2ddc10b..510354e 100644 --- a/car-mirror/Cargo.toml +++ b/car-mirror/Cargo.toml @@ -49,6 +49,7 @@ car-mirror = { path = ".", features = ["test_utils"] } proptest = "1.1" roaring-graphs = "0.12" test-strategy = "0.3" +testresult = "0.3.0" [features] default = [] diff --git a/car-mirror/src/traits.rs b/car-mirror/src/traits.rs index 46ea717..280b1cb 100644 --- a/car-mirror/src/traits.rs +++ b/car-mirror/src/traits.rs @@ -182,3 +182,68 @@ impl Cache for NoCache { Ok(()) } } + +#[cfg(all(test, feature = "quick_cache"))] +mod quick_cache_tests { + use super::{Cache, InMemoryCache}; + use libipld::{Ipld, IpldCodec}; + use testresult::TestResult; + use wnfs_common::{BlockStore, MemoryBlockStore}; + + #[async_std::test] + async fn test_has_block_cache() -> TestResult { + let store = &MemoryBlockStore::new(); + let cache = InMemoryCache::new(10_000, 150_000); + + let cid = store + .put_block(b"Hello, World!".to_vec(), IpldCodec::Raw.into()) + .await?; + + // Initially, the cache is unpopulated + assert!(!cache.get_has_block_cache(&cid).await?); + + // Then, we populate that cache + assert!(cache.has_block(cid, store).await?); + + // Now, the cache should be populated + assert!(cache.get_has_block_cache(&cid).await?); + + Ok(()) + } + + #[async_std::test] + async fn test_references_cache() -> TestResult { + let store = &MemoryBlockStore::new(); + let cache = InMemoryCache::new(10_000, 150_000); + + let hello_one_cid = store + .put_block(b"Hello, One?".to_vec(), IpldCodec::Raw.into()) + .await?; + let hello_two_cid = store + .put_block(b"Hello, Two?".to_vec(), IpldCodec::Raw.into()) + .await?; + let cid = store + .put_serializable(&Ipld::List(vec![ + Ipld::Link(hello_one_cid), + Ipld::Link(hello_two_cid), + ])) + .await?; + + // Cache unpopulated initially + assert_eq!(cache.get_references_cache(cid).await?, None); + + // This should populate the references cache + assert_eq!( + cache.references(cid, store).await?, + vec![hello_one_cid, hello_two_cid] + ); + + // Cache should now contain the references + assert_eq!( + cache.get_references_cache(cid).await?, + Some(vec![hello_one_cid, hello_two_cid]) + ); + + Ok(()) + } +}