Skip to content

Commit

Permalink
chore: Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Jan 2, 2024
1 parent 0917467 commit 5f47f01
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions car-mirror/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
65 changes: 65 additions & 0 deletions car-mirror/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}

0 comments on commit 5f47f01

Please sign in to comment.