diff options
author | Vika <vika@fireburn.ru> | 2021-09-26 01:35:00 +0300 |
---|---|---|
committer | Vika <vika@fireburn.ru> | 2021-09-26 02:06:55 +0300 |
commit | 572435f7c8e1d613983309eca268c3f87ec5f00f (patch) | |
tree | e0c134c994379e037928f4e20e95f5fbc0bfc0da /src/database/mod.rs | |
parent | 13bcfb013c4a5ac5ea15c7ebe04f243431165c03 (diff) | |
download | kittybox-572435f7c8e1d613983309eca268c3f87ec5f00f.tar.zst |
Added file updates and fixed a bug with truncated JSON files
There was a bug where `File::write()` would not write the entire buffer, and this condition was left unchecked by the code. All `File::write()` calls are now replaced with `File::write_all()` which ensures the whole buffer is written to the backing file. Additionally added a smoke check for the file updates. It is in no way comprehensive nor it is able to catch all the possible failures but it's a good way of testing the functionality without way too much hassle.
Diffstat (limited to 'src/database/mod.rs')
-rw-r--r-- | src/database/mod.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/database/mod.rs b/src/database/mod.rs index 58f0a35..e6873b0 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -252,6 +252,58 @@ mod tests { } } + /// Note: this is merely a smoke check and is in no way comprehensive. + async fn test_backend_update<Backend: Storage>(backend: Backend) { + let post: serde_json::Value = json!({ + "type": ["h-entry"], + "properties": { + "content": ["Test content"], + "author": ["https://fireburn.ru/"], + "uid": ["https://fireburn.ru/posts/hello"], + "url": ["https://fireburn.ru/posts/hello", "https://fireburn.ru/posts/test"] + } + }); + let key = post["properties"]["uid"][0].as_str().unwrap().to_string(); + + // Reading and writing + backend + .put_post(&post, "https://fireburn.ru/") + .await + .unwrap(); + + backend.update_post(&key, json!({ + "url": &key, + "add": { + "category": ["testing"], + }, + "replace": { + "content": ["Different test content"] + } + })).await.unwrap(); + + if let Some(returned_post) = backend.get_post(&key).await.unwrap() { + assert!(returned_post.is_object()); + assert_eq!( + returned_post["type"].as_array().unwrap().len(), + post["type"].as_array().unwrap().len() + ); + assert_eq!( + returned_post["type"].as_array().unwrap(), + post["type"].as_array().unwrap() + ); + assert_eq!( + returned_post["properties"]["content"][0].as_str().unwrap(), + "Different test content" + ); + assert_eq!( + returned_post["properties"]["category"].as_array().unwrap(), + &vec![json!("testing")] + ); + } else { + panic!("For some reason the backend did not return the post.") + } + } + async fn test_backend_get_channel_list<Backend: Storage>(backend: Backend) { let feed = json!({ "type": ["h-feed"], @@ -330,7 +382,9 @@ mod tests { redis_test!(test_backend_basic_operations); redis_test!(test_backend_get_channel_list); redis_test!(test_backend_settings); + redis_test!(test_backend_update); file_test!(test_backend_basic_operations); file_test!(test_backend_get_channel_list); file_test!(test_backend_settings); + file_test!(test_backend_update); } |