Skip to content

Commit 407c17d

Browse files
committed
test: add tests for subgraph client fallback logic
1 parent 6fbb535 commit 407c17d

File tree

1 file changed

+218
-0
lines changed

1 file changed

+218
-0
lines changed

common/src/subgraph_client/client.rs

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ impl SubgraphClient {
134134

135135
#[cfg(test)]
136136
mod test {
137+
use std::str::FromStr;
138+
137139
use serde_json::json;
138140
use wiremock::matchers::{method, path};
139141
use wiremock::{Mock, MockServer, ResponseTemplate};
@@ -197,4 +199,220 @@ mod test {
197199

198200
assert!(result.data.is_some());
199201
}
202+
203+
#[tokio::test]
204+
async fn test_uses_local_deployment_if_healthy_and_synced() {
205+
let deployment =
206+
DeploymentId::from_str("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA").unwrap();
207+
208+
let mock_server_local = MockServer::start().await;
209+
mock_server_local
210+
.register(
211+
Mock::given(method("POST"))
212+
.and(path("/status"))
213+
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
214+
"data": {
215+
"indexingStatuses": [
216+
{
217+
"synced": true,
218+
"health": "healthy"
219+
}
220+
]
221+
}
222+
}))),
223+
)
224+
.await;
225+
mock_server_local
226+
.register(
227+
Mock::given(method("POST"))
228+
.and(path(&format!("/subgraphs/id/{}", deployment)))
229+
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
230+
"data": {
231+
"user": {
232+
"name": "local"
233+
}
234+
}
235+
}))),
236+
)
237+
.await;
238+
239+
let mock_server_remote = MockServer::start().await;
240+
mock_server_remote
241+
.register(
242+
Mock::given(method("POST"))
243+
.and(path(&format!("/subgraphs/id/{}", deployment)))
244+
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
245+
"data": {
246+
"user": {
247+
"name": "remote"
248+
}
249+
}
250+
}))),
251+
)
252+
.await;
253+
254+
// Create the subgraph client
255+
let client = SubgraphClient::new(
256+
Some(DeploymentDetails::for_graph_node(&mock_server_local.uri(), deployment).unwrap()),
257+
DeploymentDetails::for_query_url(&format!(
258+
"{}/subgraphs/id/{}",
259+
mock_server_remote.uri(),
260+
deployment
261+
))
262+
.unwrap(),
263+
)
264+
.unwrap();
265+
266+
// Query the subgraph
267+
let response: Response<Value> = client
268+
.query(&json!({ "query": "{ user(id: 1} { name } }"}))
269+
.await
270+
.unwrap();
271+
272+
assert_eq!(response.data, Some(json!({ "user": { "name": "local" } })));
273+
}
274+
275+
#[tokio::test]
276+
async fn test_uses_query_url_if_local_deployment_is_unhealthy() {
277+
let deployment =
278+
DeploymentId::from_str("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA").unwrap();
279+
280+
let mock_server_local = MockServer::start().await;
281+
mock_server_local
282+
.register(
283+
Mock::given(method("POST"))
284+
.and(path("/status"))
285+
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
286+
"data": {
287+
"indexingStatuses": [
288+
{
289+
"synced": true,
290+
"health": "unhealthy"
291+
}
292+
]
293+
}
294+
}))),
295+
)
296+
.await;
297+
mock_server_local
298+
.register(
299+
Mock::given(method("POST"))
300+
.and(path(&format!("/subgraphs/id/{}", deployment)))
301+
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
302+
"data": {
303+
"user": {
304+
"name": "local"
305+
}
306+
}
307+
}))),
308+
)
309+
.await;
310+
311+
let mock_server_remote = MockServer::start().await;
312+
mock_server_remote
313+
.register(
314+
Mock::given(method("POST"))
315+
.and(path(&format!("/subgraphs/id/{}", deployment)))
316+
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
317+
"data": {
318+
"user": {
319+
"name": "remote"
320+
}
321+
}
322+
}))),
323+
)
324+
.await;
325+
326+
// Create the subgraph client
327+
let client = SubgraphClient::new(
328+
Some(DeploymentDetails::for_graph_node(&mock_server_local.uri(), deployment).unwrap()),
329+
DeploymentDetails::for_query_url(&format!(
330+
"{}/subgraphs/id/{}",
331+
mock_server_remote.uri(),
332+
deployment
333+
))
334+
.unwrap(),
335+
)
336+
.unwrap();
337+
338+
// Query the subgraph
339+
let response: Response<Value> = client
340+
.query(&json!({ "query": "{ user(id: 1} { name } }"}))
341+
.await
342+
.unwrap();
343+
344+
assert_eq!(response.data, Some(json!({ "user": { "name": "remote" } })));
345+
}
346+
347+
#[tokio::test]
348+
async fn test_uses_query_url_if_local_deployment_is_not_synced() {
349+
let deployment =
350+
DeploymentId::from_str("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA").unwrap();
351+
352+
let mock_server_local = MockServer::start().await;
353+
mock_server_local
354+
.register(
355+
Mock::given(method("POST"))
356+
.and(path("/status"))
357+
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
358+
"data": {
359+
"indexingStatuses": [
360+
{
361+
"synced": false,
362+
"health": "healthy"
363+
}
364+
]
365+
}
366+
}))),
367+
)
368+
.await;
369+
mock_server_local
370+
.register(
371+
Mock::given(method("POST"))
372+
.and(path(&format!("/subgraphs/id/{}", deployment)))
373+
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
374+
"data": {
375+
"user": {
376+
"name": "local"
377+
}
378+
}
379+
}))),
380+
)
381+
.await;
382+
383+
let mock_server_remote = MockServer::start().await;
384+
mock_server_remote
385+
.register(
386+
Mock::given(method("POST"))
387+
.and(path(&format!("/subgraphs/id/{}", deployment)))
388+
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
389+
"data": {
390+
"user": {
391+
"name": "remote"
392+
}
393+
}
394+
}))),
395+
)
396+
.await;
397+
398+
// Create the subgraph client
399+
let client = SubgraphClient::new(
400+
Some(DeploymentDetails::for_graph_node(&mock_server_local.uri(), deployment).unwrap()),
401+
DeploymentDetails::for_query_url(&format!(
402+
"{}/subgraphs/id/{}",
403+
mock_server_remote.uri(),
404+
deployment
405+
))
406+
.unwrap(),
407+
)
408+
.unwrap();
409+
410+
// Query the subgraph
411+
let response: Response<Value> = client
412+
.query(&json!({ "query": "{ user(id: 1} { name } }"}))
413+
.await
414+
.unwrap();
415+
416+
assert_eq!(response.data, Some(json!({ "user": { "name": "remote" } })));
417+
}
200418
}

0 commit comments

Comments
 (0)