From f793badf2bc853a66da39e8612b81955bb6b2234 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Mon, 11 Dec 2017 12:04:23 +0100
Subject: [PATCH] unit tests for voucher views

---
 smash/web/forms/forms.py             |  2 +-
 smash/web/tests/functions.py         |  5 +--
 smash/web/tests/view/test_voucher.py | 54 ++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 3 deletions(-)
 create mode 100644 smash/web/tests/view/test_voucher.py

diff --git a/smash/web/forms/forms.py b/smash/web/forms/forms.py
index d1f90b47..836874bf 100644
--- a/smash/web/forms/forms.py
+++ b/smash/web/forms/forms.py
@@ -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:
diff --git a/smash/web/tests/functions.py b/smash/web/tests/functions.py
index db0e29df..ad3da907 100644
--- a/smash/web/tests/functions.py
+++ b/smash/web/tests/functions.py
@@ -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():
diff --git a/smash/web/tests/view/test_voucher.py b/smash/web/tests/view/test_voucher.py
new file mode 100644
index 00000000..3d2b0233
--- /dev/null
+++ b/smash/web/tests/view/test_voucher.py
@@ -0,0 +1,54 @@
+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())
-- 
GitLab