|
| 1 | +use super::types::*; |
| 2 | +use crate::blocking::StudioClient; |
| 3 | +use crate::operations::readme::publish::ReadmePublishInput; |
| 4 | +use crate::shared::GraphRef; |
| 5 | +use crate::RoverClientError; |
| 6 | +use graphql_client::*; |
| 7 | + |
| 8 | +type Timestamp = String; |
| 9 | + |
| 10 | +#[derive(GraphQLQuery)] |
| 11 | +// The paths are relative to the directory where your `Cargo.toml` is located. |
| 12 | +// Both json and the GraphQL schema language are supported as sources for the schema |
| 13 | +#[graphql( |
| 14 | + query_path = "src/operations/readme/publish/publish_mutation.graphql", |
| 15 | + schema_path = ".schema/schema.graphql", |
| 16 | + response_derives = "PartialEq, Debug, Serialize, Deserialize", |
| 17 | + deprecated = "warn" |
| 18 | +)] |
| 19 | + |
| 20 | +pub struct ReadmePublishMutation; |
| 21 | + |
| 22 | +pub fn run( |
| 23 | + input: ReadmePublishInput, |
| 24 | + client: &StudioClient, |
| 25 | +) -> Result<ReadmePublishResponse, RoverClientError> { |
| 26 | + let graph_ref = input.graph_ref.clone(); |
| 27 | + let response = client.post::<ReadmePublishMutation>(input.into())?; |
| 28 | + build_response(response, graph_ref) |
| 29 | +} |
| 30 | + |
| 31 | +fn build_response( |
| 32 | + data: readme_publish_mutation::ResponseData, |
| 33 | + graph_ref: GraphRef, |
| 34 | +) -> Result<ReadmePublishResponse, RoverClientError> { |
| 35 | + let readme = data |
| 36 | + .graph |
| 37 | + .ok_or(RoverClientError::GraphNotFound { |
| 38 | + graph_ref: graph_ref.clone(), |
| 39 | + })? |
| 40 | + .variant |
| 41 | + .ok_or(RoverClientError::GraphNotFound { |
| 42 | + graph_ref: graph_ref.clone(), |
| 43 | + })? |
| 44 | + .update_variant_readme |
| 45 | + .ok_or(RoverClientError::MalformedResponse { |
| 46 | + null_field: "update_variant_readme".to_string(), |
| 47 | + })? |
| 48 | + .readme; |
| 49 | + Ok(ReadmePublishResponse { |
| 50 | + graph_ref, |
| 51 | + new_content: readme.content, |
| 52 | + last_updated_time: readme.last_updated_time, |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | +#[cfg(test)] |
| 57 | +mod tests { |
| 58 | + use super::*; |
| 59 | + use crate::shared::GraphRef; |
| 60 | + use serde_json::json; |
| 61 | + |
| 62 | + fn mock_graph_ref() -> GraphRef { |
| 63 | + GraphRef { |
| 64 | + name: "mygraph".to_string(), |
| 65 | + variant: "current".to_string(), |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + #[test] |
| 70 | + fn get_new_readme_from_response_data_works() { |
| 71 | + let content = "this is a readme"; |
| 72 | + let last_updated_time = "2022-05-12T20:50:06.687276000Z"; |
| 73 | + |
| 74 | + let json_response = json!({ |
| 75 | + "graph": { |
| 76 | + "variant": { |
| 77 | + "updateVariantReadme": { |
| 78 | + "readme": { |
| 79 | + "content": content, |
| 80 | + "lastUpdatedTime": last_updated_time, |
| 81 | + } |
| 82 | + } |
| 83 | + }, |
| 84 | + } |
| 85 | + }); |
| 86 | + let data = serde_json::from_value(json_response).unwrap(); |
| 87 | + let graph_ref = mock_graph_ref(); |
| 88 | + let output = build_response(data, graph_ref.clone()); |
| 89 | + |
| 90 | + let expected = ReadmePublishResponse { |
| 91 | + graph_ref, |
| 92 | + new_content: content.to_string(), |
| 93 | + last_updated_time: Some(last_updated_time.to_string()), |
| 94 | + }; |
| 95 | + assert!(output.is_ok()); |
| 96 | + assert_eq!(output.unwrap(), expected); |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn get_readme_errs_with_no_variant() { |
| 101 | + let json_response = json!({ "graph": { "variant": null }}); |
| 102 | + let data = serde_json::from_value(json_response).unwrap(); |
| 103 | + let output = build_response(data, mock_graph_ref()); |
| 104 | + assert!(output.is_err()); |
| 105 | + } |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn null_update_variant_readme_errors() { |
| 109 | + let json_response = json!({ |
| 110 | + "graph": { |
| 111 | + "variant": { |
| 112 | + "updateVariantReadme": null |
| 113 | + }, |
| 114 | + } |
| 115 | + }); |
| 116 | + let data = serde_json::from_value(json_response).unwrap(); |
| 117 | + let output = build_response(data, mock_graph_ref()); |
| 118 | + |
| 119 | + assert!(output.is_err()); |
| 120 | + } |
| 121 | +} |
0 commit comments