Skip to content

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

Fix flaky tests that defines pk for objects

IKarbowiak287fc421759c
diff --git a/saleor/discount/tests/test_discounts.py b/saleor/discount/tests/test_discounts.py
index 6775f5b37a..6fc8cbaac1 100644
--- a/saleor/discount/tests/test_discounts.py
+++ b/saleor/discount/tests/test_discounts.py
@@ -245,7 +245,6 @@ def test_sale_applies_to_correct_products(product_type, category, channel_USD):
name="Test Product",
slug="test-product",
description={},
- pk=111,
product_type=product_type,
category=category,
)
@@ -263,7 +262,7 @@ def test_sale_applies_to_correct_products(product_type, category, channel_USD):
product_type=product_type,
category=category,
)
- sec_variant = ProductVariant.objects.create(product=product2, sku="secvar", pk=111)
+ sec_variant = ProductVariant.objects.create(product=product2, sku="secvar")
ProductVariantChannelListing.objects.create(
variant=sec_variant,
channel=channel_USD,
diff --git a/saleor/graphql/core/tests/test_core_reordering.py b/saleor/graphql/core/tests/test_core_reordering.py
index ed1c98c2b5..adc1f7eb9a 100644
--- a/saleor/graphql/core/tests/test_core_reordering.py
+++ b/saleor/graphql/core/tests/test_core_reordering.py
@@ -169,12 +169,8 @@ def test_reordering_null_sort_orders(dummy_attribute):
non_null_sorted_entries = list(
qs.bulk_create(
[
- SortedModel(
- pk=1, attribute=attribute, slug="1", name="1", sort_order=1
- ),
- SortedModel(
- pk=2, attribute=attribute, slug="2", name="2", sort_order=0
- ),
+ SortedModel(attribute=attribute, slug="1", name="1", sort_order=1),
+ SortedModel(attribute=attribute, slug="2", name="2", sort_order=0),
]
)
)
@@ -182,26 +178,20 @@ def test_reordering_null_sort_orders(dummy_attribute):
null_sorted_entries = list(
qs.bulk_create(
[
- SortedModel(
- pk=5, attribute=attribute, slug="5", name="5", sort_order=None
- ),
- SortedModel(
- pk=4, attribute=attribute, slug="4", name="4", sort_order=None
- ),
- SortedModel(
- pk=3, attribute=attribute, slug="3", name="3", sort_order=None
- ),
+ SortedModel(attribute=attribute, slug="3", name="3", sort_order=None),
+ SortedModel(attribute=attribute, slug="4", name="4", sort_order=None),
+ SortedModel(attribute=attribute, slug="5", name="5", sort_order=None),
]
)
)
- operations = {null_sorted_entries[0].pk: -2}
+ operations = {null_sorted_entries[2].pk: -2}
expected = [
(non_null_sorted_entries[1].pk, 0),
(non_null_sorted_entries[0].pk, 1),
- (null_sorted_entries[0].pk, 2),
- (null_sorted_entries[2].pk, 3),
+ (null_sorted_entries[2].pk, 2),
+ (null_sorted_entries[0].pk, 3),
(null_sorted_entries[1].pk, 4),
]
@@ -250,12 +240,8 @@ def test_reordering_concurrently(dummy_attribute, assert_num_queries):
entries = list(
qs.bulk_create(
[
- SortedModel(
- pk=1, attribute=attribute, slug="1", name="1", sort_order=0
- ),
- SortedModel(
- pk=2, attribute=attribute, slug="2", name="2", sort_order=1
- ),
+ SortedModel(attribute=attribute, slug="1", name="1", sort_order=0),
+ SortedModel(attribute=attribute, slug="2", name="2", sort_order=1),
]
)
)
@@ -275,10 +261,12 @@ def test_reordering_concurrently(dummy_attribute, assert_num_queries):
)
assert ctx[1]["sql"] == (
'UPDATE "attribute_attributevalue" '
- 'SET "sort_order" = (CASE WHEN ("attribute_attributevalue"."id" = 1) '
- 'THEN 1 WHEN ("attribute_attributevalue"."id" = 2) '
+ 'SET "sort_order" = '
+ f'(CASE WHEN ("attribute_attributevalue"."id" = {entries[0].pk}) '
+ f'THEN 1 WHEN ("attribute_attributevalue"."id" = {entries[1].pk}) '
"THEN 0 ELSE NULL END)::integer "
- 'WHERE "attribute_attributevalue"."id" IN (1, 2)'
+ 'WHERE "attribute_attributevalue"."id" '
+ f"IN ({entries[0].pk}, {entries[1].pk})"
)
@@ -294,12 +282,8 @@ def test_reordering_deleted_node_from_concurrent(dummy_attribute, assert_num_que
entries = list(
qs.bulk_create(
[
- SortedModel(
- pk=1, attribute=attribute, slug="1", name="1", sort_order=0
- ),
- SortedModel(
- pk=2, attribute=attribute, slug="2", name="2", sort_order=1
- ),
+ SortedModel(attribute=attribute, slug="1", name="1", sort_order=0),
+ SortedModel(attribute=attribute, slug="2", name="2", sort_order=1),
]
)
)
@@ -311,8 +295,9 @@ def test_reordering_deleted_node_from_concurrent(dummy_attribute, assert_num_que
assert ctx[1]["sql"] == (
'UPDATE "attribute_attributevalue" '
- 'SET "sort_order" = (CASE WHEN ("attribute_attributevalue"."id" = 1) '
- 'THEN 1 WHEN ("attribute_attributevalue"."id" = 2) '
+ 'SET "sort_order" = '
+ f'(CASE WHEN ("attribute_attributevalue"."id" = {entries[0].pk}) '
+ f'THEN 1 WHEN ("attribute_attributevalue"."id" = {entries[1].pk}) '
"THEN 0 ELSE NULL END)::integer "
- 'WHERE "attribute_attributevalue"."id" IN (1, 2)'
+ f'WHERE "attribute_attributevalue"."id" IN ({entries[0].pk}, {entries[1].pk})'
)
diff --git a/saleor/graphql/core/tests/test_graphql.py b/saleor/graphql/core/tests/test_graphql.py
index 3d29e5f362..5d1c8eeec5 100644
--- a/saleor/graphql/core/tests/test_graphql.py
+++ b/saleor/graphql/core/tests/test_graphql.py
@@ -234,7 +234,7 @@ def test_get_nodes(product_list):
assert products == product_list
# Raise an error if requested id has no related database object
- nonexistent_item = Mock(type="Product", pk=123)
+ nonexistent_item = Mock(type="Product", pk=-1)
nonexistent_item_global_id = to_global_id(
nonexistent_item.type, nonexistent_item.pk
)
@@ -249,7 +249,7 @@ def test_get_nodes(product_list):
global_ids.pop()
# Raise an error if one of the node is of wrong type
- invalid_item = Mock(type="test", pk=123)
+ invalid_item = Mock(type="test", pk=-1)
invalid_item_global_id = to_global_id(invalid_item.type, invalid_item.pk)
global_ids.append(invalid_item_global_id)
with pytest.raises(GraphQLError) as exc:
diff --git a/saleor/graphql/product/tests/test_product_sorting.py b/saleor/graphql/product/tests/test_product_sorting.py
index 7a1ab330be..449d701db5 100644
--- a/saleor/graphql/product/tests/test_product_sorting.py
+++ b/saleor/graphql/product/tests/test_product_sorting.py
@@ -5,7 +5,7 @@ import graphene
import pytest
from freezegun import freeze_time
-from ....product.models import Product, ProductChannelListing
+from ....product.models import CollectionProduct, Product, ProductChannelListing
from ...tests.utils import get_graphql_content
COLLECTION_RESORT_QUERY = """
@@ -92,13 +92,26 @@ def test_sort_products_within_collection(
collection_id = graphene.Node.to_global_id("Collection", published_collection.pk)
products = collection_with_products
- product = graphene.Node.to_global_id("Product", products[0].pk)
- second_product = graphene.Node.to_global_id("Product", products[1].pk)
- third_product = graphene.Node.to_global_id("Product", products[2].pk)
+ collection = products[0].collections.first()
+ collection_products = list(collection.collectionproduct.all())
+
+ collection_prod_1 = collection_products[0]
+ collection_prod_2 = collection_products[1]
+ collection_prod_3 = collection_products[2]
+
+ collection_prod_1.sort_order = 0
+ collection_prod_2.sort_order = 1
+ collection_prod_3.sort_order = 2
+
+ CollectionProduct.objects.bulk_update(collection_products, ["sort_order"])
+
+ product = graphene.Node.to_global_id("Product", collection_prod_1.product_id)
+ second_product = graphene.Node.to_global_id("Product", collection_prod_2.product_id)
+ third_product = graphene.Node.to_global_id("Product", collection_prod_3.product_id)
variables = {
"collectionId": collection_id,
- "moves": [{"productId": product, "sortOrder": -1}],
+ "moves": [{"productId": third_product, "sortOrder": -1}],
}
content = get_graphql_content(
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 984065f0c2..5eb099b82b 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -1717,7 +1717,6 @@ def product_list(product_type, category, warehouse, channel_USD, channel_PLN):
Product.objects.bulk_create(
[
Product(
- pk=1486,
name="Test product 1",
slug="test-product-a",
description_plaintext="big blue product",
@@ -1725,7 +1724,6 @@ def product_list(product_type, category, warehouse, channel_USD, channel_PLN):
product_type=product_type,
),
Product(
- pk=1487,
name="Test product 2",
slug="test-product-b",
description_plaintext="big orange product",
@@ -1733,7 +1731,6 @@ def product_list(product_type, category, warehouse, channel_USD, channel_PLN):
product_type=product_type,
),
Product(
- pk=1489,
name="Test product 3",
slug="test-product-c",
description_plaintext="small red",
@@ -1836,21 +1833,18 @@ def product_list_with_variants_many_channel(
Product.objects.bulk_create(
[
Product(
- pk=1486,
name="Test product 1",
slug="test-product-a",
category=category,
product_type=product_type,
),
Product(
- pk=1487,
name="Test product 2",
slug="test-product-b",
category=category,
product_type=product_type,
),
Product(
- pk=1489,
name="Test product 3",
slug="test-product-c",
category=category,