|
| 1 | +//! Storage adapter testing utilities |
| 2 | +//! |
| 3 | +//! rewritten from: |
| 4 | +//! automerge-repo/packages/automerge-repo/src/helpers/tests/storage-adapter-tests.ts |
| 5 | +//! |
| 6 | +//! Provides a test suite for any implementation of the `Storage` trait. |
| 7 | +//! Based on the TypeScript `runStorageAdapterTests` from automerge-repo. |
| 8 | +
|
| 9 | +#![allow(dead_code)] |
| 10 | + |
| 11 | +use rand::Rng; |
| 12 | +use std::future::Future; |
| 13 | +use std::pin::Pin; |
| 14 | +use std::sync::LazyLock; |
| 15 | + |
| 16 | +use super::{Storage, StorageKey}; |
| 17 | + |
| 18 | +pub fn payload_a() -> Vec<u8> { |
| 19 | + vec![0, 1, 127, 99, 154, 235] |
| 20 | +} |
| 21 | + |
| 22 | +pub fn payload_b() -> Vec<u8> { |
| 23 | + vec![1, 76, 160, 53, 57, 10, 230] |
| 24 | +} |
| 25 | + |
| 26 | +pub fn payload_c() -> Vec<u8> { |
| 27 | + vec![2, 111, 74, 131, 236, 96, 142, 193] |
| 28 | +} |
| 29 | + |
| 30 | +static LARGE_PAYLOAD: LazyLock<Vec<u8>> = LazyLock::new(|| { |
| 31 | + let mut vec = vec![0u8; 100000]; |
| 32 | + rand::rng().fill(&mut vec[..]); |
| 33 | + vec |
| 34 | +}); |
| 35 | + |
| 36 | +pub fn large_payload() -> Vec<u8> { |
| 37 | + LARGE_PAYLOAD.clone() |
| 38 | +} |
| 39 | + |
| 40 | +/// Trait for storage test fixtures |
| 41 | +pub trait StorageTestFixture: Sized + Send { |
| 42 | + /// The storage type being tested |
| 43 | + type Storage: Storage + Send + Sync + 'static; |
| 44 | + |
| 45 | + /// Setup the test fixture |
| 46 | + fn setup() -> impl std::future::Future<Output = Self> + Send; |
| 47 | + |
| 48 | + /// Get reference to the storage adapter |
| 49 | + fn storage(&self) -> &Self::Storage; |
| 50 | + |
| 51 | + /// Optional cleanup |
| 52 | + fn teardown(self) -> impl std::future::Future<Output = ()> + Send { |
| 53 | + async {} |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/// Helper to run a single test with setup and teardown |
| 58 | +async fn run_test<F, TestFn>(test_fn: TestFn) |
| 59 | +where |
| 60 | + F: StorageTestFixture, |
| 61 | + TestFn: for<'a> FnOnce(&'a F::Storage) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> + Send, |
| 62 | +{ |
| 63 | + let fixture = F::setup().await; |
| 64 | + test_fn(fixture.storage()).await; |
| 65 | + fixture.teardown().await; |
| 66 | +} |
| 67 | + |
| 68 | +/// Run all storage adapter acceptance tests |
| 69 | +pub async fn run_storage_adapter_tests<F: StorageTestFixture>() { |
| 70 | + run_test::<F, _>(|a| Box::pin(test_load_should_return_none_if_no_data(a))).await; |
| 71 | + run_test::<F, _>(|a| Box::pin(test_save_and_load_should_return_data_that_was_saved(a))).await; |
| 72 | + run_test::<F, _>(|a| Box::pin(test_save_and_load_should_work_with_composite_keys(a))).await; |
| 73 | + run_test::<F, _>(|a| Box::pin(test_save_and_load_should_work_with_large_payload(a))).await; |
| 74 | + run_test::<F, _>(|a| Box::pin(test_load_range_should_return_empty_if_no_data(a))).await; |
| 75 | + run_test::<F, _>(|a| Box::pin(test_save_and_load_range_should_return_all_matching_data(a))) |
| 76 | + .await; |
| 77 | + run_test::<F, _>(|a| Box::pin(test_save_and_load_range_should_only_load_matching_values(a))) |
| 78 | + .await; |
| 79 | + run_test::<F, _>(|a| Box::pin(test_save_and_remove_should_be_empty_after_removing(a))).await; |
| 80 | + run_test::<F, _>(|a| Box::pin(test_save_and_save_should_overwrite(a))).await; |
| 81 | +} |
| 82 | + |
| 83 | +// describe("load") |
| 84 | +pub async fn test_load_should_return_none_if_no_data<S: Storage>(adapter: &S) { |
| 85 | + let actual = adapter |
| 86 | + .load(StorageKey::from_parts(["AAAAA", "sync-state", "xxxxx"]).unwrap()) |
| 87 | + .await; |
| 88 | + |
| 89 | + assert_eq!(actual, None); |
| 90 | +} |
| 91 | + |
| 92 | +// describe("save and load") |
| 93 | +pub async fn test_save_and_load_should_return_data_that_was_saved<S: Storage>(adapter: &S) { |
| 94 | + let key = StorageKey::from_parts(["storage-adapter-id"]).unwrap(); |
| 95 | + adapter.put(key.clone(), payload_a()).await; |
| 96 | + |
| 97 | + let actual = adapter.load(key).await; |
| 98 | + |
| 99 | + assert_eq!(actual, Some(payload_a())); |
| 100 | +} |
| 101 | + |
| 102 | +pub async fn test_save_and_load_should_work_with_composite_keys<S: Storage>(adapter: &S) { |
| 103 | + let key = StorageKey::from_parts(["AAAAA", "sync-state", "xxxxx"]).unwrap(); |
| 104 | + adapter.put(key.clone(), payload_a()).await; |
| 105 | + |
| 106 | + let actual = adapter.load(key).await; |
| 107 | + |
| 108 | + assert_eq!(actual, Some(payload_a())); |
| 109 | +} |
| 110 | + |
| 111 | +pub async fn test_save_and_load_should_work_with_large_payload<S: Storage>(adapter: &S) { |
| 112 | + let key = StorageKey::from_parts(["AAAAA", "sync-state", "xxxxx"]).unwrap(); |
| 113 | + adapter.put(key.clone(), large_payload()).await; |
| 114 | + |
| 115 | + let actual = adapter.load(key).await; |
| 116 | + |
| 117 | + assert_eq!(actual, Some(large_payload())); |
| 118 | +} |
| 119 | + |
| 120 | +// describe("loadRange") |
| 121 | +pub async fn test_load_range_should_return_empty_if_no_data<S: Storage>(adapter: &S) { |
| 122 | + let result = adapter.load_range(StorageKey::from_parts(["AAAAA"]).unwrap()).await; |
| 123 | + |
| 124 | + assert_eq!(result.len(), 0); |
| 125 | +} |
| 126 | + |
| 127 | +// describe("save and loadRange") |
| 128 | +pub async fn test_save_and_load_range_should_return_all_matching_data<S: Storage>(adapter: &S) { |
| 129 | + let key_a = StorageKey::from_parts(["AAAAA", "sync-state", "xxxxx"]).unwrap(); |
| 130 | + let key_b = StorageKey::from_parts(["AAAAA", "snapshot", "yyyyy"]).unwrap(); |
| 131 | + let key_c = StorageKey::from_parts(["AAAAA", "sync-state", "zzzzz"]).unwrap(); |
| 132 | + |
| 133 | + adapter.put(key_a.clone(), payload_a()).await; |
| 134 | + adapter.put(key_b.clone(), payload_b()).await; |
| 135 | + adapter.put(key_c.clone(), payload_c()).await; |
| 136 | + |
| 137 | + let result = adapter.load_range(StorageKey::from_parts(["AAAAA"]).unwrap()).await; |
| 138 | + |
| 139 | + assert_eq!(result.len(), 3); |
| 140 | + assert_eq!(result.get(&key_a), Some(&payload_a())); |
| 141 | + assert_eq!(result.get(&key_b), Some(&payload_b())); |
| 142 | + assert_eq!(result.get(&key_c), Some(&payload_c())); |
| 143 | + |
| 144 | + let sync_result = adapter |
| 145 | + .load_range(StorageKey::from_parts(["AAAAA", "sync-state"]).unwrap()) |
| 146 | + .await; |
| 147 | + |
| 148 | + assert_eq!(sync_result.len(), 2); |
| 149 | + assert_eq!(sync_result.get(&key_a), Some(&payload_a())); |
| 150 | + assert_eq!(sync_result.get(&key_c), Some(&payload_c())); |
| 151 | +} |
| 152 | + |
| 153 | +pub async fn test_save_and_load_range_should_only_load_matching_values<S: Storage>(adapter: &S) { |
| 154 | + let key_a = StorageKey::from_parts(["AAAAA", "sync-state", "xxxxx"]).unwrap(); |
| 155 | + let key_c = StorageKey::from_parts(["BBBBB", "sync-state", "zzzzz"]).unwrap(); |
| 156 | + |
| 157 | + adapter.put(key_a.clone(), payload_a()).await; |
| 158 | + adapter.put(key_c.clone(), payload_c()).await; |
| 159 | + |
| 160 | + let actual = adapter.load_range(StorageKey::from_parts(["AAAAA"]).unwrap()).await; |
| 161 | + |
| 162 | + assert_eq!(actual.len(), 1); |
| 163 | + assert_eq!(actual.get(&key_a), Some(&payload_a())); |
| 164 | +} |
| 165 | + |
| 166 | +// describe("save and remove") |
| 167 | +pub async fn test_save_and_remove_should_be_empty_after_removing<S: Storage>(adapter: &S) { |
| 168 | + let key = StorageKey::from_parts(["AAAAA", "snapshot", "xxxxx"]).unwrap(); |
| 169 | + adapter.put(key.clone(), payload_a()).await; |
| 170 | + adapter.delete(key.clone()).await; |
| 171 | + |
| 172 | + let range_result = adapter.load_range(StorageKey::from_parts(["AAAAA"]).unwrap()).await; |
| 173 | + assert_eq!(range_result.len(), 0); |
| 174 | + |
| 175 | + let load_result = adapter.load(key).await; |
| 176 | + assert_eq!(load_result, None); |
| 177 | +} |
| 178 | + |
| 179 | +// describe("save and save") |
| 180 | +pub async fn test_save_and_save_should_overwrite<S: Storage>(adapter: &S) { |
| 181 | + let key = StorageKey::from_parts(["AAAAA", "sync-state", "xxxxx"]).unwrap(); |
| 182 | + adapter.put(key.clone(), payload_a()).await; |
| 183 | + adapter.put(key.clone(), payload_b()).await; |
| 184 | + |
| 185 | + let result = adapter |
| 186 | + .load_range(StorageKey::from_parts(["AAAAA", "sync-state"]).unwrap()) |
| 187 | + .await; |
| 188 | + |
| 189 | + assert_eq!(result.len(), 1); |
| 190 | + assert_eq!(result.get(&key), Some(&payload_b())); |
| 191 | +} |
0 commit comments