diff --git a/smash/web/tests/functions.py b/smash/web/tests/functions.py index cfd5cf42dd4cad7ec5309ade610019151c104747..52ba55dfdfa75cfec5161885500b5294244371a4 100644 --- a/smash/web/tests/functions.py +++ b/smash/web/tests/functions.py @@ -6,7 +6,7 @@ from django.contrib.auth.models import User from web.models import Location, AppointmentType, StudySubject, Worker, Visit, Appointment, ConfigurationItem, \ Language, ContactAttempt, FlyingTeam, Availability, Subject, Study, StudyColumns, StudyNotificationParameters, \ - VoucherType + VoucherType, VoucherTypePrice 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 @@ -31,6 +31,13 @@ def create_voucher_type(): return VoucherType.objects.create(code="X", study=get_test_study()) +def create_voucher_type_price(): + return VoucherTypePrice.objects.create(voucher_type=create_voucher_type(), + price=12.34, + start_date=get_today_midnight_date(), + end_date=get_today_midnight_date()) + + def create_empty_study_columns(): study_columns = StudyColumns.objects.create( postponed=False, @@ -238,7 +245,9 @@ def get_resource_path(filename): def format_form_field(value): - if isinstance(value, datetime.datetime): + if isinstance(value, datetime.date): + return value.strftime('%Y-%m-%d') + elif isinstance(value, datetime.datetime): return value.strftime('%Y-%m-%d %H:%M') else: return value diff --git a/smash/web/tests/view/test_voucher_type_price.py b/smash/web/tests/view/test_voucher_type_price.py new file mode 100644 index 0000000000000000000000000000000000000000..be90a2fd10abf9ffe14a774be02eee329d1e4fab --- /dev/null +++ b/smash/web/tests/view/test_voucher_type_price.py @@ -0,0 +1,58 @@ +import logging + +from django.urls import reverse + +from web.forms.forms import VoucherTypePriceForm +from web.models import VoucherType, VoucherTypePrice +from web.tests.functions import create_voucher_type, create_voucher_type_price, format_form_field +from .. import LoggedInTestCase + +logger = logging.getLogger(__name__) + + +class VoucherTypePriceViewTests(LoggedInTestCase): + def setUp(self): + super(VoucherTypePriceViewTests, self).setUp() + self.voucher_type = create_voucher_type() + + def test_render_add_voucher_type_price_request(self): + url = reverse('web.views.voucher_type_price_add', kwargs={'voucher_type_id': self.voucher_type.id}) + response = self.client.get(url + ) + self.assertEqual(response.status_code, 200) + + def test_render_edit_voucher_type_price_request(self): + voucher_type_price = create_voucher_type_price() + url = reverse('web.views.voucher_type_price_edit', + kwargs={'pk': voucher_type_price.id, 'voucher_type_id': self.voucher_type.id}) + + response = self.client.get(url) + + self.assertEqual(response.status_code, 200) + + def test_add_voucher_type_price(self): + form_data = { + 'price': "11.0", + 'start_date': "2017-01-01", + 'end_date': "2018-01-01" + } + url = reverse('web.views.voucher_type_price_add', kwargs={'voucher_type_id': self.voucher_type.id}) + response = self.client.post(url, data=form_data) + self.assertEqual(response.status_code, 302) + + self.assertEqual(1, VoucherType.objects.all().count()) + + def test_edit_voucher_type_price(self): + voucher_type_price = create_voucher_type_price() + form = VoucherTypePriceForm(instance=voucher_type_price) + form_data = {} + for key, value in form.initial.items(): + if value is not None: + form_data['{}'.format(key)] = format_form_field(value) + form_data['price'] = 90.50 + + url = reverse('web.views.voucher_type_price_edit', + kwargs={'pk': voucher_type_price.id, 'voucher_type_id': voucher_type_price.voucher_type.id}) + response = self.client.post(url, data=form_data) + + self.assertEqual(90.50, VoucherTypePrice.objects.get(id=voucher_type_price.id).price)