Skip to content

Commit

Permalink
Merge pull request #81 from Shopify/ap.json_value_tests
Browse files Browse the repository at this point in the history
Use json_value comparison for tests
  • Loading branch information
adampetro authored Jun 21, 2022
2 parents 44e23d5 + a4c2241 commit 3a90167
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 319 deletions.
50 changes: 11 additions & 39 deletions discounts/rust/order-discounts/default/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,64 +43,36 @@ mod tests {
use super::*;

fn input(configuration: Option<Configuration>) -> input::Input {
let input = r#"
{
"discountNode": {
"metafield": null
}
}
"#;
let default_input: input::Input = serde_json::from_str(input).unwrap();
let discount_node = input::DiscountNode {
metafield: configuration.map(|value| {
let value = serde_json::to_string(&value).unwrap();
input::Metafield { value }
}),
};

input::Input {
discount_node,
..default_input
}
input::Input { discount_node }
}

#[test]
fn test_discount_with_no_configuration() {
let input = input(None);
let handle_result = serde_json::json!(function(input).unwrap());

let expected_json = r#"
{
"discounts": [],
"discountApplicationStrategy": "FIRST"
}
"#;

let expected_handle_result: serde_json::Value =
serde_json::from_str(expected_json).unwrap();
assert_eq!(
handle_result.to_string(),
expected_handle_result.to_string()
);
let expected_handle_result = serde_json::json!({
"discounts": [],
"discountApplicationStrategy": "FIRST",
});
assert_eq!(handle_result, expected_handle_result);
}

#[test]
fn test_discount_with_configuration() {
let input = input(Some(Configuration {}));
let handle_result = serde_json::json!(function(input).unwrap());

let expected_json = r#"
{
"discounts": [],
"discountApplicationStrategy": "FIRST"
}
"#;

let expected_handle_result: serde_json::Value =
serde_json::from_str(expected_json).unwrap();
assert_eq!(
handle_result.to_string(),
expected_handle_result.to_string()
);
let expected_handle_result = serde_json::json!({
"discounts": [],
"discountApplicationStrategy": "FIRST",
});
assert_eq!(handle_result, expected_handle_result);
}
}
50 changes: 11 additions & 39 deletions discounts/rust/product-discounts/default/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,64 +43,36 @@ mod tests {
use super::*;

fn input(configuration: Option<Configuration>) -> input::Input {
let input = r#"
{
"discountNode": {
"metafield": null
}
}
"#;
let default_input: input::Input = serde_json::from_str(input).unwrap();
let discount_node = input::DiscountNode {
metafield: configuration.map(|value| {
let value = serde_json::to_string(&value).unwrap();
input::Metafield { value }
}),
};

input::Input {
discount_node,
..default_input
}
input::Input { discount_node }
}

#[test]
fn test_discount_with_no_configuration() {
let input = input(None);
let handle_result = serde_json::json!(function(input).unwrap());

let expected_json = r#"
{
"discounts": [],
"discountApplicationStrategy": "FIRST"
}
"#;

let expected_handle_result: serde_json::Value =
serde_json::from_str(expected_json).unwrap();
assert_eq!(
handle_result.to_string(),
expected_handle_result.to_string()
);
let expected_handle_result = serde_json::json!({
"discounts": [],
"discountApplicationStrategy": "FIRST",
});
assert_eq!(handle_result, expected_handle_result);
}

#[test]
fn test_discount_with_configuration() {
let input = input(Some(Configuration {}));
let handle_result = serde_json::json!(function(input).unwrap());

let expected_json = r#"
{
"discounts": [],
"discountApplicationStrategy": "FIRST"
}
"#;

let expected_handle_result: serde_json::Value =
serde_json::from_str(expected_json).unwrap();
assert_eq!(
handle_result.to_string(),
expected_handle_result.to_string()
);
let expected_handle_result = serde_json::json!({
"discounts": [],
"discountApplicationStrategy": "FIRST",
});
assert_eq!(handle_result, expected_handle_result);
}
}
50 changes: 11 additions & 39 deletions discounts/rust/shipping-discounts/default/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,64 +43,36 @@ mod tests {
use super::*;

fn input(configuration: Option<Configuration>) -> input::Input {
let input = r#"
{
"discountNode": {
"metafield": null
}
}
"#;
let default_input: input::Input = serde_json::from_str(input).unwrap();
let discount_node = input::DiscountNode {
metafield: configuration.map(|value| {
let value = serde_json::to_string(&value).unwrap();
input::Metafield { value }
}),
};

input::Input {
discount_node,
..default_input
}
input::Input { discount_node }
}

#[test]
fn test_discount_with_no_configuration() {
let input = input(None);
let handle_result = serde_json::json!(function(input).unwrap());

let expected_json = r#"
{
"discounts": [],
"discountApplicationStrategy": "FIRST"
}
"#;

let expected_handle_result: serde_json::Value =
serde_json::from_str(expected_json).unwrap();
assert_eq!(
handle_result.to_string(),
expected_handle_result.to_string()
);
let expected_handle_result = serde_json::json!({
"discounts": [],
"discountApplicationStrategy": "FIRST",
});
assert_eq!(handle_result, expected_handle_result);
}

#[test]
fn test_discount_with_configuration() {
let input = input(Some(Configuration {}));
let handle_result = serde_json::json!(function(input).unwrap());

let expected_json = r#"
{
"discounts": [],
"discountApplicationStrategy": "FIRST"
}
"#;

let expected_handle_result: serde_json::Value =
serde_json::from_str(expected_json).unwrap();
assert_eq!(
handle_result.to_string(),
expected_handle_result.to_string()
);
let expected_handle_result = serde_json::json!({
"discounts": [],
"discountApplicationStrategy": "FIRST",
});
assert_eq!(handle_result, expected_handle_result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,14 @@ mod tests {
let input = input(None);
let handle_result = serde_json::json!(function(input).unwrap());

let expected_json = r#"
{
"discounts": [{
"targets": [{ "orderSubtotal": { "excludedVariantIds": [] } }],
"value": { "percentage": { "value": 50.0 } }
}],
"discountApplicationStrategy": "FIRST"
}
"#;

let expected_handle_result: serde_json::Value =
serde_json::from_str(expected_json).unwrap();
assert_eq!(
handle_result.to_string(),
expected_handle_result.to_string()
);
let expected_handle_result = serde_json::json!({
"discounts": [{
"targets": [{ "orderSubtotal": { "excludedVariantIds": [] } }],
"value": { "percentage": { "value": 50.0 } },
}],
"discountApplicationStrategy": "FIRST",
});
assert_eq!(handle_result, expected_handle_result);
}

#[test]
Expand All @@ -107,18 +99,14 @@ mod tests {
}));
let result = serde_json::json!(function(input).unwrap());

let expected_json = r#"
{
"discounts": [{
"targets": [{ "orderSubtotal": { "excludedVariantIds": [] } }],
"value": { "percentage": { "value": 10.0 } }
}],
"discountApplicationStrategy": "FIRST"
}
"#;

let expected_result: serde_json::Value = serde_json::from_str(expected_json).unwrap();
assert_eq!(result.to_string(), expected_result.to_string());
let expected_result = serde_json::json!({
"discounts": [{
"targets": [{ "orderSubtotal": { "excludedVariantIds": [] } }],
"value": { "percentage": { "value": 10.0 } },
}],
"discountApplicationStrategy": "FIRST",
});
assert_eq!(result, expected_result);
}

#[test]
Expand All @@ -129,17 +117,13 @@ mod tests {
}));
let result = serde_json::json!(function(input).unwrap());

let expected_json = r#"
{
"discounts": [{
"targets": [{ "orderSubtotal": { "excludedVariantIds": ["gid://shopify/ProductVariant/1"] } }],
"value": { "percentage": { "value": 50.0 } }
}],
"discountApplicationStrategy": "FIRST"
}
"#;

let expected_result: serde_json::Value = serde_json::from_str(expected_json).unwrap();
assert_eq!(result.to_string(), expected_result.to_string());
let expected_result = serde_json::json!({
"discounts": [{
"targets": [{ "orderSubtotal": { "excludedVariantIds": ["gid://shopify/ProductVariant/1"] } }],
"value": { "percentage": { "value": 50.0 } },
}],
"discountApplicationStrategy": "FIRST",
});
assert_eq!(result, expected_result);
}
}
Loading

0 comments on commit 3a90167

Please sign in to comment.