Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add unique together to variant image

IKarbowiakcfce1dcc2d09
diff --git a/saleor/graphql/core/tests/test_core.py b/saleor/graphql/core/tests/test_core.py
index 1971be5d2c..714d0b40e4 100644
--- a/saleor/graphql/core/tests/test_core.py
+++ b/saleor/graphql/core/tests/test_core.py
@@ -10,7 +10,7 @@ from graphene import InputField
from ....product.models import Category, Product
from ...product import types as product_types
-from ...tests.utils import _get_graphql_content_from_response, get_graphql_content
+from ...tests.utils import get_graphql_content, get_graphql_content_from_response
from ...utils import get_database_id, requestor_is_superuser
from ...utils.filters import filter_range_field, reporting_period_to_date
from ..enums import ReportingPeriod
@@ -140,7 +140,7 @@ def test_require_pagination(api_client):
}
"""
response = api_client.post_graphql(query)
- content = _get_graphql_content_from_response(response)
+ content = get_graphql_content_from_response(response)
assert "errors" in content
assert content["errors"][0]["message"] == (
"You must provide a `first` or `last` value to properly paginate the "
diff --git a/saleor/graphql/core/tests/test_view.py b/saleor/graphql/core/tests/test_view.py
index 9affffafd2..52ffd30f2d 100644
--- a/saleor/graphql/core/tests/test_view.py
+++ b/saleor/graphql/core/tests/test_view.py
@@ -13,7 +13,7 @@ from ...tests.fixtures import (
ACCESS_CONTROL_ALLOW_ORIGIN,
API_PATH,
)
-from ...tests.utils import _get_graphql_content_from_response, get_graphql_content
+from ...tests.utils import get_graphql_content, get_graphql_content_from_response
def test_batch_queries(category, product, api_client):
@@ -153,7 +153,7 @@ def test_invalid_request_body_non_debug(client):
data = "invalid-data"
response = client.post(API_PATH, data, content_type="application/json")
assert response.status_code == 400
- content = _get_graphql_content_from_response(response)
+ content = get_graphql_content_from_response(response)
assert "errors" in content
@@ -162,7 +162,7 @@ def test_invalid_request_body_with_debug(client):
data = "invalid-data"
response = client.post(API_PATH, data, content_type="application/json")
assert response.status_code == 400
- content = _get_graphql_content_from_response(response)
+ content = get_graphql_content_from_response(response)
errors = content.get("errors")
assert errors == [
{
@@ -176,14 +176,14 @@ def test_invalid_query(api_client):
query = "query { invalid }"
response = api_client.post_graphql(query, check_no_permissions=False)
assert response.status_code == 400
- content = _get_graphql_content_from_response(response)
+ content = get_graphql_content_from_response(response)
assert "errors" in content
def test_no_query(client):
response = client.post(API_PATH, "", content_type="application/json")
assert response.status_code == 400
- content = _get_graphql_content_from_response(response)
+ content = get_graphql_content_from_response(response)
assert content["errors"][0]["message"] == "Must provide a query string."
@@ -191,7 +191,7 @@ def test_query_is_dict(client):
data = {"query": {"type": "dict"}}
response = client.post(API_PATH, data, content_type="application/json")
assert response.status_code == 400
- content = _get_graphql_content_from_response(response)
+ content = get_graphql_content_from_response(response)
assert content["errors"][0]["message"] == "Must provide a query string."
@@ -202,7 +202,7 @@ def test_graphql_execution_exception(monkeypatch, api_client):
monkeypatch.setattr("graphql.backend.core.execute_and_validate", mocked_execute)
response = api_client.post_graphql("{ shop { name }}")
assert response.status_code == 400
- content = _get_graphql_content_from_response(response)
+ content = get_graphql_content_from_response(response)
assert content["errors"][0]["message"] == "Spanish inquisition"
diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.py
index d23c3c1c09..d9de8d2076 100644
--- a/saleor/graphql/product/tests/test_product.py
+++ b/saleor/graphql/product/tests/test_product.py
@@ -35,6 +35,7 @@ from ...core.enums import ReportingPeriod
from ...tests.utils import (
assert_no_permission,
get_graphql_content,
+ get_graphql_content_from_response,
get_multipart_request_body,
)
from ..bulk_mutations.products import ProductVariantStocksUpdate
@@ -3409,6 +3410,35 @@ def test_assign_variant_image(
assert variant.images.first() == image
+def test_assign_variant_image_second_time(
+ staff_api_client, user_api_client, product_with_image, permission_manage_products
+):
+ # given
+ query = ASSIGN_VARIANT_QUERY
+ variant = product_with_image.variants.first()
+ image = product_with_image.images.first()
+
+ image.variant_images.create(variant=variant)
+
+ variables = {
+ "variantId": to_global_id("ProductVariant", variant.pk),
+ "imageId": to_global_id("ProductImage", image.pk),
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content_from_response(response)
+ assert "errors" in content
+ assert (
+ "duplicate key value violates unique constraint"
+ in content["errors"][0]["message"]
+ )
+
+
def test_assign_variant_image_from_different_product(
staff_api_client, user_api_client, product_with_image, permission_manage_products
):
diff --git a/saleor/graphql/tests/utils.py b/saleor/graphql/tests/utils.py
index 7bc4aabaac..d4882978d7 100644
--- a/saleor/graphql/tests/utils.py
+++ b/saleor/graphql/tests/utils.py
@@ -3,7 +3,7 @@ import json
from django.core.serializers.json import DjangoJSONEncoder
-def _get_graphql_content_from_response(response):
+def get_graphql_content_from_response(response):
return json.loads(response.content.decode("utf8"))
@@ -12,14 +12,14 @@ def get_graphql_content(response, *, ignore_errors: bool = False):
contains any operating-related errors, eg. schema errors or lack of
permissions.
"""
- content = _get_graphql_content_from_response(response)
+ content = get_graphql_content_from_response(response)
if not ignore_errors:
assert "errors" not in content, content["errors"]
return content
def assert_no_permission(response):
- content = _get_graphql_content_from_response(response)
+ content = get_graphql_content_from_response(response)
assert "errors" in content, content
assert content["errors"][0]["message"] == (
"You do not have permission to perform this action"
diff --git a/saleor/product/migrations/0123_auto_20200904_1251.py b/saleor/product/migrations/0123_auto_20200904_1251.py
new file mode 100644
index 0000000000..465691feff
--- /dev/null
+++ b/saleor/product/migrations/0123_auto_20200904_1251.py
@@ -0,0 +1,16 @@
+# Generated by Django 3.1 on 2020-09-04 12:51
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("product", "0122_auto_20200828_1135"),
+ ]
+
+ operations = [
+ migrations.AlterUniqueTogether(
+ name="variantimage", unique_together={("variant", "image")},
+ ),
+ ]
diff --git a/saleor/product/models.py b/saleor/product/models.py
index c903572be4..95048858dd 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -814,6 +814,9 @@ class VariantImage(models.Model):
ProductImage, related_name="variant_images", on_delete=models.CASCADE
)
+ class Meta:
+ unique_together = ("variant", "image")
+
class CollectionProduct(SortableModel):
collection = models.ForeignKey(