Skip to content

Commit

Permalink
OM-239 Fixed missing config handling in validation (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
malinowskikam authored Sep 12, 2024
1 parent ae3d8fe commit ee5521a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
40 changes: 18 additions & 22 deletions worker_voucher/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ def _check_voucher_limit(insuree, user, policyholder, year, count=1):


def _check_dates(date_ranges: List[Dict]):
expiry_period = WorkerVoucherConfig.voucher_expiry_period
max_date = datetime.date.today() + datetime.datetimedelta(**expiry_period)
max_date = _get_voucher_expiry_date(datetime.date.today())
dates = set()
for date_range in date_ranges:
start_date, end_date = date_range.get("start_date"), date_range.get("end_date")
Expand All @@ -211,6 +210,19 @@ def _check_dates(date_ranges: List[Dict]):
raise VoucherException(_(f"No valid dates"))
return dates

def _get_voucher_expiry_date(start_date: datetime):
expiry_type = WorkerVoucherConfig.voucher_expiry_type

if expiry_type == "end_of_year":
expiry_date = datetime.date(start_date.year, 12, 31)
elif expiry_type == "fixed_period":
expiry_period = WorkerVoucherConfig.voucher_expiry_period
expiry_date = datetime.date.today() + datetime.datetimedelta(**expiry_period)
else:
raise VoucherException(_("Invalid voucher expiry type"))

return expiry_date


def check_existing_active_vouchers(ph, insurees, dates):
if isinstance(dates, set):
Expand Down Expand Up @@ -256,16 +268,8 @@ def get_worker_yearly_voucher_count_counts(insuree: Insuree, user: User, year):


def create_assigned_voucher(user, date, insuree_id, policyholder_id):
expiry_type = WorkerVoucherConfig.voucher_expiry_type

if expiry_type == "end_of_year":
current_date = datetime.datetime.now()
expiry_date = datetime.datetime(current_date.year, 12, 31, 23, 59, 59)
elif expiry_type == "fixed_period":
expiry_period = WorkerVoucherConfig.voucher_expiry_period
expiry_date = datetime.datetime.now() + datetime.datetimedelta(**expiry_period)
else:
raise ValueError(f"Unknown expiry type: {expiry_type}")
current_date = datetime.date.today()
expiry_date = _get_voucher_expiry_date(current_date)

voucher_service = WorkerVoucherService(user)
service_result = voucher_service.create({
Expand All @@ -282,16 +286,8 @@ def create_assigned_voucher(user, date, insuree_id, policyholder_id):


def create_unassigned_voucher(user, policyholder_id):
expiry_type = WorkerVoucherConfig.voucher_expiry_type

if expiry_type == "end_of_year":
current_date = datetime.datetime.now()
expiry_date = datetime.datetime(current_date.year, 12, 31, 23, 59, 59)
elif expiry_type == "fixed_period":
expiry_period = WorkerVoucherConfig.voucher_expiry_period
expiry_date = datetime.datetime.now() + datetime.datetimedelta(**expiry_period)
else:
raise ValueError(f"Unknown expiry type: {expiry_type}")
current_date = datetime.date.today()
expiry_date = _get_voucher_expiry_date(current_date)

voucher_service = WorkerVoucherService(user)
service_result = voucher_service.create({
Expand Down
17 changes: 16 additions & 1 deletion worker_voucher/tests/test_validate_acquire_assigned.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from worker_voucher.apps import WorkerVoucherConfig
from worker_voucher.services import validate_acquire_assigned_vouchers, create_assigned_voucher
from worker_voucher.tests.util import create_test_eu_for_user, create_test_worker_for_eu, \
OverrideAppConfigContextManager as override_config
OverrideAppConfig as override_config


class ValidateAcquireAssignedTestCase(TestCase):
Expand Down Expand Up @@ -83,6 +83,21 @@ def test_validate_dates_overlap(self):

self.assertFalse(res['success'])

@override_config(WorkerVoucherConfig, {"voucher_expiry_type": "end_of_year"})
def test_validate_end_of_year(self):
end_of_year = datetime.date(datetime.date.today().year, 12, 31)

payload = (
self.user,
self.eu.code,
(self.worker.chf_id,),
([{'start_date': end_of_year, 'end_date': end_of_year}])
)

res = validate_acquire_assigned_vouchers(*payload)

self.assertTrue(res['success'])

@override_config(WorkerVoucherConfig, {"yearly_worker_voucher_limit": 3,
"voucher_expiry_type": "fixed_period",
"voucher_expiry_period": {"years": 2}})
Expand Down
7 changes: 4 additions & 3 deletions worker_voucher/tests/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import random
from contextlib import ContextDecorator
from typing import Any, Type

from django.apps import AppConfig

Expand Down Expand Up @@ -74,15 +75,15 @@ def generate_idnp():
return str(idnp_first_12_digits) + str(get_idnp_crc(str(idnp_first_12_digits)))


class OverrideAppConfigContextManager(ContextDecorator):
class OverrideAppConfig(ContextDecorator):
"""
Context manager/decorator for overriding default config for tests
"""
config_class: AppConfig
config_class: Type[AppConfig]
temp_config: dict
original_config: dict

def __init__(self, config_class, config):
def __init__(self, config_class: Type[AppConfig], config: dict[str, Any]):
self.config_class = config_class
self.temp_config = config
self.original_config = dict()
Expand Down

0 comments on commit ee5521a

Please sign in to comment.