Skip to content

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

Split fixtures in the Warehouse module (#16850)

Filip Owczarek8a363d23760c
diff --git a/conftest.py b/conftest.py
index ab1ef2572e..812f50c16e 100644
--- a/conftest.py
+++ b/conftest.py
@@ -37,6 +37,7 @@ pytest_plugins = [
"saleor.channel.tests.fixtures",
"saleor.page.tests.fixtures",
"saleor.menu.tests.fixtures",
+ "saleor.warehouse.tests.fixtures",
]
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index afbb4d26ad..4598047a79 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -148,7 +148,6 @@ from ..site.models import SiteSettings
from ..tax import TaxCalculationStrategy
from ..tax.utils import calculate_tax_rate, get_tax_class_kwargs_for_order_line
from ..thumbnail.models import Thumbnail
-from ..warehouse import WarehouseClickAndCollectOption
from ..warehouse.models import (
Allocation,
PreorderAllocation,
@@ -7832,143 +7831,6 @@ def staff_notification_recipient(db, staff_user):
return StaffNotificationRecipient.objects.create(active=True, user=staff_user)
-def warehouse(address, shipping_zone, channel_USD):
- warehouse = Warehouse.objects.create(
- address=address,
- name="Example Warehouse",
- slug="example-warehouse",
- )
- warehouse.shipping_zones.add(shipping_zone)
- warehouse.channels.add(channel_USD)
- warehouse.save()
- return warehouse
-
-
-def warehouse_with_external_ref(address, shipping_zone, channel_USD):
- warehouse = Warehouse.objects.create(
- address=address,
- name="Example Warehouse With Ref",
- slug="example-warehouse-with-ref",
- external_reference="example-warehouse-with-ref",
- )
- warehouse.shipping_zones.add(shipping_zone)
- warehouse.channels.add(channel_USD)
- warehouse.save()
- return warehouse
-
-
-def warehouse_JPY(address, shipping_zone_JPY, channel_JPY):
- warehouse = Warehouse.objects.create(
- address=address,
- name="Example Warehouse JPY",
- slug="example-warehouse-jpy",
- )
- warehouse.shipping_zones.add(shipping_zone_JPY)
- warehouse.channels.add(channel_JPY)
- warehouse.save()
- return warehouse
-
-
-def warehouses(address, address_usa, channel_USD):
- warehouses = Warehouse.objects.bulk_create(
- [
- Warehouse(
- address=address.get_copy(),
- name="Warehouse PL",
- slug="warehouse1",
- external_reference="warehouse1",
- ),
- Warehouse(
- address=address_usa.get_copy(),
- name="Warehouse USA",
- slug="warehouse2",
- external_reference="warehouse2",
- ),
- ]
- )
- for warehouse in warehouses:
- warehouse.channels.add(channel_USD)
- return warehouses
-
-
-def warehouses_for_cc(address, shipping_zones, channel_USD):
- warehouses = Warehouse.objects.bulk_create(
- [
- Warehouse(
- address=address.get_copy(),
- name="Warehouse1",
- slug="warehouse1",
- ),
- Warehouse(
- address=address.get_copy(),
- name="Warehouse2",
- slug="warehouse2",
- click_and_collect_option=WarehouseClickAndCollectOption.ALL_WAREHOUSES,
- ),
- Warehouse(
- address=address.get_copy(),
- name="Warehouse3",
- slug="warehouse3",
- click_and_collect_option=WarehouseClickAndCollectOption.LOCAL_STOCK,
- is_private=False,
- ),
- Warehouse(
- address=address.get_copy(),
- name="Warehouse4",
- slug="warehouse4",
- click_and_collect_option=WarehouseClickAndCollectOption.LOCAL_STOCK,
- is_private=False,
- ),
- ]
- )
- # add to shipping zones only not click and collect warehouses
- warehouses[0].shipping_zones.add(*shipping_zones)
- channel_USD.warehouses.add(*warehouses)
- return warehouses
-
-
-def warehouse_for_cc(address, product_variant_list, channel_USD):
- warehouse = Warehouse.objects.create(
- address=address.get_copy(),
- name="Local Warehouse",
- slug="local-warehouse",
- is_private=False,
- click_and_collect_option=WarehouseClickAndCollectOption.LOCAL_STOCK,
- )
- warehouse.channels.add(channel_USD)
-
- Stock.objects.bulk_create(
- [
- Stock(
- warehouse=warehouse, product_variant=product_variant_list[0], quantity=1
- ),
- Stock(
- warehouse=warehouse, product_variant=product_variant_list[1], quantity=2
- ),
- Stock(
- warehouse=warehouse, product_variant=product_variant_list[2], quantity=2
- ),
- ]
- )
- return warehouse
-
-
@pytest.fixture(params=["warehouse_for_cc", "shipping_method"])
def delivery_method(request, warehouse_for_cc, shipping_method):
if request.param == "warehouse":
@@ -7977,59 +7839,6 @@ def delivery_method(request, warehouse_for_cc, shipping_method):
return shipping_method
-def stocks_for_cc(warehouses_for_cc, product_variant_list, product_with_two_variants):
- return Stock.objects.bulk_create(
- [
- Stock(
- warehouse=warehouses_for_cc[0],
- product_variant=product_variant_list[0],
- quantity=5,
- ),
- Stock(
- warehouse=warehouses_for_cc[1],
- product_variant=product_variant_list[0],
- quantity=3,
- ),
- Stock(
- warehouse=warehouses_for_cc[1],
- product_variant=product_variant_list[1],
- quantity=10,
- ),
- Stock(
- warehouse=warehouses_for_cc[1],
- product_variant=product_variant_list[2],
- quantity=10,
- ),
- Stock(
- warehouse=warehouses_for_cc[2],
- product_variant=product_variant_list[0],
- quantity=3,
- ),
- Stock(
- warehouse=warehouses_for_cc[3],
- product_variant=product_variant_list[0],
- quantity=3,
- ),
- Stock(
- warehouse=warehouses_for_cc[3],
- product_variant=product_variant_list[1],
- quantity=3,
- ),
- Stock(
- warehouse=warehouses_for_cc[3],
- product_variant=product_with_two_variants.variants.last(),
- quantity=7,
- ),
- Stock(
- warehouse=warehouses_for_cc[3],
- product_variant=product_variant_list[2],
- quantity=3,
- ),
- ]
- )
-
-
@pytest.fixture
def checkout_for_cc(channel_USD, customer_user):
checkout = Checkout.objects.create(
@@ -8164,139 +7973,6 @@ def checkout_with_prices(
return checkout_with_items
-def warehouses_with_shipping_zone(warehouses, shipping_zone):
- warehouses[0].shipping_zones.add(shipping_zone)
- warehouses[1].shipping_zones.add(shipping_zone)
- return warehouses
-
-
-def warehouses_with_different_shipping_zone(warehouses, shipping_zones):
- warehouses[0].shipping_zones.add(shipping_zones[0])
- warehouses[1].shipping_zones.add(shipping_zones[1])
- return warehouses
-
-
-def warehouse_no_shipping_zone(address, channel_USD):
- warehouse = Warehouse.objects.create(
- address=address,
- name="Warehouse without shipping zone",
- slug="warehouse-no-shipping-zone",
- external_reference="warehouse-no-shipping-zone",
- )
- warehouse.channels.add(channel_USD)
- return warehouse
-
-
-def stock(variant, warehouse):
- return Stock.objects.create(
- product_variant=variant, warehouse=warehouse, quantity=15
- )
-
-
-def allocation(order_line, stock):
- return Allocation.objects.create(
- order_line=order_line, stock=stock, quantity_allocated=order_line.quantity
- )
-
-
-def allocations(order_list, stock, channel_USD):
- variant = stock.product_variant
- product = variant.product
- channel_listing = variant.channel_listings.get(channel=channel_USD)
- net = variant.get_price(channel_listing)
- gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
- price = TaxedMoney(net=net, gross=gross)
- lines = OrderLine.objects.bulk_create(
- [
- OrderLine(
- order=order_list[0],
- variant=variant,
- quantity=1,
- product_name=str(variant.product),
- variant_name=str(variant),
- product_sku=variant.sku,
- product_variant_id=variant.get_global_id(),
- is_shipping_required=variant.is_shipping_required(),
- is_gift_card=variant.is_gift_card(),
- unit_price=price,
- total_price=price,
- tax_rate=Decimal("0.23"),
- **get_tax_class_kwargs_for_order_line(product.product_type.tax_class),
- ),
- OrderLine(
- order=order_list[1],
- variant=variant,
- quantity=2,
- product_name=str(variant.product),
- variant_name=str(variant),
- product_sku=variant.sku,
- product_variant_id=variant.get_global_id(),
- is_shipping_required=variant.is_shipping_required(),
- is_gift_card=variant.is_gift_card(),
- unit_price=price,
- total_price=price,
- tax_rate=Decimal("0.23"),
- **get_tax_class_kwargs_for_order_line(product.product_type.tax_class),
- ),
- OrderLine(
- order=order_list[2],
- variant=variant,
- quantity=4,
- product_name=str(variant.product),
- variant_name=str(variant),
- product_sku=variant.sku,
- product_variant_id=variant.get_global_id(),
- is_shipping_required=variant.is_shipping_required(),
- is_gift_card=variant.is_gift_card(),
- unit_price=price,
- total_price=price,
- tax_rate=Decimal("0.23"),
- **get_tax_class_kwargs_for_order_line(product.product_type.tax_class),
- ),
- ]
- )
-
- for order in order_list:
- order.search_vector = FlatConcatSearchVector(
- *prepare_order_search_vector_value(order)
- )
- Order.objects.bulk_update(order_list, ["search_vector"])
-
- return Allocation.objects.bulk_create(
- [
- Allocation(
- order_line=lines[0], stock=stock, quantity_allocated=lines[0].quantity
- ),
- Allocation(
- order_line=lines[1], stock=stock, quantity_allocated=lines[1].quantity
- ),
- Allocation(
- order_line=lines[2], stock=stock, quantity_allocated=lines[2].quantity
- ),
- ]
- )
-
-
-def preorder_allocation(
- order_line, preorder_variant_global_and_channel_threshold, channel_PLN
-):
- variant = preorder_variant_global_and_channel_threshold
- product_variant_channel_listing = variant.channel_listings.get(channel=channel_PLN)
- return PreorderAllocation.objects.create(
- order_line=order_line,
- product_variant_channel_listing=product_variant_channel_listing,
- quantity=order_line.quantity,
- )
-
-
@pytest.fixture
def app_installation():
app_installation = AppInstallation.objects.create(
diff --git a/saleor/warehouse/tests/fixtures/__init__.py b/saleor/warehouse/tests/fixtures/__init__.py
new file mode 100644
index 0000000000..bbc7546a67
--- /dev/null
+++ b/saleor/warehouse/tests/fixtures/__init__.py
@@ -0,0 +1,4 @@
+from .allocation import * # noqa: F403
+from .preorder_allocation import * # noqa: F403
+from .stock import * # noqa: F403
+from .warehouse import * # noqa: F403
diff --git a/saleor/warehouse/tests/fixtures/allocation.py b/saleor/warehouse/tests/fixtures/allocation.py
new file mode 100644
index 0000000000..a1908d8929
--- /dev/null
+++ b/saleor/warehouse/tests/fixtures/allocation.py
@@ -0,0 +1,96 @@
+from decimal import Decimal
+
+import pytest
+
+from ....core.postgres import FlatConcatSearchVector
+from ....core.prices import Money, TaxedMoney
+from ....order.models import Order, OrderLine
+from ....order.search import prepare_order_search_vector_value
+from ....tax.utils import get_tax_class_kwargs_for_order_line
+from ...models import Allocation
+
+
+def allocation(order_line, stock):
+ return Allocation.objects.create(
+ order_line=order_line, stock=stock, quantity_allocated=order_line.quantity
+ )
+
+
+def allocations(order_list, stock, channel_USD):
+ variant = stock.product_variant
+ product = variant.product
+ channel_listing = variant.channel_listings.get(channel=channel_USD)
+ net = variant.get_price(channel_listing)
+ gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
+ price = TaxedMoney(net=net, gross=gross)
+ lines = OrderLine.objects.bulk_create(
+ [
+ OrderLine(
+ order=order_list[0],
+ variant=variant,
+ quantity=1,
+ product_name=str(variant.product),
+ variant_name=str(variant),
+ product_sku=variant.sku,
+ product_variant_id=variant.get_global_id(),
+ is_shipping_required=variant.is_shipping_required(),
+ is_gift_card=variant.is_gift_card(),
+ unit_price=price,
+ total_price=price,
+ tax_rate=Decimal("0.23"),
+ **get_tax_class_kwargs_for_order_line(product.product_type.tax_class),
+ ),
+ OrderLine(
+ order=order_list[1],
+ variant=variant,
+ quantity=2,
+ product_name=str(variant.product),
+ variant_name=str(variant),
+ product_sku=variant.sku,
+ product_variant_id=variant.get_global_id(),
+ is_shipping_required=variant.is_shipping_required(),
+ is_gift_card=variant.is_gift_card(),
+ unit_price=price,
+ total_price=price,
+ tax_rate=Decimal("0.23"),
+ **get_tax_class_kwargs_for_order_line(product.product_type.tax_class),
+ ),
+ OrderLine(
+ order=order_list[2],
+ variant=variant,
+ quantity=4,
+ product_name=str(variant.product),
+ variant_name=str(variant),
+ product_sku=variant.sku,
+ product_variant_id=variant.get_global_id(),
+ is_shipping_required=variant.is_shipping_required(),
+ is_gift_card=variant.is_gift_card(),
+ unit_price=price,
+ total_price=price,
+ tax_rate=Decimal("0.23"),
+ **get_tax_class_kwargs_for_order_line(product.product_type.tax_class),
+ ),
+ ]
+ )
+
+ for order in order_list:
+ order.search_vector = FlatConcatSearchVector(
+ *prepare_order_search_vector_value(order)
+ )
+ Order.objects.bulk_update(order_list, ["search_vector"])
+
+ return Allocation.objects.bulk_create(
+ [
+ Allocation(
+ order_line=lines[0], stock=stock, quantity_allocated=lines[0].quantity
+ ),
+ Allocation(
+ order_line=lines[1], stock=stock, quantity_allocated=lines[1].quantity
+ ),
+ Allocation(
+ order_line=lines[2], stock=stock, quantity_allocated=lines[2].quantity
+ ),
+ ]
+ )
diff --git a/saleor/warehouse/tests/fixtures/preorder_allocation.py b/saleor/warehouse/tests/fixtures/preorder_allocation.py
new file mode 100644
index 0000000000..065eecbe90
--- /dev/null
+++ b/saleor/warehouse/tests/fixtures/preorder_allocation.py
@@ -0,0 +1,16 @@
+import pytest
+
+from ...models import PreorderAllocation
+
+
+def preorder_allocation(
+ order_line, preorder_variant_global_and_channel_threshold, channel_PLN
+):
+ variant = preorder_variant_global_and_channel_threshold
+ product_variant_channel_listing = variant.channel_listings.get(channel=channel_PLN)
+ return PreorderAllocation.objects.create(
+ order_line=order_line,
+ product_variant_channel_listing=product_variant_channel_listing,
+ quantity=order_line.quantity,
+ )
diff --git a/saleor/warehouse/tests/fixtures/stock.py b/saleor/warehouse/tests/fixtures/stock.py
new file mode 100644
index 0000000000..31dd840d2e
--- /dev/null
+++ b/saleor/warehouse/tests/fixtures/stock.py
@@ -0,0 +1,63 @@
+import pytest
+
+from ...models import Stock
+
+
+def stock(variant, warehouse):
+ return Stock.objects.create(
+ product_variant=variant, warehouse=warehouse, quantity=15
+ )
+
+
+def stocks_for_cc(warehouses_for_cc, product_variant_list, product_with_two_variants):
+ return Stock.objects.bulk_create(
+ [
+ Stock(
+ warehouse=warehouses_for_cc[0],
+ product_variant=product_variant_list[0],
+ quantity=5,
+ ),
+ Stock(
+ warehouse=warehouses_for_cc[1],
+ product_variant=product_variant_list[0],
+ quantity=3,
+ ),
+ Stock(
+ warehouse=warehouses_for_cc[1],
+ product_variant=product_variant_list[1],
+ quantity=10,
+ ),
+ Stock(
+ warehouse=warehouses_for_cc[1],
+ product_variant=product_variant_list[2],
+ quantity=10,
+ ),
+ Stock(
+ warehouse=warehouses_for_cc[2],
+ product_variant=product_variant_list[0],
+ quantity=3,
+ ),
+ Stock(
+ warehouse=warehouses_for_cc[3],
+ product_variant=product_variant_list[0],
+ quantity=3,
+ ),
+ Stock(
+ warehouse=warehouses_for_cc[3],
+ product_variant=product_variant_list[1],
+ quantity=3,
+ ),
+ Stock(
+ warehouse=warehouses_for_cc[3],
+ product_variant=product_with_two_variants.variants.last(),
+ quantity=7,
+ ),
+ Stock(
+ warehouse=warehouses_for_cc[3],
+ product_variant=product_variant_list[2],
+ quantity=3,
+ ),
+ ]
+ )
diff --git a/saleor/warehouse/tests/fixtures/warehouse.py b/saleor/warehouse/tests/fixtures/warehouse.py
new file mode 100644
index 0000000000..9de10e2272
--- /dev/null
+++ b/saleor/warehouse/tests/fixtures/warehouse.py
@@ -0,0 +1,165 @@
+import pytest
+
+from ... import WarehouseClickAndCollectOption
+from ...models import Stock, Warehouse
+
+
+def warehouse(address, shipping_zone, channel_USD):
+ warehouse = Warehouse.objects.create(
+ address=address,
+ name="Example Warehouse",
+ slug="example-warehouse",
+ )
+ warehouse.shipping_zones.add(shipping_zone)
+ warehouse.channels.add(channel_USD)
+ return warehouse
+
+
+def warehouse_with_external_ref(address, shipping_zone, channel_USD):
+ warehouse = Warehouse.objects.create(
+ address=address,
+ name="Example Warehouse With Ref",
+ slug="example-warehouse-with-ref",
+ external_reference="example-warehouse-with-ref",
+ )
+ warehouse.shipping_zones.add(shipping_zone)
+ warehouse.channels.add(channel_USD)
+ return warehouse
+
+
+def warehouse_JPY(address, shipping_zone_JPY, channel_JPY):
+ warehouse = Warehouse.objects.create(
+ address=address,
+ name="Example Warehouse JPY",
+ slug="example-warehouse-jpy",
+ )
+ warehouse.shipping_zones.add(shipping_zone_JPY)
+ warehouse.channels.add(channel_JPY)
+ return warehouse
+
+
+def warehouses(address, address_usa, channel_USD):
+ warehouses = Warehouse.objects.bulk_create(
+ [
+ Warehouse(
+ address=address.get_copy(),
+ name="Warehouse PL",
+ slug="warehouse1",
+ external_reference="warehouse1",
+ ),
+ Warehouse(
+ address=address_usa.get_copy(),
+ name="Warehouse USA",
+ slug="warehouse2",
+ external_reference="warehouse2",
+ ),
+ ]
+ )
+ for warehouse in warehouses:
+ warehouse.channels.add(channel_USD)
+ return warehouses
+
+
+def warehouses_for_cc(address, shipping_zones, channel_USD):
+ warehouses = Warehouse.objects.bulk_create(
+ [
+ Warehouse(
+ address=address.get_copy(),
+ name="Warehouse1",
+ slug="warehouse1",
+ ),
+ Warehouse(
+ address=address.get_copy(),
+ name="Warehouse2",
+ slug="warehouse2",
+ click_and_collect_option=WarehouseClickAndCollectOption.ALL_WAREHOUSES,
+ ),
+ Warehouse(
+ address=address.get_copy(),
+ name="Warehouse3",
+ slug="warehouse3",
+ click_and_collect_option=WarehouseClickAndCollectOption.LOCAL_STOCK,
+ is_private=False,
+ ),
+ Warehouse(
+ address=address.get_copy(),
+ name="Warehouse4",
+ slug="warehouse4",
+ click_and_collect_option=WarehouseClickAndCollectOption.LOCAL_STOCK,
+ is_private=False,
+ ),
+ ]
+ )
+ # add to shipping zones only not click and collect warehouses
+ warehouses[0].shipping_zones.add(*shipping_zones)
+ channel_USD.warehouses.add(*warehouses)
+ return warehouses
+
+
+def warehouse_for_cc(address, product_variant_list, channel_USD):
+ warehouse = Warehouse.objects.create(
+ address=address.get_copy(),
+ name="Local Warehouse",
+ slug="local-warehouse",
+ is_private=False,
+ click_and_collect_option=WarehouseClickAndCollectOption.LOCAL_STOCK,
+ )
+ warehouse.channels.add(channel_USD)
+
+ Stock.objects.bulk_create(
+ [
+ Stock(
+ warehouse=warehouse, product_variant=product_variant_list[0], quantity=1
+ ),
+ Stock(
+ warehouse=warehouse, product_variant=product_variant_list[1], quantity=2
+ ),
+ Stock(
+ warehouse=warehouse, product_variant=product_variant_list[2], quantity=2
+ ),
+ ]
+ )
+ return warehouse
+
+
+def warehouses_with_shipping_zone(warehouses, shipping_zone):
+ warehouses[0].shipping_zones.add(shipping_zone)
+ warehouses[1].shipping_zones.add(shipping_zone)
+ return warehouses
+
+
+def warehouses_with_different_shipping_zone(warehouses, shipping_zones):
+ warehouses[0].shipping_zones.add(shipping_zones[0])
+ warehouses[1].shipping_zones.add(shipping_zones[1])
+ return warehouses
+
+
+def warehouse_no_shipping_zone(address, channel_USD):
+ warehouse = Warehouse.objects.create(
+ address=address,
+ name="Warehouse without shipping zone",
+ slug="warehouse-no-shipping-zone",
+ external_reference="warehouse-no-shipping-zone",
+ )
+ warehouse.channels.add(channel_USD)
+ return warehouse