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

unit tests for expire voucher cron job

parent 41af46a4
No related branches found
No related tags found
1 merge request!120Resolve "auto-expire of vouchers"
Pipeline #
......@@ -4,13 +4,13 @@ import os
from django.contrib.auth.models import User
from web.models.worker_study_role import ROLE_CHOICES_DOCTOR
from web.models import Location, AppointmentType, StudySubject, Worker, Visit, Appointment, ConfigurationItem, \
Language, ContactAttempt, FlyingTeam, Availability, Subject, Study, StudyColumns, StudyNotificationParameters, \
VoucherType, VoucherTypePrice, Voucher, WorkerStudyRole
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, VOUCHER_STATUS_NEW, GLOBAL_STUDY_ID
from web.models.worker_study_role import ROLE_CHOICES_DOCTOR
from web.redcap_connector import RedcapSubject
from web.views.notifications import get_today_midnight_date
......@@ -68,10 +68,21 @@ def create_study(name="test"):
return Study.objects.create(name=name, columns=study_columns, notification_parameters=notification_parameters)
TEST_ID_COUNTER = 0
def get_test_id():
global TEST_ID_COUNTER
result = str(TEST_ID_COUNTER)
TEST_ID_COUNTER += 1
return result
def create_voucher(study_subject=None):
if study_subject is None:
study_subject = create_study_subject()
return Voucher.objects.create(number="123456",
number = str(get_test_id())
return Voucher.objects.create(number=number,
study_subject=study_subject,
issue_date=get_today_midnight_date(),
expiry_date=get_today_midnight_date(),
......
import logging
import datetime
from django.urls import reverse
from web.views.notifications import get_today_midnight_date
from web.views.voucher import ExpireVouchersJob
from web.forms import VoucherForm
from web.models import Voucher
from web.models.constants import VOUCHER_STATUS_NEW
from web.models.constants import VOUCHER_STATUS_NEW, VOUCHER_STATUS_USED, VOUCHER_STATUS_EXPIRED
from web.tests.functions import create_voucher, create_study_subject, format_form_field, create_voucher_type
from .. import LoggedInTestCase
......@@ -56,3 +59,25 @@ class VoucherTypeViewTests(LoggedInTestCase):
self.assertEqual(response.status_code, 302)
self.assertEqual(1, Voucher.objects.all().count())
def test_expire_voucher_cron_job(self):
voucher = create_voucher()
voucher.status = VOUCHER_STATUS_NEW
voucher.expiry_date = "2011-01-01"
voucher.save()
status = ExpireVouchersJob().do()
self.assertTrue("1" in status)
updated_voucher = Voucher.objects.get(id=voucher.id)
self.assertEqual(VOUCHER_STATUS_EXPIRED, updated_voucher.status)
def test_expire_voucher_cron_job_with_no_vouchers_to_update(self):
voucher = create_voucher()
voucher.status = VOUCHER_STATUS_USED
voucher.expiry_date = "2011-01-01"
voucher.save()
voucher = create_voucher()
voucher.status = VOUCHER_STATUS_NEW
voucher.expiry_date = get_today_midnight_date() + datetime.timedelta(days=2)
voucher.save()
status = ExpireVouchersJob().do()
self.assertTrue("0" in status)
......@@ -8,7 +8,7 @@ from django.views.generic import ListView
from django.views.generic import UpdateView
from django_cron import CronJobBase, Schedule
from views.notifications import get_today_midnight_date
from web.views.notifications import get_today_midnight_date
from web.forms import VoucherForm
from web.models import Voucher, StudySubject
from web.models.constants import GLOBAL_STUDY_ID, VOUCHER_STATUS_NEW, VOUCHER_STATUS_EXPIRED
......@@ -80,4 +80,4 @@ class ExpireVouchersJob(CronJobBase):
count = vouchers.count()
if count > 0:
vouchers.update(status=VOUCHER_STATUS_EXPIRED)
return count + " vouchers expired"
return str(count) + " vouchers expired"
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