Skip to content

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

Add product class form tests

Krzysztof Wolskib0c1c9d1abbf
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index e4ac2cc6df..2aa5d83d02 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -43,9 +43,9 @@ class ProductClassForm(forms.ModelForm):
def clean(self):
data = super(ProductClassForm, self).clean()
- has_variants = self.cleaned_data.get('product_attributes')
- product_attr = set(self.cleaned_data.get('product_attributes', []))
- variant_attr = set(self.cleaned_data.get('variant_attributes', []))
+ has_variants = self.cleaned_data['has_variants']
+ product_attr = set(self.cleaned_data['product_attributes'])
+ variant_attr = set(self.cleaned_data['variant_attributes'])
if not has_variants and len(variant_attr) > 0:
msg = pgettext_lazy(
"Product Class Errors",
diff --git a/tests/dashboard/test_product.py b/tests/dashboard/test_product.py
index 227244d041..e630d17477 100644
--- a/tests/dashboard/test_product.py
+++ b/tests/dashboard/test_product.py
@@ -3,6 +3,9 @@ from __future__ import unicode_literals
from django.core.urlresolvers import reverse
import pytest
+from saleor.dashboard.product.forms import ProductClassForm
+from saleor.product.models import ProductClass
+
@pytest.mark.integration
@pytest.mark.django_db
@@ -22,3 +25,36 @@ def test_stock_record_update_works(admin_client, product_in_stock):
new_stock = variant.stock.get(pk=stock.pk)
assert new_stock.quantity == quantity + 5
assert new_stock.quantity_allocated == quantity_allocated
+
+
+def test_valid_product_class_form(color_attribute, size_attribute):
+ data = {'name': "Testing Class",
+ 'product_attributes': [color_attribute.pk],
+ 'variant_attributes': [size_attribute.pk],
+ 'has_variants': True}
+ form = ProductClassForm(data)
+ assert form.is_valid()
+
+ # Don't allow same attribute in both fields
+ data['variant_attributes'] = [color_attribute.pk, size_attribute.pk]
+ data['product_attributes'] = [size_attribute.pk]
+ form = ProductClassForm(data)
+ assert not form.is_valid()
+
+
+def test_variantless_product_class_form(color_attribute, size_attribute):
+ data = {'name': "Testing Class",
+ 'product_attributes': [color_attribute.pk],
+ 'variant_attributes': [],
+ 'has_variants': False}
+ form = ProductClassForm(data)
+ assert form.is_valid()
+
+ # Don't allow variant attributes when no variants
+ data = {'name': "Testing Class",
+ 'product_attributes': [color_attribute.pk],
+ 'variant_attributes': [size_attribute.pk],
+ 'has_variants': False}
+ form = ProductClassForm(data)
+ assert not form.is_valid()
+