Skip to content

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

Add test for new type of start and end dates in vouchers

fowczarekd241afe29c85
diff --git a/tests/api/test_discount.py b/tests/api/test_discount.py
index 6ec6689e4f..6d2bf57786 100644
--- a/tests/api/test_discount.py
+++ b/tests/api/test_discount.py
@@ -2,6 +2,7 @@ from datetime import date, timedelta
import graphene
import pytest
+from django.utils import timezone
from django_countries import countries
from saleor.discount import DiscountValueType, VoucherType
@@ -139,7 +140,8 @@ mutation voucherCreate(
$type: VoucherTypeEnum, $name: String, $code: String,
$discountValueType: DiscountValueTypeEnum,
$discountValue: Decimal, $minAmountSpent: Decimal,
- $startDate: Date, $endDate: Date, $applyOncePerOrder: Boolean) {
+ $startDate: Date, $endDate: Date, ) {
+ $startDate: DateTime, $endDate: DateTime, $applyOncePerOrder: Boolean) {
voucherCreate(input: {
name: $name, type: $type, code: $code,
discountValueType: $discountValueType,
@@ -169,8 +171,8 @@ mutation voucherCreate(
def test_create_voucher(staff_api_client, permission_manage_discounts):
- start_date = date(day=1, month=1, year=2018)
- end_date = date(day=1, month=1, year=2019)
+ start_date = timezone.now() - timedelta(days=365)
+ end_date = timezone.now() + timedelta(days=365)
variables = {
"name": "test voucher",
"type": VoucherTypeEnum.VALUE.name,
@@ -199,8 +201,8 @@ def test_create_voucher(staff_api_client, permission_manage_discounts):
def test_create_voucher_with_empty_code(staff_api_client, permission_manage_discounts):
- start_date = date(day=1, month=1, year=2018)
- end_date = date(day=1, month=1, year=2019)
+ start_date = timezone.now() - timedelta(days=365)
+ end_date = timezone.now() + timedelta(days=365)
variables = {
"name": "test voucher",
"type": VoucherTypeEnum.VALUE.name,
@@ -224,8 +226,8 @@ def test_create_voucher_with_empty_code(staff_api_client, permission_manage_disc
def test_create_voucher_with_existing_gift_card_code(
staff_api_client, gift_card, permission_manage_discounts
):
- start_date = date(day=1, month=1, year=2018)
- end_date = date(day=1, month=1, year=2019)
+ start_date = timezone.now() - timedelta(days=365)
+ end_date = timezone.now() + timedelta(days=365)
variables = {
"name": "test voucher",
"type": VoucherTypeEnum.VALUE.name,
@@ -250,8 +252,8 @@ def test_create_voucher_with_existing_gift_card_code(
def test_create_voucher_with_existing_voucher_code(
staff_api_client, voucher_shipping_type, permission_manage_discounts
):
- start_date = date(day=1, month=1, year=2018)
- end_date = date(day=1, month=1, year=2019)
+ start_date = timezone.now() - timedelta(days=365)
+ end_date = timezone.now() + timedelta(days=365)
variables = {
"name": "test voucher",
"type": VoucherTypeEnum.VALUE.name,
@@ -742,12 +744,22 @@ def test_sale_remove_no_catalogues(
@pytest.mark.parametrize(
"voucher_filter, start_date, end_date, count",
[
- ({"status": "ACTIVE"}, date(2015, 1, 1), date(2020, 1, 1), 2),
- ({"status": "EXPIRED"}, date(2015, 1, 1), date(2018, 1, 1), 1),
+ (
+ {"status": "ACTIVE"},
+ timezone.now().replace(year=2015, month=1, day=1),
+ timezone.now().replace(year=2020, month=1, day=1),
+ 2,
+ ),
+ (
+ {"status": "EXPIRED"},
+ timezone.now().replace(year=2015, month=1, day=1),
+ timezone.now().replace(year=2018, month=1, day=1),
+ 1,
+ ),
(
{"status": "SCHEDULED"},
- date.today() + timedelta(days=3),
- date.today() + timedelta(days=10),
+ timezone.now() + timedelta(days=3),
+ timezone.now() + timedelta(days=10),
1,
),
],
@@ -764,7 +776,10 @@ def test_query_vouchers_with_filter_status(
Voucher.objects.bulk_create(
[
Voucher(
- name="Voucher1", discount_value=123, code="abc", start_date=date.today()
+ name="Voucher1",
+ discount_value=123,
+ code="abc",
+ start_date=timezone.now(),
),
Voucher(
name="Voucher2",
@@ -817,10 +832,18 @@ def test_query_vouchers_with_filter_times_used(
@pytest.mark.parametrize(
"voucher_filter, count",
[
- ({"started": {"gte": "2019-04-18"}}, 1),
- ({"started": {"lte": "2012-01-14"}}, 1),
- ({"started": {"lte": "2012-01-15", "gte": "2012-01-01"}}, 1),
- ({"started": {"gte": "2012-01-03"}}, 2),
+ ({"started": {"gte": "2019-04-18T00:00:00+00:00"}}, 1),
+ ({"started": {"lte": "2012-01-14T00:00:00+00:00"}}, 1),
+ (
+ {
+ "started": {
+ "lte": "2012-01-15T00:00:00+00:00",
+ "gte": "2012-01-01T00:00:00+00:00",
+ }
+ },
+ 1,
+ ),
+ ({"started": {"gte": "2012-01-03T00:00:00+00:00"}}, 2),
],
)
def test_query_vouchers_with_filter_started(
@@ -837,7 +860,7 @@ def test_query_vouchers_with_filter_started(
name="Voucher2",
discount_value=123,
code="123",
- start_date=date(2012, 1, 5),
+ start_date=timezone.now().replace(year=2012, month=1, day=5),
),
]
)
diff --git a/tests/dashboard/test_discounts.py b/tests/dashboard/test_discounts.py
index 33ec805fd4..16bee7aa6d 100644
--- a/tests/dashboard/test_discounts.py
+++ b/tests/dashboard/test_discounts.py
@@ -1,14 +1,11 @@
import json
-from datetime import date
from decimal import Decimal
from unittest.mock import Mock
+import pytest
from django.urls import reverse
-
from prices import Money, TaxedMoney
-import pytest
-
from saleor.dashboard.order.utils import get_voucher_discount_for_order
from saleor.discount import DiscountValueType, VoucherType
from saleor.discount.models import NotApplicable, Sale, Voucher
@@ -48,8 +45,8 @@ def test_voucher_shipping_add(admin_client):
assert voucher.type == VoucherType.SHIPPING
assert voucher.code == data["code"]
assert voucher.name == data["name"]
- assert voucher.start_date == date(2018, 1, 1)
- assert voucher.end_date == date(2018, 6, 1)
+ assert voucher.start_date.isoformat() == "2018-01-01T06:00:00+00:00"
+ assert voucher.end_date.isoformat() == "2018-06-01T05:00:00+00:00"
assert voucher.discount_value_type == DiscountValueType.FIXED
assert voucher.discount_value == Decimal("15.99")
assert voucher.min_amount_spent == Money("59.99", "USD")
@@ -258,8 +255,8 @@ def test_voucher_form_min_amount_spent_is_changed_on_edit(
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.start_date.isoformat() == "2019-01-01T06:00:00+00:00"
+ assert voucher.end_date.isoformat() == "2019-06-01T05:00:00+00:00"
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_checkout.py b/tests/test_checkout.py
index ee97d1cad3..3b394cbc44 100644
--- a/tests/test_checkout.py
+++ b/tests/test_checkout.py
@@ -4,6 +4,7 @@ from unittest.mock import Mock, patch
import pytest
from django.contrib.auth.models import AnonymousUser
from django.urls import reverse
+from django.utils import timezone
from django_countries.fields import Country
from freezegun import freeze_time
from prices import Money, TaxedMoney, TaxedMoneyRange
@@ -497,7 +498,7 @@ def test_view_checkout_place_order_with_expired_voucher_code(
checkout.shipping_method = shipping_zone.shipping_methods.first()
# set voucher to be expired
- yesterday = datetime.date.today() - datetime.timedelta(days=1)
+ yesterday = timezone.now() - datetime.timedelta(days=1)
voucher.end_date = yesterday
voucher.save()
@@ -1118,7 +1119,7 @@ def test_checkout_voucher_form_active_queryset_voucher_not_active(
voucher, request_checkout_with_item
):
assert Voucher.objects.count() == 1
- voucher.start_date = datetime.date.today() + datetime.timedelta(days=1)
+ voucher.start_date = timezone.now() + datetime.timedelta(days=1)
voucher.save()
form = CheckoutVoucherForm(
{"voucher": voucher.code}, instance=request_checkout_with_item
@@ -1131,7 +1132,7 @@ def test_checkout_voucher_form_active_queryset_voucher_active(
voucher, request_checkout_with_item
):
assert Voucher.objects.count() == 1
- voucher.start_date = datetime.date.today()
+ voucher.start_date = timezone.now()
voucher.save()
form = CheckoutVoucherForm(
{"voucher": voucher.code}, instance=request_checkout_with_item
@@ -1144,8 +1145,8 @@ def test_checkout_voucher_form_active_queryset_after_some_time(
voucher, request_checkout_with_item
):
assert Voucher.objects.count() == 1
- voucher.start_date = datetime.date(year=2016, month=6, day=1)
- voucher.end_date = datetime.date(year=2016, month=6, day=2)
+ voucher.start_date = timezone.now().replace(year=2016, month=6, day=1, hour=0)
+ voucher.end_date = timezone.now().replace(year=2016, month=6, day=2, hour=0)
voucher.save()
with freeze_time("2016-05-31"):
@@ -1154,7 +1155,7 @@ def test_checkout_voucher_form_active_queryset_after_some_time(
)
assert form.fields["voucher"].queryset.count() == 0
- with freeze_time("2016-06-01"):
+ with freeze_time("2016-06-01T05:00:00+00:00"):
form = CheckoutVoucherForm(
{"voucher": voucher.code}, instance=request_checkout_with_item
)
@@ -1198,7 +1199,7 @@ def test_get_voucher_for_checkout(checkout_with_voucher, voucher):
def test_get_voucher_for_checkout_expired_voucher(checkout_with_voucher, voucher):
- date_yesterday = datetime.date.today() - datetime.timedelta(days=1)
+ date_yesterday = timezone.now() - datetime.timedelta(days=1)
voucher.end_date = date_yesterday
voucher.save()
checkout_voucher = get_voucher_for_checkout(checkout_with_voucher)
@@ -1250,7 +1251,7 @@ def test_recalculate_checkout_discount_voucher_not_applicable(
def test_recalculate_checkout_discount_expired_voucher(checkout_with_voucher, voucher):
checkout = checkout_with_voucher
- date_yesterday = datetime.date.today() - datetime.timedelta(days=1)
+ date_yesterday = timezone.now() - datetime.timedelta(days=1)
voucher.end_date = date_yesterday
voucher.save()
diff --git a/tests/test_discounts.py b/tests/test_discounts.py
index 45bf2cbb03..fe6cae46df 100644
--- a/tests/test_discounts.py
+++ b/tests/test_discounts.py
@@ -1,6 +1,7 @@
from datetime import date, timedelta
import pytest
+from django.utils import timezone
from prices import Money, TaxedMoney
from saleor.checkout.utils import get_voucher_discount_for_checkout
@@ -79,7 +80,7 @@ def test_percentage_discounts(product):
def test_voucher_queryset_active(voucher):
vouchers = Voucher.objects.all()
assert len(vouchers) == 1
- active_vouchers = Voucher.objects.active(date=date.today() - timedelta(days=1))
+ active_vouchers = Voucher.objects.active(date=timezone.now() - timedelta(days=1))
assert len(active_vouchers) == 0