Skip to content

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

Update and test stock queries

Mira Szewczykae5d7e84c279
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 95b927501c..83fe83dcab 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -3496,6 +3496,7 @@ type Query {
translations(kind: TranslatableKinds!, before: String, after: String, first: Int, last: Int): TranslatableItemConnection
translation(id: ID!, kind: TranslatableKinds!): TranslatableItem
stock(id: ID!): Stock
+ stocks(filter: StockFilterInput, query: String, before: String, after: String, first: Int, last: Int): StockCountableConnection
shop: Shop
shippingZone(id: ID!): ShippingZone
shippingZones(before: String, after: String, first: Int, last: Int): ShippingZoneCountableConnection
@@ -4196,6 +4197,13 @@ type StockError {
code: StockErorrCode
}
+input StockFilterInput {
+ quantity: Float
+ quantityAllocated: Float
+ productVariant: String
+ warehouse: String
+}
+
input StockInput {
productVariant: ID!
warehouse: ID!
diff --git a/saleor/graphql/stock/filters.py b/saleor/graphql/stock/filters.py
new file mode 100644
index 0000000000..9276dc45c0
--- /dev/null
+++ b/saleor/graphql/stock/filters.py
@@ -0,0 +1,35 @@
+import django_filters
+from django.db.models import Q
+
+from ...stock.models import Stock
+from ..core.types import FilterInputObjectType
+
+
+def filter_product_variant(qs, _, value):
+ qs = qs.filter(
+ Q(product_variant__product__name__icontains=value)
+ | Q(product_variant__name__icontains=value)
+ )
+ return qs
+
+
+def filter_warehouse(qs, _, value):
+ qs = qs.filter(
+ Q(warehouse__name__icontains=value)
+ | Q(warehouse__company_name__icontains=value)
+ )
+ return qs
+
+
+class StockFilter(django_filters.FilterSet):
+ product_variant = django_filters.CharFilter(method=filter_product_variant)
+ warehouse = django_filters.CharFilter(method=filter_warehouse)
+
+ class Meta:
+ model = Stock
+ fields = ["quantity", "quantity_allocated"]
+
+
+class StockFilterInput(FilterInputObjectType):
+ class Meta:
+ filterset_class = StockFilter
diff --git a/saleor/graphql/stock/schema.py b/saleor/graphql/stock/schema.py
index ec2e55dfb6..99e576aabe 100644
--- a/saleor/graphql/stock/schema.py
+++ b/saleor/graphql/stock/schema.py
@@ -1,6 +1,10 @@
import graphene
+import graphene_django_optimizer as gql_optimizer
+from ...stock import models
+from ..core.fields import FilterInputConnectionField
from ..decorators import permission_required
+from .filters import StockFilterInput
from .mutations import StockBulkDelete, StockCreate, StockDelete, StockUpdate
from .types import Stock
@@ -11,12 +15,27 @@ class StockQueries(graphene.ObjectType):
description="Look up a stok by ID",
id=graphene.ID(required=True, description="ID of an warehouse"),
)
- # TODO: Add stocks query
+ stocks = FilterInputConnectionField(
+ Stock,
+ description="List of stocks.",
+ filter=StockFilterInput(),
+ query=graphene.String(),
+ )
@permission_required("stock.manage_stocks")
- def resolve_warehouse(self, info, **kwargs):
+ def resolve_stock(self, info, **kwargs):
stock_pk = kwargs.get("id")
- return graphene.Node.get_node_from_global_id(info, stock_pk, Stock)
+ stock = graphene.Node.get_node_from_global_id(info, stock_pk, Stock)
+ return stock
+
+ @permission_required("stock.manage_stocks")
+ def resolve_stocks(self, info, **data):
+ qs = (
+ models.Stock.objects.select_related("warehouse")
+ .prefetch_related("product_variant__product")
+ .all()
+ )
+ return gql_optimizer.query(qs, info)
class StockMutations(graphene.ObjectType):
diff --git a/saleor/graphql/warehouse/schema.py b/saleor/graphql/warehouse/schema.py
index bdb89318ad..978db5ea39 100644
--- a/saleor/graphql/warehouse/schema.py
+++ b/saleor/graphql/warehouse/schema.py
@@ -32,7 +32,7 @@ class WarehouseQueries(graphene.ObjectType):
@permission_required("warehouse.manage_warehouses")
def resolve_warehouses(self, info, **kwargs):
- qs = models.Warehouse.objects.all()
+ qs = models.Warehouse.objects.select_related("address").all()
return gql_optimizer.query(qs, info)
diff --git a/tests/api/test_stock.py b/tests/api/test_stock.py
index b222a7530f..9c2dfd3806 100644
--- a/tests/api/test_stock.py
+++ b/tests/api/test_stock.py
@@ -62,6 +62,61 @@ mutation bulkDeleteStock($ids: [ID]!) {
"""
+QUERY_STOCK = """
+query stock($id: ID!) {
+ stock(id: $id) {
+ warehouse {
+ name
+ }
+ productVariant {
+ product {
+ name
+ }
+ }
+ quantity
+ quantityAllocated
+ }
+}
+"""
+
+
+QUERY_STOCKS = """
+ query {
+ stocks(first:100) {
+ totalCount
+ edges {
+ node {
+ id
+ warehouse {
+ name
+ id
+ }
+ productVariant {
+ name
+ id
+ }
+ quantity
+ quantityAllocated
+ }
+ }
+ }
+ }
+"""
+
+QUERY_STOCKS_WITH_FILTERS = """
+ query stocks($filter: StockFilterInput!) {
+ stocks(first: 100, filter: $filter) {
+ totalCount
+ edges {
+ node {
+ id
+ }
+ }
+ }
+ }
+"""
+
+
def test_stock_cannot_be_created_without_permission(
staff_api_client, variant, warehouse
):
@@ -197,3 +252,134 @@ def test_bulk_delete_bulk_stock_mutation(staff_api_client, permission_manage_sto
content_count = content["data"]["bulkDeleteStock"]["count"]
assert content_count == initial_stock_count
assert not Stock.objects.all().exists()
+
+
+def test_query_stock_requires_permission(staff_api_client, stock):
+ assert not staff_api_client.user.has_perm("stock.manage_stocks")
+ stock_id = graphene.Node.to_global_id("Stock", stock.pk)
+ response = staff_api_client.post_graphql(QUERY_STOCK, variables={"id": stock_id})
+ assert_no_permission(response)
+
+
+def test_query_stock(staff_api_client, stock, permission_manage_stocks):
+ staff_api_client.user.user_permissions.add(permission_manage_stocks)
+ stock_id = graphene.Node.to_global_id("Stock", stock.pk)
+ response = staff_api_client.post_graphql(QUERY_STOCK, variables={"id": stock_id})
+ content = get_graphql_content(response)
+ content_stock = content["data"]["stock"]
+ assert (
+ content_stock["productVariant"]["product"]["name"]
+ == stock.product_variant.product.name
+ )
+ assert content_stock["warehouse"]["name"] == stock.warehouse.name
+ assert content_stock["quantity"] == stock.quantity
+ assert content_stock["quantityAllocated"] == stock.quantity_allocated
+
+
+def test_query_stocks_requires_permissions(staff_api_client):
+ assert not staff_api_client.user.has_perm("stock.manage_stocks")
+ response = staff_api_client.post_graphql(QUERY_STOCKS)
+ assert_no_permission(response)
+
+
+def test_query_stocks(staff_api_client, stock, permission_manage_stocks):
+ staff_api_client.user.user_permissions.add(permission_manage_stocks)
+ response = staff_api_client.post_graphql(QUERY_STOCKS)
+ content = get_graphql_content(response)
+ total_count = content["data"]["stocks"]["totalCount"]
+ assert total_count == Stock.objects.count()
+
+
+def test_query_stocks_with_filters_quantity(
+ staff_api_client, stock, permission_manage_stocks
+):
+ staff_api_client.user.user_permissions.add(permission_manage_stocks)
+ quantities = Stock.objects.all().values_list("quantity", flat=True)
+ sum_quantities = sum(quantities)
+ variables = {"filter": {"quantity": sum_quantities}}
+ response = staff_api_client.post_graphql(
+ QUERY_STOCKS_WITH_FILTERS, variables=variables
+ )
+ content = get_graphql_content(response)
+ total_count = content["data"]["stocks"]["totalCount"]
+ assert total_count == 0
+
+ variables = {"filter": {"quantity": max(quantities)}}
+ response = staff_api_client.post_graphql(
+ QUERY_STOCKS_WITH_FILTERS, variables=variables
+ )
+ content = get_graphql_content(response)
+ total_count = content["data"]["stocks"]["totalCount"]
+ assert total_count == Stock.objects.filter(quantity=max(quantities)).count()
+
+
+def test_query_stocks_with_filters_quantity_allocated(
+ staff_api_client, stock, permission_manage_stocks
+):
+ staff_api_client.user.user_permissions.add(permission_manage_stocks)
+ quantities = Stock.objects.all().values_list("quantity_allocated", flat=True)
+ sum_quantities = sum(quantities)
+ variables = {"filter": {"quantityAllocated": sum_quantities}}
+ response = staff_api_client.post_graphql(
+ QUERY_STOCKS_WITH_FILTERS, variables=variables
+ )
+ content = get_graphql_content(response)
+ total_count = content["data"]["stocks"]["totalCount"]
+ assert total_count == 0
+
+ variables = {"filter": {"quantityAllocated": max(quantities)}}
+ response = staff_api_client.post_graphql(
+ QUERY_STOCKS_WITH_FILTERS, variables=variables
+ )
+ content = get_graphql_content(response)
+ total_count = content["data"]["stocks"]["totalCount"]
+ assert (
+ total_count == Stock.objects.filter(quantity_allocated=max(quantities)).count()
+ )
+
+
+def test_query_stocks_with_filters_warehouse(
+ staff_api_client, stock, permission_manage_stocks
+):
+ staff_api_client.user.user_permissions.add(permission_manage_stocks)
+ warehouse = stock.warehouse
+ response_name = staff_api_client.post_graphql(
+ QUERY_STOCKS_WITH_FILTERS, variables={"filter": {"warehouse": warehouse.name}}
+ )
+ content = get_graphql_content(response_name)
+ total_count = content["data"]["stocks"]["totalCount"]
+ assert total_count == Stock.objects.filter(warehouse=warehouse).count()
+
+
+def test_query_stocks_with_filters_product_variant(
+ staff_api_client, stock, permission_manage_stocks
+):
+ staff_api_client.user.user_permissions.add(permission_manage_stocks)
+ product_variant = stock.product_variant
+ response_name = staff_api_client.post_graphql(
+ QUERY_STOCKS_WITH_FILTERS,
+ variables={"filter": {"productVariant": product_variant.name}},
+ )
+ content = get_graphql_content(response_name)
+ total_count = content["data"]["stocks"]["totalCount"]
+ assert (
+ total_count
+ == Stock.objects.filter(product_variant__name=product_variant.name).count()
+ )
+
+
+def test_query_stocks_with_filters_product_variant__product(
+ staff_api_client, stock, permission_manage_stocks
+):
+ staff_api_client.user.user_permissions.add(permission_manage_stocks)
+ product = stock.product_variant.product
+ response_name = staff_api_client.post_graphql(
+ QUERY_STOCKS_WITH_FILTERS,
+ variables={"filter": {"productVariant": product.name}},
+ )
+ content = get_graphql_content(response_name)
+ total_count = content["data"]["stocks"]["totalCount"]
+ assert (
+ total_count
+ == Stock.objects.filter(product_variant__product__name=product.name).count()
+ )