Skip to content

Commit 846eeac

Browse files
committed
test: add tests for SupergraphSdlQuery
1 parent 4106b19 commit 846eeac

File tree

3 files changed

+118
-43
lines changed

3 files changed

+118
-43
lines changed

crates/apollo-mcp-registry/src/uplink.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,44 @@ where
413413
let response_body: graphql_client::Response<Query::ResponseData> = res.json().await?;
414414
Ok(response_body)
415415
}
416+
417+
#[cfg(test)]
418+
mod test {
419+
use super::*;
420+
use futures::stream::StreamExt;
421+
use secrecy::SecretString;
422+
use std::str::FromStr;
423+
use std::time::Duration;
424+
use url::Url;
425+
426+
#[tokio::test]
427+
async fn test_stream_from_uplink() {
428+
for url in &[GCP_URL, AWS_URL] {
429+
if let (Ok(apollo_key), Ok(apollo_graph_ref)) = (
430+
std::env::var("TEST_APOLLO_KEY"),
431+
std::env::var("TEST_APOLLO_GRAPH_REF"),
432+
) {
433+
let results =
434+
stream_from_uplink::<schema::SupergraphSdlQuery, String>(UplinkConfig {
435+
apollo_key: SecretString::from(apollo_key),
436+
apollo_graph_ref,
437+
endpoints: Some(Endpoints::fallback(vec![
438+
Url::from_str(url).expect("url must be valid"),
439+
])),
440+
poll_interval: Duration::from_secs(1),
441+
timeout: Duration::from_secs(5),
442+
})
443+
.take(1)
444+
.collect::<Vec<_>>()
445+
.await;
446+
447+
let schema = results
448+
.first()
449+
.unwrap_or_else(|| panic!("expected one result from {url}"))
450+
.as_ref()
451+
.unwrap_or_else(|_| panic!("schema should be OK from {url}"));
452+
assert!(schema.contains("type Product"))
453+
}
454+
}
455+
}
456+
}

crates/apollo-mcp-registry/src/uplink/schema.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ use std::pin::Pin;
99
use std::time::Duration;
1010

1111
use crate::uplink::UplinkConfig;
12-
use crate::uplink::schema::schema_stream::SupergraphSdlQuery;
1312
use crate::uplink::stream_from_uplink;
1413
use derive_more::Display;
1514
use derive_more::From;
1615
use educe::Educe;
1716
use event::Event;
1817
use event::Event::{NoMoreSchema, UpdateSchema};
1918
use futures::prelude::*;
19+
pub(crate) use schema_stream::SupergraphSdlQuery;
2020
use url::Url;
2121

2222
/// Represents the new state of a schema after an update.

crates/apollo-mcp-registry/src/uplink/schema/schema_stream.rs

Lines changed: 76 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -99,47 +99,81 @@ impl From<supergraph_sdl_query::ResponseData> for UplinkResponse<SchemaState> {
9999

100100
#[cfg(test)]
101101
mod test {
102-
use std::str::FromStr;
103-
use std::time::Duration;
104-
105-
use futures::stream::StreamExt;
106-
use secrecy::SecretString;
107-
use url::Url;
108-
109-
use crate::uplink::AWS_URL;
110-
use crate::uplink::Endpoints;
111-
use crate::uplink::GCP_URL;
112-
use crate::uplink::UplinkConfig;
113-
use crate::uplink::schema::schema_stream::SupergraphSdlQuery;
114-
use crate::uplink::stream_from_uplink;
115-
116-
#[tokio::test]
117-
async fn integration_test() {
118-
for url in &[GCP_URL, AWS_URL] {
119-
if let (Ok(apollo_key), Ok(apollo_graph_ref)) = (
120-
std::env::var("TEST_APOLLO_KEY"),
121-
std::env::var("TEST_APOLLO_GRAPH_REF"),
122-
) {
123-
let results = stream_from_uplink::<SupergraphSdlQuery, String>(UplinkConfig {
124-
apollo_key: SecretString::from(apollo_key),
125-
apollo_graph_ref,
126-
endpoints: Some(Endpoints::fallback(vec![
127-
Url::from_str(url).expect("url must be valid"),
128-
])),
129-
poll_interval: Duration::from_secs(1),
130-
timeout: Duration::from_secs(5),
131-
})
132-
.take(1)
133-
.collect::<Vec<_>>()
134-
.await;
135-
136-
let schema = results
137-
.first()
138-
.unwrap_or_else(|| panic!("expected one result from {url}"))
139-
.as_ref()
140-
.unwrap_or_else(|_| panic!("schema should be OK from {url}"));
141-
assert!(schema.contains("type Product"))
142-
}
143-
}
102+
use super::*;
103+
104+
#[test]
105+
fn test_uplink_request_to_graphql_variables() {
106+
let request = UplinkRequest {
107+
api_key: "test_key".to_string(),
108+
graph_ref: "test_ref".to_string(),
109+
id: Some("test_id".to_string()),
110+
};
111+
112+
let variables: supergraph_sdl_query::Variables = request.into();
113+
114+
assert_eq!(variables.api_key, "test_key");
115+
assert_eq!(variables.graph_ref, "test_ref");
116+
assert_eq!(variables.if_after_id, Some("test_id".to_string()));
117+
}
118+
119+
#[test]
120+
fn test_graphql_response_to_uplink_response_new() {
121+
let response = supergraph_sdl_query::ResponseData {
122+
router_config: SupergraphSdlQueryRouterConfig::RouterConfigResult(
123+
supergraph_sdl_query::SupergraphSdlQueryRouterConfigOnRouterConfigResult {
124+
supergraph_sdl: "test_sdl".to_string(),
125+
id: "result_id".to_string(),
126+
min_delay_seconds: 42.0,
127+
},
128+
),
129+
};
130+
131+
let uplink_response: UplinkResponse<String> = response.into();
132+
133+
assert!(matches!(
134+
uplink_response,
135+
UplinkResponse::New { response, id, delay }
136+
if response == "test_sdl" && id == "result_id" && delay == 42
137+
));
138+
}
139+
140+
#[test]
141+
fn test_graphql_response_to_uplink_response_unchanged() {
142+
let response = supergraph_sdl_query::ResponseData {
143+
router_config: SupergraphSdlQueryRouterConfig::Unchanged(
144+
supergraph_sdl_query::SupergraphSdlQueryRouterConfigOnUnchanged {
145+
id: "unchanged_id".to_string(),
146+
min_delay_seconds: 30.0,
147+
},
148+
),
149+
};
150+
151+
let uplink_response: UplinkResponse<String> = response.into();
152+
153+
assert!(matches!(
154+
uplink_response,
155+
UplinkResponse::Unchanged { id, delay }
156+
if id == Some("unchanged_id".to_string()) && delay == Some(30)
157+
));
158+
}
159+
160+
#[test]
161+
fn test_graphql_response_to_uplink_response_error() {
162+
let response = supergraph_sdl_query::ResponseData {
163+
router_config: SupergraphSdlQueryRouterConfig::FetchError(
164+
supergraph_sdl_query::SupergraphSdlQueryRouterConfigOnFetchError {
165+
code: FetchErrorCode::RETRY_LATER,
166+
message: "Try again later".to_string(),
167+
},
168+
),
169+
};
170+
171+
let uplink_response: UplinkResponse<String> = response.into();
172+
173+
assert!(matches!(
174+
uplink_response,
175+
UplinkResponse::Error { retry_later, code, message }
176+
if retry_later && code == "RETRY_LATER" && message == "Try again later"
177+
));
144178
}
145179
}

0 commit comments

Comments
 (0)