Skip to content

Commit

Permalink
test: add tests for monitoring the status of deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
Jannis committed Oct 25, 2023
1 parent 9b6ff55 commit 3e86e97
Showing 1 changed file with 163 additions and 1 deletion.
164 changes: 163 additions & 1 deletion common/src/subgraph_client/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct DeploymentStatusResponse {
indexing_statuses: Option<Vec<DeploymentStatus>>,
}

#[derive(Clone, Deserialize, Eq, PartialEq)]
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
pub struct DeploymentStatus {
pub synced: bool,
pub health: String,
Expand Down Expand Up @@ -96,3 +96,165 @@ pub fn monitor_deployment_status(
},
)
}

#[cfg(test)]
mod tests {
use std::str::FromStr;

use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

use super::*;

#[tokio::test]
async fn test_parses_synced_and_healthy_response() {
let mock_server = MockServer::start().await;
let status_url: Url = mock_server
.uri()
.parse::<Url>()
.unwrap()
.join("/status")
.unwrap();
let deployment =
DeploymentId::from_str("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA").unwrap();

Mock::given(method("POST"))
.and(path("/status"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"data": {
"indexingStatuses": [
{
"synced": true,
"health": "healthy"
}
]
}
})))
.mount(&mock_server)
.await;

let status = monitor_deployment_status(deployment, status_url);

assert_eq!(
status.value().await.unwrap(),
DeploymentStatus {
synced: true,
health: "healthy".to_string()
}
);
}

#[tokio::test]
async fn test_parses_not_synced_and_healthy_response() {
let mock_server = MockServer::start().await;
let status_url: Url = mock_server
.uri()
.parse::<Url>()
.unwrap()
.join("/status")
.unwrap();
let deployment =
DeploymentId::from_str("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA").unwrap();

Mock::given(method("POST"))
.and(path("/status"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"data": {
"indexingStatuses": [
{
"synced": false,
"health": "healthy"
}
]
}
})))
.mount(&mock_server)
.await;

let status = monitor_deployment_status(deployment, status_url);

assert_eq!(
status.value().await.unwrap(),
DeploymentStatus {
synced: false,
health: "healthy".to_string()
}
);
}

#[tokio::test]
async fn test_parses_synced_and_unhealthy_response() {
let mock_server = MockServer::start().await;
let status_url: Url = mock_server
.uri()
.parse::<Url>()
.unwrap()
.join("/status")
.unwrap();
let deployment =
DeploymentId::from_str("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA").unwrap();

Mock::given(method("POST"))
.and(path("/status"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"data": {
"indexingStatuses": [
{
"synced": true,
"health": "unhealthy"
}
]
}
})))
.mount(&mock_server)
.await;

let status = monitor_deployment_status(deployment, status_url);

assert_eq!(
status.value().await.unwrap(),
DeploymentStatus {
synced: true,
health: "unhealthy".to_string()
}
);
}

#[tokio::test]
async fn test_parses_synced_and_failed_response() {
let mock_server = MockServer::start().await;
let status_url: Url = mock_server
.uri()
.parse::<Url>()
.unwrap()
.join("/status")
.unwrap();
let deployment =
DeploymentId::from_str("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA").unwrap();

Mock::given(method("POST"))
.and(path("/status"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"data": {
"indexingStatuses": [
{
"synced": true,
"health": "failed"
}
]
}
})))
.mount(&mock_server)
.await;

let status = monitor_deployment_status(deployment, status_url);

assert_eq!(
status.value().await.unwrap(),
DeploymentStatus {
synced: true,
health: "failed".to_string()
}
);
}
}

0 comments on commit 3e86e97

Please sign in to comment.