Skip to content
Snippets Groups Projects
Commit f793badf authored by Piotr Gawron's avatar Piotr Gawron
Browse files

unit tests for voucher views

parent 7c27133b
No related branches found
No related tags found
1 merge request!113Resolve "voucher functionality"
......@@ -443,7 +443,7 @@ class VoucherForm(ModelForm):
instance.issue_date = timezone.now()
instance.expiry_date = instance.issue_date + datetime.timedelta(days=92)
max_id = str(0).zfill(5)
if Voucher.objects.latest('id'):
if Voucher.objects.all().count() > 0:
max_id = str(Voucher.objects.latest('id').id).zfill(5)
instance.number = max_id + VerhoeffAlgorithm.calculate_verhoeff_check_sum(max_id)
if instance.status == VOUCHER_STATUS_USED and not instance.use_date:
......
......@@ -9,7 +9,7 @@ from web.models import Location, AppointmentType, StudySubject, Worker, Visit, A
VoucherType, VoucherTypePrice, Voucher
from web.models.constants import REDCAP_TOKEN_CONFIGURATION_TYPE, REDCAP_BASE_URL_CONFIGURATION_TYPE, \
SEX_CHOICES_MALE, SUBJECT_TYPE_CHOICES_CONTROL, CONTACT_TYPES_PHONE, \
MONDAY_AS_DAY_OF_WEEK, COUNTRY_AFGHANISTAN_ID
MONDAY_AS_DAY_OF_WEEK, COUNTRY_AFGHANISTAN_ID, VOUCHER_STATUS_NEW
from web.redcap_connector import RedcapSubject
from web.views.notifications import get_today_midnight_date
......@@ -74,7 +74,8 @@ def create_voucher(study_subject=None):
study_subject=study_subject,
issue_date=get_today_midnight_date(),
expiry_date=get_today_midnight_date(),
voucher_type=create_voucher_type())
voucher_type=create_voucher_type(),
status = VOUCHER_STATUS_NEW)
def create_empty_notification_parameters():
......
import logging
from django.urls import reverse
from web.forms.forms import VoucherForm
from web.models import Voucher
from web.models.constants import VOUCHER_STATUS_NEW
from web.tests.functions import create_voucher, create_study_subject, format_form_field, create_voucher_type
from .. import LoggedInTestCase
logger = logging.getLogger(__name__)
class VoucherTypeViewTests(LoggedInTestCase):
def test_render_add_voucher_request(self):
response = self.client.get(reverse('web.views.voucher_add'))
self.assertEqual(response.status_code, 200)
def test_render_edit_voucher_request(self):
voucher = create_voucher()
response = self.client.get(reverse('web.views.voucher_edit', kwargs={'pk': voucher.id}))
self.assertEqual(response.status_code, 200)
def test_add_voucher(self):
study_subject = create_study_subject()
visit_detail_form = VoucherForm()
form_data = {
"status": VOUCHER_STATUS_NEW,
"voucher_type": create_voucher_type().id
}
for key, value in visit_detail_form.initial.items():
form_data[key] = format_form_field(value)
url = reverse('web.views.voucher_add') + '?study_subject_id=' + str(study_subject.id)
response = self.client.post(url, data=form_data)
self.assertEqual(response.status_code, 302)
self.assertEqual(1, Voucher.objects.all().count())
def test_edit_voucher(self):
voucher = create_voucher()
voucher_form = VoucherForm(instance=voucher)
form_data = {}
for key, value in voucher_form.initial.items():
form_data[key] = format_form_field(value)
form_data["usage_partner"] = ""
form_data["use_date"] = ""
url = reverse('web.views.voucher_edit', kwargs={'pk': voucher.id})
response = self.client.post(url, data=form_data)
self.assertEqual(response.status_code, 302)
self.assertEqual(1, Voucher.objects.all().count())
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment