From 572435f7c8e1d613983309eca268c3f87ec5f00f Mon Sep 17 00:00:00 2001 From: Vika Date: Sun, 26 Sep 2021 01:35:00 +0300 Subject: 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. --- src/database/mod.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'src/database/mod.rs') 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: 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: 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); } -- cgit 1.4.1