-
Notifications
You must be signed in to change notification settings - Fork 19
Add storage adapter tests from automerge-repo
#59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jmoggr
wants to merge
1
commit into
alexjg:main
Choose a base branch
from
jmoggr:storage-adapter-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| //! Storage adapter testing utilities | ||
| //! | ||
| //! rewritten from: | ||
| //! automerge-repo/packages/automerge-repo/src/helpers/tests/storage-adapter-tests.ts | ||
| //! | ||
| //! Provides a test suite for any implementation of the `Storage` trait. | ||
| //! Based on the TypeScript `runStorageAdapterTests` from automerge-repo. | ||
|
|
||
| #![allow(dead_code)] | ||
|
|
||
| use rand::Rng; | ||
| use std::future::Future; | ||
| use std::pin::Pin; | ||
| use std::sync::LazyLock; | ||
|
|
||
| use super::{Storage, StorageKey}; | ||
|
|
||
| pub fn payload_a() -> Vec<u8> { | ||
| vec![0, 1, 127, 99, 154, 235] | ||
| } | ||
|
|
||
| pub fn payload_b() -> Vec<u8> { | ||
| vec![1, 76, 160, 53, 57, 10, 230] | ||
| } | ||
|
|
||
| pub fn payload_c() -> Vec<u8> { | ||
| vec![2, 111, 74, 131, 236, 96, 142, 193] | ||
| } | ||
|
|
||
| static LARGE_PAYLOAD: LazyLock<Vec<u8>> = LazyLock::new(|| { | ||
| let mut vec = vec![0u8; 100000]; | ||
| rand::rng().fill(&mut vec[..]); | ||
| vec | ||
| }); | ||
|
|
||
| pub fn large_payload() -> Vec<u8> { | ||
| LARGE_PAYLOAD.clone() | ||
| } | ||
|
|
||
| /// Trait for storage test fixtures | ||
| pub trait StorageTestFixture: Sized + Send { | ||
| /// The storage type being tested | ||
| type Storage: Storage + Send + Sync + 'static; | ||
|
|
||
| /// Setup the test fixture | ||
| fn setup() -> impl std::future::Future<Output = Self> + Send; | ||
|
|
||
| /// Get reference to the storage adapter | ||
| fn storage(&self) -> &Self::Storage; | ||
|
|
||
| /// Optional cleanup | ||
| fn teardown(self) -> impl std::future::Future<Output = ()> + Send { | ||
| async {} | ||
| } | ||
| } | ||
|
|
||
| /// Helper to run a single test with setup and teardown | ||
| async fn run_test<F, TestFn>(test_fn: TestFn) | ||
| where | ||
| F: StorageTestFixture, | ||
| TestFn: for<'a> FnOnce(&'a F::Storage) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> + Send, | ||
| { | ||
| let fixture = F::setup().await; | ||
| test_fn(fixture.storage()).await; | ||
| fixture.teardown().await; | ||
| } | ||
|
|
||
| /// Run all storage adapter acceptance tests | ||
| pub async fn run_storage_adapter_tests<F: StorageTestFixture>() { | ||
| run_test::<F, _>(|a| Box::pin(test_load_should_return_none_if_no_data(a))).await; | ||
| run_test::<F, _>(|a| Box::pin(test_save_and_load_should_return_data_that_was_saved(a))).await; | ||
| run_test::<F, _>(|a| Box::pin(test_save_and_load_should_work_with_composite_keys(a))).await; | ||
| run_test::<F, _>(|a| Box::pin(test_save_and_load_should_work_with_large_payload(a))).await; | ||
| run_test::<F, _>(|a| Box::pin(test_load_range_should_return_empty_if_no_data(a))).await; | ||
| run_test::<F, _>(|a| Box::pin(test_save_and_load_range_should_return_all_matching_data(a))) | ||
| .await; | ||
| run_test::<F, _>(|a| Box::pin(test_save_and_load_range_should_only_load_matching_values(a))) | ||
| .await; | ||
| run_test::<F, _>(|a| Box::pin(test_save_and_remove_should_be_empty_after_removing(a))).await; | ||
| run_test::<F, _>(|a| Box::pin(test_save_and_save_should_overwrite(a))).await; | ||
| } | ||
|
|
||
| // describe("load") | ||
| pub async fn test_load_should_return_none_if_no_data<S: Storage>(adapter: &S) { | ||
| let actual = adapter | ||
| .load(StorageKey::from_parts(["AAAAA", "sync-state", "xxxxx"]).unwrap()) | ||
| .await; | ||
|
|
||
| assert_eq!(actual, None); | ||
| } | ||
|
|
||
| // describe("save and load") | ||
| pub async fn test_save_and_load_should_return_data_that_was_saved<S: Storage>(adapter: &S) { | ||
| let key = StorageKey::from_parts(["storage-adapter-id"]).unwrap(); | ||
| adapter.put(key.clone(), payload_a()).await; | ||
|
|
||
| let actual = adapter.load(key).await; | ||
|
|
||
| assert_eq!(actual, Some(payload_a())); | ||
| } | ||
|
|
||
| pub async fn test_save_and_load_should_work_with_composite_keys<S: Storage>(adapter: &S) { | ||
| let key = StorageKey::from_parts(["AAAAA", "sync-state", "xxxxx"]).unwrap(); | ||
| adapter.put(key.clone(), payload_a()).await; | ||
|
|
||
| let actual = adapter.load(key).await; | ||
|
|
||
| assert_eq!(actual, Some(payload_a())); | ||
| } | ||
|
|
||
| pub async fn test_save_and_load_should_work_with_large_payload<S: Storage>(adapter: &S) { | ||
| let key = StorageKey::from_parts(["AAAAA", "sync-state", "xxxxx"]).unwrap(); | ||
| adapter.put(key.clone(), large_payload()).await; | ||
|
|
||
| let actual = adapter.load(key).await; | ||
|
|
||
| assert_eq!(actual, Some(large_payload())); | ||
| } | ||
|
|
||
| // describe("loadRange") | ||
| pub async fn test_load_range_should_return_empty_if_no_data<S: Storage>(adapter: &S) { | ||
| let result = adapter.load_range(StorageKey::from_parts(["AAAAA"]).unwrap()).await; | ||
|
|
||
| assert_eq!(result.len(), 0); | ||
| } | ||
|
|
||
| // describe("save and loadRange") | ||
| pub async fn test_save_and_load_range_should_return_all_matching_data<S: Storage>(adapter: &S) { | ||
| let key_a = StorageKey::from_parts(["AAAAA", "sync-state", "xxxxx"]).unwrap(); | ||
| let key_b = StorageKey::from_parts(["AAAAA", "snapshot", "yyyyy"]).unwrap(); | ||
| let key_c = StorageKey::from_parts(["AAAAA", "sync-state", "zzzzz"]).unwrap(); | ||
|
|
||
| adapter.put(key_a.clone(), payload_a()).await; | ||
| adapter.put(key_b.clone(), payload_b()).await; | ||
| adapter.put(key_c.clone(), payload_c()).await; | ||
|
|
||
| let result = adapter.load_range(StorageKey::from_parts(["AAAAA"]).unwrap()).await; | ||
|
|
||
| assert_eq!(result.len(), 3); | ||
| assert_eq!(result.get(&key_a), Some(&payload_a())); | ||
| assert_eq!(result.get(&key_b), Some(&payload_b())); | ||
| assert_eq!(result.get(&key_c), Some(&payload_c())); | ||
|
|
||
| let sync_result = adapter | ||
| .load_range(StorageKey::from_parts(["AAAAA", "sync-state"]).unwrap()) | ||
| .await; | ||
|
|
||
| assert_eq!(sync_result.len(), 2); | ||
| assert_eq!(sync_result.get(&key_a), Some(&payload_a())); | ||
| assert_eq!(sync_result.get(&key_c), Some(&payload_c())); | ||
| } | ||
|
|
||
| pub async fn test_save_and_load_range_should_only_load_matching_values<S: Storage>(adapter: &S) { | ||
| let key_a = StorageKey::from_parts(["AAAAA", "sync-state", "xxxxx"]).unwrap(); | ||
| let key_c = StorageKey::from_parts(["BBBBB", "sync-state", "zzzzz"]).unwrap(); | ||
|
|
||
| adapter.put(key_a.clone(), payload_a()).await; | ||
| adapter.put(key_c.clone(), payload_c()).await; | ||
|
|
||
| let actual = adapter.load_range(StorageKey::from_parts(["AAAAA"]).unwrap()).await; | ||
|
|
||
| assert_eq!(actual.len(), 1); | ||
| assert_eq!(actual.get(&key_a), Some(&payload_a())); | ||
| } | ||
|
|
||
| // describe("save and remove") | ||
| pub async fn test_save_and_remove_should_be_empty_after_removing<S: Storage>(adapter: &S) { | ||
| let key = StorageKey::from_parts(["AAAAA", "snapshot", "xxxxx"]).unwrap(); | ||
| adapter.put(key.clone(), payload_a()).await; | ||
| adapter.delete(key.clone()).await; | ||
|
|
||
| let range_result = adapter.load_range(StorageKey::from_parts(["AAAAA"]).unwrap()).await; | ||
| assert_eq!(range_result.len(), 0); | ||
|
|
||
| let load_result = adapter.load(key).await; | ||
| assert_eq!(load_result, None); | ||
| } | ||
|
|
||
| // describe("save and save") | ||
| pub async fn test_save_and_save_should_overwrite<S: Storage>(adapter: &S) { | ||
| let key = StorageKey::from_parts(["AAAAA", "sync-state", "xxxxx"]).unwrap(); | ||
| adapter.put(key.clone(), payload_a()).await; | ||
| adapter.put(key.clone(), payload_b()).await; | ||
|
|
||
| let result = adapter | ||
| .load_range(StorageKey::from_parts(["AAAAA", "sync-state"]).unwrap()) | ||
| .await; | ||
|
|
||
| assert_eq!(result.len(), 1); | ||
| assert_eq!(result.get(&key), Some(&payload_b())); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| mod in_memory_tests { | ||
| use samod::storage::{testing::StorageTestFixture, InMemoryStorage}; | ||
|
|
||
| fn init_logging() { | ||
| let _ = tracing_subscriber::fmt() | ||
| .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) | ||
| .try_init(); | ||
| } | ||
|
|
||
| struct InMemoryStorageFixture { | ||
| storage: InMemoryStorage, | ||
| } | ||
|
|
||
| impl StorageTestFixture for InMemoryStorageFixture { | ||
| type Storage = InMemoryStorage; | ||
|
|
||
| async fn setup() -> Self { | ||
| Self { | ||
| storage: InMemoryStorage::new(), | ||
| } | ||
| } | ||
|
|
||
| fn storage(&self) -> &Self::Storage { | ||
| &self.storage | ||
| } | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn in_memory_storage_standard_tests() { | ||
| init_logging(); | ||
| samod::storage::testing::run_storage_adapter_tests::<InMemoryStorageFixture>().await; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be behind a feature flag which we call
testing.