about summary refs log tree commit diff
path: root/src/database/mod.rs
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2021-08-15 15:13:34 +0300
committerVika <vika@fireburn.ru>2021-08-15 15:13:34 +0300
commit13bcfb013c4a5ac5ea15c7ebe04f243431165c03 (patch)
tree514619680064668bab41dcfcafb6d601f89d6806 /src/database/mod.rs
parentc017b87aea27b61b5b3eb98d2d6cf53ea3d116b6 (diff)
Added a WIP file backend
Currently unavailable for use and only has basic GET and POST operations
implemented. A lot more work is needed to make it truly usable.

Locking is implemented using flock() system call on Linux.
Diffstat (limited to 'src/database/mod.rs')
-rw-r--r--src/database/mod.rs67
1 files changed, 37 insertions, 30 deletions
diff --git a/src/database/mod.rs b/src/database/mod.rs
index 7b144f8..58f0a35 100644
--- a/src/database/mod.rs
+++ b/src/database/mod.rs
@@ -7,6 +7,8 @@ mod redis;
 pub use crate::database::redis::RedisStorage;
 #[cfg(test)]
 pub use redis::tests::{get_redis_instance, RedisInstance};
+mod file;
+pub use crate::database::file::FileStorage;
 
 #[derive(Serialize, Deserialize, PartialEq, Debug)]
 pub struct MicropubChannel {
@@ -133,12 +135,6 @@ pub trait Storage: Clone + Send + Sync {
     /// Note that the `post` object MUST have `post["properties"]["uid"][0]` defined.
     async fn put_post<'a>(&self, post: &'a serde_json::Value, user: &'a str) -> Result<()>;
 
-    /*/// Save a post and add it to the relevant feeds listed in `post["properties"]["channel"]`.
-    ///
-    /// Note that the `post` object MUST have `post["properties"]["uid"][0]` defined
-    /// and `post["properties"]["channel"]` defined, even if it's empty.
-    async fn put_and_index_post<'a>(&mut self, post: &'a serde_json::Value) -> Result<()>;*/
-
     /// Modify a post using an update object as defined in the Micropub spec.
     ///
     /// Note to implementors: the update operation MUST be atomic OR MUST lock the database
@@ -191,6 +187,7 @@ mod tests {
     use super::redis::tests::get_redis_instance;
     use super::{MicropubChannel, Storage};
     use serde_json::json;
+    use paste::paste;
 
     async fn test_backend_basic_operations<Backend: Storage>(backend: Backend) {
         let post: serde_json::Value = json!({
@@ -210,7 +207,7 @@ mod tests {
             .put_post(&post, "https://fireburn.ru/")
             .await
             .unwrap();
-        if let Ok(Some(returned_post)) = backend.get_post(&key).await {
+        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(),
@@ -300,30 +297,40 @@ mod tests {
             "Vika's Hideout"
         );
     }
-
-    #[async_std::test]
-    async fn test_redis_storage_basic_operations() {
-        let redis_instance = get_redis_instance().await;
-        let backend = super::RedisStorage::new(redis_instance.uri().to_string())
-            .await
-            .unwrap();
-        test_backend_basic_operations(backend).await;
-    }
-    #[async_std::test]
-    async fn test_redis_storage_channel_list() {
-        let redis_instance = get_redis_instance().await;
-        let backend = super::RedisStorage::new(redis_instance.uri().to_string())
-            .await
-            .unwrap();
-        test_backend_get_channel_list(backend).await;
+    macro_rules! redis_test {
+        ($func_name:expr) => {
+            paste! {
+                #[async_std::test]
+                async fn [<redis_ $func_name>] () {
+                    test_logger::ensure_env_logger_initialized();
+                    let redis_instance = get_redis_instance().await;
+                    let backend = super::RedisStorage::new(redis_instance.uri().to_string())
+                        .await
+                        .unwrap();
+                    $func_name(backend).await
+                }
+            }
+        }
     }
 
-    #[async_std::test]
-    async fn test_redis_settings() {
-        let redis_instance = get_redis_instance().await;
-        let backend = super::RedisStorage::new(redis_instance.uri().to_string())
-            .await
-            .unwrap();
-        test_backend_settings(backend).await;
+    macro_rules! file_test {
+        ($func_name:expr) => {
+            paste! {
+                #[async_std::test]
+                async fn [<file_ $func_name>] () {
+                    test_logger::ensure_env_logger_initialized();
+                    let tempdir = tempdir::TempDir::new("file").expect("Failed to create tempdir");
+                    let backend = super::FileStorage::new(tempdir.into_path()).await.unwrap();
+                    $func_name(backend).await
+                }
+            }
+        }
     }
+
+    redis_test!(test_backend_basic_operations);
+    redis_test!(test_backend_get_channel_list);
+    redis_test!(test_backend_settings);
+    file_test!(test_backend_basic_operations);
+    file_test!(test_backend_get_channel_list);
+    file_test!(test_backend_settings);
 }