Bläddra i källkod

providing with ApiResponse enum

added a test for that enum
develop
Kyrylo Stepanov 2 år sedan
förälder
incheckning
2382f9be1c
1 ändrade filer med 40 tillägg och 0 borttagningar
  1. 40
    0
      common-api/src/api.rs

+ 40
- 0
common-api/src/api.rs Visa fil

@@ -41,6 +41,14 @@ pub struct NodeInfo {
41 41
     pub account_id: AccountId,
42 42
 }
43 43
 
44
+#[derive(Debug, Serialize, Deserialize)]
45
+#[serde(tag = "response", content = "body")]
46
+#[serde(rename_all = "snake_case")]
47
+enum ApiResponse<T> {
48
+    Success(T),
49
+    Timeout,
50
+}
51
+
44 52
 #[cfg(test)]
45 53
 mod tests {
46 54
 
@@ -70,4 +78,36 @@ mod tests {
70 78
 
71 79
         assert_eq!(s1, s2);
72 80
     }
81
+
82
+    #[test]
83
+    fn api_response_parsing() {
84
+        let test_data = serde_json::to_value(&ApiResponse::Success(NodeInfo {
85
+            account_id: AccountId::from_str("account.testnet").unwrap(),
86
+            bandwidth: Bandwidth {
87
+                speed: 100,
88
+                units: "mbps".to_owned(),
89
+            },
90
+            region: "us".to_owned(),
91
+        }))
92
+        .unwrap();
93
+
94
+        let test_data_match = serde_json::json!({
95
+            "response": "success",
96
+            "body": {
97
+                "account_id": "account.testnet",
98
+                "region": "us",
99
+                "bandwidth": {
100
+                    "speed": 100,
101
+                    "units": "mbps"
102
+                }
103
+            }
104
+        });
105
+        assert_eq!(test_data, test_data_match);
106
+
107
+        let test_data = serde_json::to_value(&ApiResponse::<()>::Timeout).unwrap();
108
+        let test_data_match = serde_json::json!({
109
+            "response": "timeout",
110
+        });
111
+        assert_eq!(test_data, test_data_match);
112
+    }
73 113
 }

Laddar…
Avbryt
Spara