Skip to content

Commit 6fbb535

Browse files
committed
test: add tests for monitoring the status of deployments
1 parent 7da7ff5 commit 6fbb535

File tree

1 file changed

+163
-1
lines changed

1 file changed

+163
-1
lines changed

common/src/subgraph_client/monitor.rs

Lines changed: 163 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct DeploymentStatusResponse {
1818
indexing_statuses: Option<Vec<DeploymentStatus>>,
1919
}
2020

21-
#[derive(Clone, Deserialize, Eq, PartialEq)]
21+
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
2222
pub struct DeploymentStatus {
2323
pub synced: bool,
2424
pub health: String,
@@ -96,3 +96,165 @@ pub fn monitor_deployment_status(
9696
},
9797
)
9898
}
99+
100+
#[cfg(test)]
101+
mod tests {
102+
use std::str::FromStr;
103+
104+
use wiremock::matchers::{method, path};
105+
use wiremock::{Mock, MockServer, ResponseTemplate};
106+
107+
use super::*;
108+
109+
#[tokio::test]
110+
async fn test_parses_synced_and_healthy_response() {
111+
let mock_server = MockServer::start().await;
112+
let status_url: Url = mock_server
113+
.uri()
114+
.parse::<Url>()
115+
.unwrap()
116+
.join("/status")
117+
.unwrap();
118+
let deployment =
119+
DeploymentId::from_str("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA").unwrap();
120+
121+
Mock::given(method("POST"))
122+
.and(path("/status"))
123+
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
124+
"data": {
125+
"indexingStatuses": [
126+
{
127+
"synced": true,
128+
"health": "healthy"
129+
}
130+
]
131+
}
132+
})))
133+
.mount(&mock_server)
134+
.await;
135+
136+
let status = monitor_deployment_status(deployment, status_url);
137+
138+
assert_eq!(
139+
status.value().await.unwrap(),
140+
DeploymentStatus {
141+
synced: true,
142+
health: "healthy".to_string()
143+
}
144+
);
145+
}
146+
147+
#[tokio::test]
148+
async fn test_parses_not_synced_and_healthy_response() {
149+
let mock_server = MockServer::start().await;
150+
let status_url: Url = mock_server
151+
.uri()
152+
.parse::<Url>()
153+
.unwrap()
154+
.join("/status")
155+
.unwrap();
156+
let deployment =
157+
DeploymentId::from_str("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA").unwrap();
158+
159+
Mock::given(method("POST"))
160+
.and(path("/status"))
161+
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
162+
"data": {
163+
"indexingStatuses": [
164+
{
165+
"synced": false,
166+
"health": "healthy"
167+
}
168+
]
169+
}
170+
})))
171+
.mount(&mock_server)
172+
.await;
173+
174+
let status = monitor_deployment_status(deployment, status_url);
175+
176+
assert_eq!(
177+
status.value().await.unwrap(),
178+
DeploymentStatus {
179+
synced: false,
180+
health: "healthy".to_string()
181+
}
182+
);
183+
}
184+
185+
#[tokio::test]
186+
async fn test_parses_synced_and_unhealthy_response() {
187+
let mock_server = MockServer::start().await;
188+
let status_url: Url = mock_server
189+
.uri()
190+
.parse::<Url>()
191+
.unwrap()
192+
.join("/status")
193+
.unwrap();
194+
let deployment =
195+
DeploymentId::from_str("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA").unwrap();
196+
197+
Mock::given(method("POST"))
198+
.and(path("/status"))
199+
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
200+
"data": {
201+
"indexingStatuses": [
202+
{
203+
"synced": true,
204+
"health": "unhealthy"
205+
}
206+
]
207+
}
208+
})))
209+
.mount(&mock_server)
210+
.await;
211+
212+
let status = monitor_deployment_status(deployment, status_url);
213+
214+
assert_eq!(
215+
status.value().await.unwrap(),
216+
DeploymentStatus {
217+
synced: true,
218+
health: "unhealthy".to_string()
219+
}
220+
);
221+
}
222+
223+
#[tokio::test]
224+
async fn test_parses_synced_and_failed_response() {
225+
let mock_server = MockServer::start().await;
226+
let status_url: Url = mock_server
227+
.uri()
228+
.parse::<Url>()
229+
.unwrap()
230+
.join("/status")
231+
.unwrap();
232+
let deployment =
233+
DeploymentId::from_str("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA").unwrap();
234+
235+
Mock::given(method("POST"))
236+
.and(path("/status"))
237+
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
238+
"data": {
239+
"indexingStatuses": [
240+
{
241+
"synced": true,
242+
"health": "failed"
243+
}
244+
]
245+
}
246+
})))
247+
.mount(&mock_server)
248+
.await;
249+
250+
let status = monitor_deployment_status(deployment, status_url);
251+
252+
assert_eq!(
253+
status.value().await.unwrap(),
254+
DeploymentStatus {
255+
synced: true,
256+
health: "failed".to_string()
257+
}
258+
);
259+
}
260+
}

0 commit comments

Comments
 (0)