Skip to content

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

Use count instead of len on querysets

Anna Siwik9fc4a0ce5e01
diff --git a/tests/dashboard/test_category.py b/tests/dashboard/test_category.py
index 3a6ddbb22e..11d94f8046 100644
--- a/tests/dashboard/test_category.py
+++ b/tests/dashboard/test_category.py
@@ -10,31 +10,31 @@ def test_category_list(admin_client, default_category):
def test_category_add(admin_client):
- assert len(Category.objects.all()) == 0
+ assert Category.objects.count() == 0
url = reverse('dashboard:category-add')
data = {'name': 'Cars', 'description': 'Fastest cars'}
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
- assert len(Category.objects.all()) == 1
+ assert Category.objects.count() == 1
def test_category_add_not_valid(admin_client):
- assert len(Category.objects.all()) == 0
+ assert Category.objects.count() == 0
url = reverse('dashboard:category-add')
data = {}
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
- assert len(Category.objects.all()) == 0
+ assert Category.objects.count() == 0
def test_category_add_subcategory(admin_client, default_category):
- assert len(Category.objects.all()) == 1
+ assert Category.objects.count() == 1
url = reverse('dashboard:category-add',
kwargs={'root_pk': default_category.pk})
data = {'name': 'Cars', 'description': 'Fastest cars'}
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
- assert len(Category.objects.all()) == 2
+ assert Category.objects.count() == 2
default_category.refresh_from_db()
subcategories = default_category.get_children()
assert len(subcategories) == 1
@@ -42,18 +42,18 @@ def test_category_add_subcategory(admin_client, default_category):
def test_category_edit(admin_client, default_category):
- assert len(Category.objects.all()) == 1
+ assert Category.objects.count() == 1
url = reverse('dashboard:category-edit',
kwargs={'root_pk': default_category.pk})
data = {'name': 'Cars', 'description': 'Super fast!'}
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
- assert len(Category.objects.all()) == 1
+ assert Category.objects.count() == 1
assert Category.objects.all()[0].name == 'Cars'
def test_category_details(admin_client, default_category):
- assert len(Category.objects.all()) == 1
+ assert Category.objects.count() == 1
url = reverse('dashboard:category-details',
kwargs={'pk': default_category.pk})
response = admin_client.post(url, follow=True)
@@ -61,9 +61,9 @@ def test_category_details(admin_client, default_category):
def test_category_delete(admin_client, default_category):
- assert len(Category.objects.all()) == 1
+ assert Category.objects.count() == 1
url = reverse('dashboard:category-delete',
kwargs={'pk': default_category.pk})
response = admin_client.post(url, follow=True)
assert response.status_code == 200
- assert len(Category.objects.all()) == 0
+ assert Category.objects.count() == 0
diff --git a/tests/dashboard/test_discounts.py b/tests/dashboard/test_discounts.py
index 0a71f113bb..c3b5bce64b 100644
--- a/tests/dashboard/test_discounts.py
+++ b/tests/dashboard/test_discounts.py
@@ -25,7 +25,7 @@ def test_vouchers_list(admin_client, voucher):
def test_voucher_shipping_add(admin_client):
- assert len(Voucher.objects.all()) == 0
+ assert Voucher.objects.count() == 0
url = reverse('dashboard:voucher-add')
data = {
'code': 'TESTVOUCHER', 'name': 'Test Voucher',
@@ -35,7 +35,7 @@ def test_voucher_shipping_add(admin_client):
'shipping-limit': '59.99'}
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
- assert len(Voucher.objects.all()) == 1
+ assert Voucher.objects.count() == 1
voucher = Voucher.objects.all()[0]
assert voucher.type == VoucherType.SHIPPING
diff --git a/tests/dashboard/test_product.py b/tests/dashboard/test_product.py
index 9927ae2773..52b5364ce1 100644
--- a/tests/dashboard/test_product.py
+++ b/tests/dashboard/test_product.py
@@ -8,6 +8,7 @@ from django.forms import HiddenInput
from django.forms.models import model_to_dict
from django.urls import reverse
from PIL import Image
+from tests.utils import get_redirect_location
from saleor.dashboard.product import ProductBulkAction
from saleor.dashboard.product.forms import (
@@ -158,7 +159,7 @@ def test_change_attributes_in_product_form(db, product, color_attribute):
def test_attribute_list(db, product, color_attribute, admin_client):
- assert len(ProductAttribute.objects.all()) == 2
+ assert ProductAttribute.objects.count() == 2
response = admin_client.get(reverse('dashboard:product-attributes'))
assert response.status_code == 200
@@ -172,50 +173,50 @@ def test_attribute_details(color_attribute, admin_client):
def test_attribute_add(color_attribute, admin_client):
- assert len(ProductAttribute.objects.all()) == 1
+ assert ProductAttribute.objects.count() == 1
url = reverse('dashboard:product-attribute-add')
data = {'name': 'test', 'slug': 'test'}
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
- assert len(ProductAttribute.objects.all()) == 2
+ assert ProductAttribute.objects.count() == 2
def test_attribute_add_not_valid(color_attribute, admin_client):
- assert len(ProductAttribute.objects.all()) == 1
+ assert ProductAttribute.objects.count() == 1
url = reverse('dashboard:product-attribute-add')
data = {}
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
- assert len(ProductAttribute.objects.all()) == 1
+ assert ProductAttribute.objects.count() == 1
def test_attribute_edit(color_attribute, admin_client):
- assert len(ProductAttribute.objects.all()) == 1
+ assert ProductAttribute.objects.count() == 1
url = reverse(
'dashboard:product-attribute-update',
kwargs={'pk': color_attribute.pk})
data = {'name': 'new_name', 'slug': 'new_slug'}
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
- assert len(ProductAttribute.objects.all()) == 1
+ assert ProductAttribute.objects.count() == 1
color_attribute.refresh_from_db()
assert color_attribute.name == 'new_name'
assert color_attribute.slug == 'new_slug'
def test_attribute_delete(color_attribute, admin_client):
- assert len(ProductAttribute.objects.all()) == 1
+ assert ProductAttribute.objects.count() == 1
url = reverse(
'dashboard:product-attribute-delete',
kwargs={'pk': color_attribute.pk})
response = admin_client.post(url, follow=True)
assert response.status_code == 200
- assert len(ProductAttribute.objects.all()) == 0
+ assert ProductAttribute.objects.count() == 0
def test_attribute_choice_value_add(color_attribute, admin_client):
values = AttributeChoiceValue.objects.filter(attribute=color_attribute.pk)
- assert len(values) == 2
+ assert values.count() == 2
url = reverse(
'dashboard:product-attribute-value-add',
kwargs={'attribute_pk': color_attribute.pk})
@@ -223,12 +224,12 @@ def test_attribute_choice_value_add(color_attribute, admin_client):
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
values = AttributeChoiceValue.objects.filter(attribute=color_attribute.pk)
- assert len(values) == 3
+ assert values.count() == 3
def test_attribute_choice_value_add_not_valid(color_attribute, admin_client):
values = AttributeChoiceValue.objects.filter(attribute=color_attribute.pk)
- assert len(values) == 2
+ assert values.count() == 2
url = reverse(
'dashboard:product-attribute-value-add',
kwargs={'attribute_pk': color_attribute.pk})
@@ -236,12 +237,12 @@ def test_attribute_choice_value_add_not_valid(color_attribute, admin_client):
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
values = AttributeChoiceValue.objects.filter(attribute=color_attribute.pk)
- assert len(values) == 2
+ assert values.count() == 2
def test_attribute_choice_value_edit(color_attribute, admin_client):
values = AttributeChoiceValue.objects.filter(attribute=color_attribute.pk)
- assert len(values) == 2
+ assert values.count() == 2
url = reverse(
'dashboard:product-attribute-value-update',
kwargs={'attribute_pk': color_attribute.pk, 'value_pk': values[0].pk})
@@ -250,13 +251,13 @@ def test_attribute_choice_value_edit(color_attribute, admin_client):
assert response.status_code == 200
values = AttributeChoiceValue.objects.filter(
attribute=color_attribute.pk, name='Pink')
- assert len(values) == 1
+ assert values.count() == 1
assert values[0].name == 'Pink'
def test_attribute_choice_value_delete(color_attribute, admin_client):
values = AttributeChoiceValue.objects.filter(attribute=color_attribute.pk)
- assert len(values) == 2
+ assert values.count() == 2
deleted_value = values[0]
url = reverse(
'dashboard:product-attribute-value-delete',
@@ -265,7 +266,7 @@ def test_attribute_choice_value_delete(color_attribute, admin_client):
response = admin_client.post(url, follow=True)
assert response.status_code == 200
values = AttributeChoiceValue.objects.filter(attribute=color_attribute.pk)
- assert len(values) == 1
+ assert values.count() == 1
assert deleted_value not in values
@@ -424,9 +425,8 @@ def test_view_product_image_add(
mock_create_thumbnails = Mock(return_value=None)
monkeypatch.setattr(
'saleor.dashboard.product.forms.create_product_thumbnails.delay',
- mock_create_thumbnails)
- assert len(ProductImage.objects.all()) == 1
- assert len(product_with_image.images.all()) == 1
+ assert ProductImage.objects.count() == 1
+ assert product_with_image.images.count() == 1
url = reverse(
'dashboard:product-image-add',
kwargs={'product_pk': product_with_image.pk})
@@ -436,7 +436,7 @@ def test_view_product_image_add(
data = {'image_0': image, 'alt': ['description']}
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
- assert len(ProductImage.objects.all()) == 2
+ assert ProductImage.objects.count() == 2
product_with_image.refresh_from_db()
images = product_with_image.images.all()
assert len(images) == 2
@@ -463,7 +463,7 @@ def test_view_product_image_edit_same_image_add_description(
data = {'image_1': ['0.49x0.59'], 'alt': ['description']}
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
- assert len(product_with_image.images.all()) == 1
+ assert product_with_image.images.count() == 1
product_image.refresh_from_db()
assert product_image.alt == 'description'
mock_create_thumbnails.assert_called_once_with(product_image.pk)
@@ -488,7 +488,7 @@ def test_view_product_image_edit_new_image(
data = {'image_0': image, 'alt': ['description']}
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
- assert len(product_with_image.images.all()) == 1
+ assert product_with_image.images.count() == 1
product_image.refresh_from_db()
assert image_name in product_image.image.name
assert product_image.alt == 'description'
diff --git a/tests/dashboard/test_shipping.py b/tests/dashboard/test_shipping.py
index fe7fa64d48..e0db33a0a2 100644
--- a/tests/dashboard/test_shipping.py
+++ b/tests/dashboard/test_shipping.py
@@ -11,36 +11,36 @@ def test_shipping_method_list(admin_client, shipping_method):
def test_shipping_method_add(admin_client):
- assert len(ShippingMethod.objects.all()) == 0
+ assert ShippingMethod.objects.count() == 0
url = reverse('dashboard:shipping-method-add')
data = {'name': 'Zium', 'description': 'Fastest zium'}
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
- assert len(ShippingMethod.objects.all()) == 1
+ assert ShippingMethod.objects.count() == 1
def test_shipping_method_add_not_valid(admin_client):
- assert len(ShippingMethod.objects.all()) == 0
+ assert ShippingMethod.objects.count() == 0
url = reverse('dashboard:shipping-method-add')
data = {}
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
- assert len(ShippingMethod.objects.all()) == 0
+ assert ShippingMethod.objects.count() == 0
def test_shipping_method_edit(admin_client, shipping_method):
- assert len(ShippingMethod.objects.all()) == 1
+ assert ShippingMethod.objects.count() == 1
url = reverse('dashboard:shipping-method-update',
kwargs={'pk': shipping_method.pk})
data = {'name': 'Flash', 'description': 'In a flash!'}
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
- assert len(ShippingMethod.objects.all()) == 1
+ assert ShippingMethod.objects.count() == 1
assert ShippingMethod.objects.all()[0].name == 'Flash'
def test_shipping_method_details(admin_client, shipping_method):
- assert len(ShippingMethod.objects.all()) == 1
+ assert ShippingMethod.objects.count() == 1
url = reverse('dashboard:shipping-method-details',
kwargs={'pk': shipping_method.pk})
response = admin_client.post(url, follow=True)
@@ -48,37 +48,37 @@ def test_shipping_method_details(admin_client, shipping_method):
def test_shipping_method_delete(admin_client, shipping_method):
- assert len(ShippingMethod.objects.all()) == 1
+ assert ShippingMethod.objects.count() == 1
url = reverse('dashboard:shipping-method-delete',
kwargs={'pk': shipping_method.pk})
response = admin_client.post(url, follow=True)
assert response.status_code == 200
- assert len(ShippingMethod.objects.all()) == 0
+ assert ShippingMethod.objects.count() == 0
def test_shipping_method_country_add(admin_client, shipping_method):
- assert len(ShippingMethodCountry.objects.all()) == 1
+ assert ShippingMethodCountry.objects.count() == 1
url = reverse('dashboard:shipping-method-country-add',
kwargs={'shipping_method_pk': shipping_method.pk})
data = {'country_code': 'FR', 'price': '50',
'shipping_method': shipping_method.pk}
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
- assert len(ShippingMethodCountry.objects.all()) == 2
+ assert ShippingMethodCountry.objects.count() == 2
def test_shipping_method_country_add_not_valid(admin_client, shipping_method):
- assert len(ShippingMethodCountry.objects.all()) == 1
+ assert ShippingMethodCountry.objects.count() == 1
url = reverse('dashboard:shipping-method-country-add',
kwargs={'shipping_method_pk': shipping_method.pk})
data = {}
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
- assert len(ShippingMethodCountry.objects.all()) == 1
+ assert ShippingMethodCountry.objects.count() == 1
def test_shipping_method_country_edit(admin_client, shipping_method):
- assert len(ShippingMethodCountry.objects.all()) == 1
+ assert ShippingMethodCountry.objects.count() == 1
country = shipping_method.price_per_country.all()[0]
assert country.price == Money(10, 'USD')
url = reverse('dashboard:shipping-method-country-edit',
@@ -88,18 +88,18 @@ def test_shipping_method_country_edit(admin_client, shipping_method):
'shipping_method': shipping_method.pk}
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
- assert len(ShippingMethodCountry.objects.all()) == 1
+ assert ShippingMethodCountry.objects.count() == 1
shipping_price = shipping_method.price_per_country.all()[0].price
assert shipping_price == Money(50, 'USD')
def test_shipping_method_country_delete(admin_client, shipping_method):
- assert len(ShippingMethodCountry.objects.all()) == 1
+ assert ShippingMethodCountry.objects.count() == 1
country = shipping_method.price_per_country.all()[0]
url = reverse('dashboard:shipping-method-country-delete',
kwargs={'shipping_method_pk': shipping_method.pk,
'country_pk': country.pk})
response = admin_client.post(url, follow=True)
assert response.status_code == 200
- assert len(ShippingMethodCountry.objects.all()) == 0
+ assert ShippingMethodCountry.objects.count() == 0
diff --git a/tests/dashboard/test_site_settings.py b/tests/dashboard/test_site_settings.py
index 13ed113fcc..d64e66ced4 100644
--- a/tests/dashboard/test_site_settings.py
+++ b/tests/dashboard/test_site_settings.py
@@ -98,7 +98,7 @@ def test_authorization_key_form_add(admin_client, site_settings):
kwargs={'site_settings_pk': site_settings.pk})
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
- assert len(AuthorizationKey.objects.all()) == 1
+ assert AuthorizationKey.objects.count() == 1
assert site_settings.available_backends().count() == 1
assert 'google-oauth2' in site_settings.available_backends()
@@ -111,7 +111,7 @@ def test_authorization_key_form_add_not_valid(admin_client, site_settings):
kwargs={'site_settings_pk': site_settings.pk})
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
- assert len(AuthorizationKey.objects.all()) == 0
+ assert AuthorizationKey.objects.count() == 0
assert site_settings.available_backends().count() == 0
@@ -125,7 +125,7 @@ def test_authorization_key_form_edit(
'key_pk': authorization_key.pk})
response = admin_client.post(url, data, follow=True)
assert response.status_code == 200
- assert len(AuthorizationKey.objects.all()) == 1
+ assert AuthorizationKey.objects.count() == 1
assert site_settings.available_backends().count() == 1
assert 'google-oauth2' in site_settings.available_backends()
@@ -138,7 +138,7 @@ def test_authorization_key_form_delete(
'key_pk': authorization_key.pk})
response = admin_client.post(url, follow=True)
assert response.status_code == 200
- assert len(AuthorizationKey.objects.all()) == 0
+ assert AuthorizationKey.objects.count() == 0
assert site_settings.available_backends().count() == 0
diff --git a/tests/test_discounts.py b/tests/test_discounts.py
index ae90b3b822..c918e0ac37 100644
--- a/tests/test_discounts.py
+++ b/tests/test_discounts.py
@@ -86,27 +86,27 @@ def test_checkout_discount_form_not_applicable_voucher(monkeypatch, voucher):
def test_checkout_discount_form_active_queryset_voucher_not_active(voucher):
- assert len(Voucher.objects.all()) == 1
+ assert Voucher.objects.count() == 1
checkout = Mock(cart=Mock())
voucher.start_date = date.today() + timedelta(days=1)
voucher.save()
form = CheckoutDiscountForm({'voucher': voucher.code}, checkout=checkout)
qs = form.fields['voucher'].queryset
- assert len(qs) == 0
+ assert qs.count() == 0
def test_checkout_discount_form_active_queryset_voucher_active(voucher):
- assert len(Voucher.objects.all()) == 1
+ assert Voucher.objects.count() == 1
checkout = Mock(cart=Mock())
voucher.start_date = date.today()
voucher.save()
form = CheckoutDiscountForm({'voucher': voucher.code}, checkout=checkout)
qs = form.fields['voucher'].queryset
- assert len(qs) == 1
+ assert qs.count() == 1
def test_checkout_discount_form_active_queryset_after_some_time(voucher):
- assert len(Voucher.objects.all()) == 1
+ assert Voucher.objects.count() == 1
checkout = Mock(cart=Mock())
voucher.start_date = date(year=2016, month=6, day=1)
voucher.end_date = date(year=2016, month=6, day=2)
@@ -115,17 +115,17 @@ def test_checkout_discount_form_active_queryset_after_some_time(voucher):
with freeze_time('2016-05-31'):
form = CheckoutDiscountForm(
{'voucher': voucher.code}, checkout=checkout)
- assert len(form.fields['voucher'].queryset) == 0
+ assert form.fields['voucher'].queryset.count() == 0
with freeze_time('2016-06-01'):
form = CheckoutDiscountForm(
{'voucher': voucher.code}, checkout=checkout)
- assert len(form.fields['voucher'].queryset) == 1
+ assert form.fields['voucher'].queryset.count() == 1
with freeze_time('2016-06-03'):
form = CheckoutDiscountForm(
{'voucher': voucher.code}, checkout=checkout)
- assert len(form.fields['voucher'].queryset) == 0
+ assert form.fields['voucher'].queryset.count() == 0
@pytest.mark.parametrize(