|
| 1 | +use serde_json::json; |
| 2 | +use shopify_function::prelude::*; |
| 3 | +use shopify_function::Result; |
| 4 | + |
| 5 | +use cart_fetch::output::{ |
| 6 | + FunctionCartFetchResult, HttpRequest as CartHttpRequest, |
| 7 | + HttpRequestMethod as CartHttpRequestMethod, HttpRequestPolicy as CartHttpRequestPolicy, |
| 8 | +}; |
| 9 | + |
| 10 | +use delivery_fetch::output::FunctionDeliveryFetchResult; |
| 11 | +use delivery_fetch::output::{ |
| 12 | + HttpRequest as DeliveryHttpRequest, HttpRequestMethod as DeliveryHttpRequestMethod, |
| 13 | + HttpRequestPolicy as DeliveryHttpRequestPolicy, |
| 14 | +}; |
| 15 | + |
| 16 | +type CartFetchResponseData = cart_fetch::input::ResponseData; |
| 17 | +type DeliveryFetchResponseData = delivery_fetch::input::ResponseData; |
| 18 | +type CartHttpRequestHeader = cart_fetch::output::HttpRequestHeader; |
| 19 | +type DeliveryHttpRequestHeader = delivery_fetch::output::HttpRequestHeader; |
| 20 | + |
| 21 | +#[shopify_function_target( |
| 22 | + target = "cart_fetch", |
| 23 | + query_path = "src/fetch.graphql", |
| 24 | + schema_path = "schema.graphql" |
| 25 | +)] |
| 26 | +fn cart_fetch(input: CartFetchResponseData) -> Result<FunctionCartFetchResult> { |
| 27 | + let body = json!({ |
| 28 | + "enteredDiscountCodes": input.entered_discount_codes |
| 29 | + }); |
| 30 | + |
| 31 | + Ok(FunctionCartFetchResult { |
| 32 | + request: Some(CartHttpRequest { |
| 33 | + body: Some(body.to_string()), |
| 34 | + headers: vec![CartHttpRequestHeader { |
| 35 | + name: "Accept".to_string(), |
| 36 | + value: "application/json; charset=utf-8".to_string(), |
| 37 | + }], |
| 38 | + json_body: Some(body.clone()), |
| 39 | + method: CartHttpRequestMethod::POST, |
| 40 | + policy: CartHttpRequestPolicy { |
| 41 | + read_timeout_ms: 2000, |
| 42 | + }, |
| 43 | + url: "http://localhost:3000".to_string(), |
| 44 | + }), |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +#[shopify_function_target( |
| 49 | + target = "delivery_fetch", |
| 50 | + query_path = "src/fetch.graphql", |
| 51 | + schema_path = "schema.graphql" |
| 52 | +)] |
| 53 | +fn delivery_fetch(input: DeliveryFetchResponseData) -> Result<FunctionDeliveryFetchResult> { |
| 54 | + let body = json!({ |
| 55 | + "enteredDiscountCodes": input.entered_discount_codes |
| 56 | + }); |
| 57 | + |
| 58 | + Ok(FunctionDeliveryFetchResult { |
| 59 | + request: Some(DeliveryHttpRequest { |
| 60 | + body: Some(body.to_string()), |
| 61 | + headers: vec![DeliveryHttpRequestHeader { |
| 62 | + name: "Accept".to_string(), |
| 63 | + value: "application/json; charset=utf-8".to_string(), |
| 64 | + }], |
| 65 | + json_body: Some(body.clone()), |
| 66 | + method: DeliveryHttpRequestMethod::POST, |
| 67 | + policy: DeliveryHttpRequestPolicy { |
| 68 | + read_timeout_ms: 2000, |
| 69 | + }, |
| 70 | + url: "http://localhost:3000".to_string(), |
| 71 | + }), |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +#[cfg(test)] |
| 76 | +mod tests { |
| 77 | + use super::*; |
| 78 | + use shopify_function::{run_function_with_input, Result}; |
| 79 | + |
| 80 | + fn get_fetch_input_json() -> serde_json::Value { |
| 81 | + json!({ |
| 82 | + "enteredDiscountCodes": ["WELCOME10"] |
| 83 | + }) |
| 84 | + } |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn test_cart_fetch() -> Result<()> { |
| 88 | + let fetch_input = get_fetch_input_json(); |
| 89 | + |
| 90 | + let expected_fetch_result = FunctionCartFetchResult { |
| 91 | + request: Some(CartHttpRequest { |
| 92 | + body: Some(fetch_input.to_string()), |
| 93 | + headers: vec![CartHttpRequestHeader { |
| 94 | + name: "Accept".to_string(), |
| 95 | + value: "application/json; charset=utf-8".to_string(), |
| 96 | + }], |
| 97 | + json_body: Some(fetch_input.clone()), |
| 98 | + method: CartHttpRequestMethod::POST, |
| 99 | + policy: CartHttpRequestPolicy { |
| 100 | + read_timeout_ms: 2000, |
| 101 | + }, |
| 102 | + url: "http://localhost:3000".to_string(), |
| 103 | + }), |
| 104 | + }; |
| 105 | + |
| 106 | + assert_eq!( |
| 107 | + run_function_with_input(cart_fetch, &fetch_input.to_string())?, |
| 108 | + expected_fetch_result |
| 109 | + ); |
| 110 | + Ok(()) |
| 111 | + } |
| 112 | + |
| 113 | + #[test] |
| 114 | + fn test_delivery_fetch() -> Result<()> { |
| 115 | + let fetch_input = get_fetch_input_json(); |
| 116 | + |
| 117 | + let expected_fetch_result = FunctionDeliveryFetchResult { |
| 118 | + request: Some(DeliveryHttpRequest { |
| 119 | + body: Some(fetch_input.to_string()), |
| 120 | + headers: vec![DeliveryHttpRequestHeader { |
| 121 | + name: "Accept".to_string(), |
| 122 | + value: "application/json; charset=utf-8".to_string(), |
| 123 | + }], |
| 124 | + json_body: Some(fetch_input.clone()), |
| 125 | + method: DeliveryHttpRequestMethod::POST, |
| 126 | + policy: DeliveryHttpRequestPolicy { |
| 127 | + read_timeout_ms: 2000, |
| 128 | + }, |
| 129 | + url: "http://localhost:3000".to_string(), |
| 130 | + }), |
| 131 | + }; |
| 132 | + |
| 133 | + assert_eq!( |
| 134 | + run_function_with_input(delivery_fetch, &fetch_input.to_string())?, |
| 135 | + expected_fetch_result |
| 136 | + ); |
| 137 | + Ok(()) |
| 138 | + } |
| 139 | +} |
0 commit comments