def test_checkout_voucher_form_active_queryset_after_some_time( voucher, request_checkout_with_item): assert Voucher.objects.count() == 1 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"): form = CheckoutVoucherForm({"voucher": voucher.code}, instance=request_checkout_with_item) assert form.fields["voucher"].queryset.count() == 0 with freeze_time("2016-06-01T05:00:00+00:00"): form = CheckoutVoucherForm({"voucher": voucher.code}, instance=request_checkout_with_item) assert form.fields["voucher"].queryset.count() == 1 with freeze_time("2016-06-03"): form = CheckoutVoucherForm({"voucher": voucher.code}, instance=request_checkout_with_item) assert form.fields["voucher"].queryset.count() == 0
def test_checkout_voucher_form_voucher_not_applicable( voucher, request_checkout_with_item): voucher.min_amount_spent = 200 voucher.save() form = CheckoutVoucherForm({"voucher": voucher.code}, instance=request_checkout_with_item) assert not form.is_valid() assert "voucher" in form.errors
def test_checkout_voucher_form_invalid_voucher_code( monkeypatch, request_checkout_with_item ): form = CheckoutVoucherForm( {"voucher": "invalid"}, instance=request_checkout_with_item ) assert not form.is_valid() assert "voucher" in form.errors
def test_checkout_voucher_form_voucher_not_applicable( voucher, request_checkout_with_item ): voucher.min_amount_spent = 200 voucher.save() form = CheckoutVoucherForm( {"voucher": voucher.code}, instance=request_checkout_with_item ) assert not form.is_valid() assert "voucher" in form.errors
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.save() form = CheckoutVoucherForm({"voucher": voucher.code}, instance=request_checkout_with_item) qs = form.fields["voucher"].queryset assert qs.count() == 1
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.save() form = CheckoutVoucherForm({'voucher': voucher.code}, instance=request_checkout_with_item) qs = form.fields['voucher'].queryset assert qs.count() == 0
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.save() with freeze_time('2016-05-31'): form = CheckoutVoucherForm({'voucher': voucher.code}, instance=request_checkout_with_item) assert form.fields['voucher'].queryset.count() == 0 with freeze_time('2016-06-01'): form = CheckoutVoucherForm({'voucher': voucher.code}, instance=request_checkout_with_item) assert form.fields['voucher'].queryset.count() == 1 with freeze_time('2016-06-03'): form = CheckoutVoucherForm({'voucher': voucher.code}, instance=request_checkout_with_item) assert form.fields['voucher'].queryset.count() == 0
def test_checkout_voucher_form_invalid_voucher_code( monkeypatch, request_checkout_with_item): form = CheckoutVoucherForm({"voucher": "invalid"}, instance=request_checkout_with_item) assert not form.is_valid() assert "voucher" in form.errors