Skip to content

Commit

Permalink
Fix metafields when checkout metadata is missing (saleor#13651)
Browse files Browse the repository at this point in the history
Previous implementation would return `"{}"` instead of expected `null`.
  • Loading branch information
patrys authored Aug 9, 2023
1 parent 123afcf commit 65349fa
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
65 changes: 65 additions & 0 deletions saleor/graphql/checkout/tests/test_checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -2381,3 +2381,68 @@ def test_checkout_balance(
+ transaction.charge_pending_value
- checkout_with_prices.total.gross.amount
)


def test_checkout_metadata(checkout, user_api_client):
# given
checkout.metadata_storage.metadata = {"foo": "bar"}
checkout.metadata_storage.save()

query = """
query getCheckout($id: ID) {
checkout(id: $id) {
metadata {
key
value
}
foo: metafield(key: "foo")
nonexistent: metafield(key: "nonexistent")
metafields
}
}
"""
variables = {"id": to_global_id_or_none(checkout)}

# when
response = user_api_client.post_graphql(
query,
variables,
)

# then
content = get_graphql_content(response)
assert content["data"]["checkout"]["metadata"] == [{"key": "foo", "value": "bar"}]
assert content["data"]["checkout"]["foo"] == "bar"
assert content["data"]["checkout"]["nonexistent"] is None
assert content["data"]["checkout"]["metafields"] == {"foo": "bar"}


def test_checkout_no_metadata(checkout, user_api_client):
# given
checkout.metadata_storage.delete()

query = """
query getCheckout($id: ID) {
checkout(id: $id) {
metadata {
key
value
}
metafield(key: "foo")
metafields
}
}
"""
variables = {"id": to_global_id_or_none(checkout)}

# when
response = user_api_client.post_graphql(
query,
variables,
)

# then
content = get_graphql_content(response)
assert content["data"]["checkout"]["metadata"] == []
assert content["data"]["checkout"]["metafield"] is None
assert content["data"]["checkout"]["metafields"] == {}
4 changes: 2 additions & 2 deletions saleor/graphql/checkout/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ def resolve_metafield(root: models.Checkout, info, *, key: str):
.then(
lambda metadata_storage: metadata_storage.metadata.get(key)
if metadata_storage
else {}
else None
)
)

Expand Down Expand Up @@ -993,7 +993,7 @@ def resolve_private_metafield_with_privilege_check(metadata_storage):
metadata_storage
)
if metadata_storage
else {}
else None
)
)

Expand Down

0 comments on commit 65349fa

Please sign in to comment.