Skip to content

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

adding tests in the right way

MahmoudRizk552a48c3f9c1
diff --git a/tests/dashboard/test_discounts.py b/tests/dashboard/test_discounts.py
index e1db457d3b..11cc870f6f 100644
--- a/tests/dashboard/test_discounts.py
+++ b/tests/dashboard/test_discounts.py
@@ -216,4 +216,46 @@ def test_ajax_voucher_list(admin_client, voucher):
resp_decoded = json.loads(response.content.decode("utf-8"))
assert response.status_code == 200
- assert resp_decoded == {"results": vouchers_list}
+ assert resp_decoded == {'results': vouchers_list}
+
+
[email protected]('voucher_type', [
+ 'collection', 'category', 'product', 'value', 'shipping'
+])
+def test_voucher_form_min_amount_spent_is_changed_on_edit(admin_client, product, collection, voucher_type):
+ assert Voucher.objects.count() == 0
+ url = reverse('dashboard:voucher-add')
+ data = {
+ 'code': 'TESTVOUCHER',
+ 'name': 'Test Voucher',
+ 'start_date': '2019-01-01',
+ 'end_date': '2019-06-01',
+ 'type': voucher_type,
+ 'discount_value': '15.99',
+ 'discount_value_type': DiscountValueType.FIXED,
+ 'product-products': [product.pk],
+ 'category-categories': [product.category.pk],
+ 'collection-collections': [collection.pk],
+ 'shipping-min_amount_spent': '400',
+ 'product-min_amount_spent': '400',
+ 'category-min_amount_spent': '400',
+ 'collection-min_amount_spent': '400',
+ 'value-min_amount_spent': '400',
+ }
+
+ data['{}-min_amount_spent'.format(voucher_type)] = '800'
+
+ response = admin_client.post(url, data, follow=True)
+
+ assert response.status_code == 200
+ assert Voucher.objects.count() == 1
+
+ voucher = Voucher.objects.all()[0]
+ assert voucher.type == voucher_type
+ assert voucher.code == data['code']
+ assert voucher.name == data['name']
+ assert voucher.start_date == date(2019, 1, 1)
+ assert voucher.end_date == date(2019, 6, 1)
+ assert voucher.discount_value_type == DiscountValueType.FIXED
+ assert voucher.discount_value == Decimal('15.99')
+ assert voucher.min_amount_spent == Money('800', 'USD')
diff --git a/tests/test_discounts.py b/tests/test_discounts.py
index 77d802cf6b..a56215ab87 100644
--- a/tests/test_discounts.py
+++ b/tests/test_discounts.py
@@ -15,11 +15,7 @@ from saleor.discount.utils import (
increase_voucher_usage,
)
from saleor.product.models import Product, ProductVariant
-
-from saleor.discount import VoucherType
-from saleor.dashboard.discount import forms
-from saleor.dashboard.discount.views import get_voucher_type_forms
-from saleor.product.models import Collection, Category, ProductType
+from saleor.product.models import Collection
def get_min_amount_spent(min_amount_spent):
@@ -289,50 +285,3 @@ def test_sale_active(current_date, start_date, end_date, is_active):
)
sale_is_active = Sale.objects.active(date=current_date).exists()
assert is_active == sale_is_active
-
-
[email protected]('voucher_type',[
- 'collection', 'category', 'product', 'value', 'shipping'
-])
-def test_voucher_form_min_amount_spent(voucher_type):
- data = {
- 'code' : 'C700C268D0DE',
- 'collection-min_amount_spent': 800,
- 'category-min_amount_spent': 800,
- 'product-min_amount_spent': 800,
- 'value-min_amount_spent': 800,
- 'shipping-min_amount_spent': 800,
- 'type': voucher_type, # <-------------
- 'discount_value': 5,
- 'discount_value_type': 'fixed',
- 'collection-collections': ['1'],
- 'category-categories': ['1'],
- 'product-products': ['1'],
- 'start_date': '2019-05-05'
- }
-
- collection = Collection(name='test')
- collection.save()
-
- category = Category(name='test')
- category.save()
-
- productType = ProductType(name='test')
- productType.save()
-
- product = Product(name='test', description='blablabla', price=100, category=category)
- product.product_type = productType
- product.save()
-
- voucher = Voucher()
- type_base_forms = get_voucher_type_forms(voucher, data)
- voucher_form = forms.VoucherForm(data or None, instance=voucher)
-
- if voucher_form.is_valid():
- voucher_type = voucher_form.cleaned_data.get('type')
- form_type = type_base_forms.get(voucher_type)
-
- if form_type.is_valid():
- voucher = form_type.save(commit=False)
-
- assert voucher.min_amount_spent == get_min_amount_spent(800)