From d73c56a20491a7279564ae0ae24ebcec84ee9681 Mon Sep 17 00:00:00 2001 From: sniedzielski Date: Mon, 2 May 2022 16:54:56 +0200 Subject: [PATCH 1/7] OP-758: fixed test due to using in bad way 'cursor' mock --- policy/test_services.py | 88 ++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 45 deletions(-) diff --git a/policy/test_services.py b/policy/test_services.py index 16b0946..89a4812 100644 --- a/policy/test_services.py +++ b/policy/test_services.py @@ -31,51 +31,49 @@ def test_eligibility_request_permission_denied(self): service.request(req) mock_user.has_perms.assert_called_with(PolicyConfig.gql_query_eligibilities_perms) - def test_eligibility_request_all_good(self): - with mock.patch("django.db.backends.utils.CursorWrapper") as mock_cursor: - return_values = [ - list(range(1, 13)), - [ - core.datetime.date(2020, 1, 9), - core.datetime.date(2020, 1, 10), - 20, - 21, - True, - True, - ], - ][::-1] - - mock_cursor.return_value.__enter__.return_value.fetchone = ( - lambda: return_values.pop() - ) - mock_user = mock.Mock(is_anonymous=False) - mock_user.has_perm = mock.MagicMock(return_value=True) - req = EligibilityRequest(chf_id="a") - service = StoredProcEligibilityService(mock_user) - res = service.request(req, EligibilityResponse(req)) - - expected = EligibilityResponse( - eligibility_request=req, - prod_id=1, - total_admissions_left=2, - total_visits_left=3, - total_consultations_left=4, - total_surgeries_left=5, - total_deliveries_left=6, - total_antenatal_left=7, - consultation_amount_left=8, - surgery_amount_left=9, - delivery_amount_left=10, - hospitalization_amount_left=11, - antenatal_amount_left=12, - min_date_service=core.datetime.date(2020, 1, 9), - min_date_item=core.datetime.date(2020, 1, 10), - service_left=20, - item_left=21, - is_item_ok=True, - is_service_ok=True, - ) - self.assertEquals(expected, res) + @mock.patch('django.db.connections') + def test_eligibility_request_all_good(self, mock_connections): + return_values = [ + list(range(1, 13)), + [ + core.datetime.date(2020, 1, 9), + core.datetime.date(2020, 1, 10), + 20, + 21, + True, + True, + ], + ][::-1] + mock_connections.__getitem__.return_value.cursor.return_value \ + .__enter__.return_value.fetchone = (lambda: return_values.pop()) + mock_user = mock.Mock(is_anonymous=False) + mock_user.has_perm = mock.MagicMock(return_value=True) + req = EligibilityRequest(chf_id="a") + service = StoredProcEligibilityService(mock_user) + res = service.request(req, EligibilityResponse(req)) + + expected = EligibilityResponse( + eligibility_request=req, + prod_id=1, + total_admissions_left=2, + total_visits_left=3, + total_consultations_left=4, + total_surgeries_left=5, + total_deliveries_left=6, + total_antenatal_left=7, + consultation_amount_left=8, + surgery_amount_left=9, + delivery_amount_left=10, + hospitalization_amount_left=11, + antenatal_amount_left=12, + min_date_service=core.datetime.date(2020, 1, 9), + min_date_item=core.datetime.date(2020, 1, 10), + service_left=20, + item_left=21, + is_item_ok=True, + is_service_ok=True, + ) + self.assertEquals(expected, res) def test_eligibility_sp_call(self): mock_user = mock.Mock(is_anonymous=False) From 8c55b4f1daea2096a2c023ca1eabd852f22b1e8f Mon Sep 17 00:00:00 2001 From: Eric Darchis Date: Fri, 6 May 2022 15:25:39 +0200 Subject: [PATCH 2/7] Error when policy had no officer (FS) --- policy/services.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/policy/services.py b/policy/services.py index 1395985..22d8a1f 100644 --- a/policy/services.py +++ b/policy/services.py @@ -206,8 +206,8 @@ def _to_item(row): enroll_date=row.enroll_date, effective_date=row.effective_date, expiry_date=row.expiry_date, - officer_code=row.officer.code, - officer_name=row.officer.name(), + officer_code=row.officer.code if row.officer else None, + officer_name=row.officer.name() if row.officer else None, status=row.status, ded=row.total_ded_g, ded_in_patient=row.total_ded_ip, From a6cc02b3fdabc732f32c0b5804eb7360e39f7bf4 Mon Sep 17 00:00:00 2001 From: Eric Darchis Date: Thu, 11 Aug 2022 00:05:57 +0200 Subject: [PATCH 3/7] Unused import --- policy/services.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/policy/services.py b/policy/services.py index 22d8a1f..674d422 100644 --- a/policy/services.py +++ b/policy/services.py @@ -7,7 +7,7 @@ from django import dispatch from django.core.exceptions import PermissionDenied from django.db import connection -from django.db.models import Q, Count, Min, Max, Value +from django.db.models import Q, Count, Min, Max from django.db.models import Sum, F from django.db.models.functions import Coalesce from django.template import Template, Context From 037a5628de74d886217692adc291317802c50c30 Mon Sep 17 00:00:00 2001 From: Damian Borowiecki Date: Wed, 7 Sep 2022 11:18:44 +0200 Subject: [PATCH 4/7] OP-831: Remove calcrule signals from module --- policy/services.py | 41 ++++++++++++++++++++++++++++------------- policy/signals.py | 25 +------------------------ 2 files changed, 29 insertions(+), 37 deletions(-) diff --git a/policy/services.py b/policy/services.py index 674d422..0aeb55e 100644 --- a/policy/services.py +++ b/policy/services.py @@ -7,7 +7,7 @@ from django import dispatch from django.core.exceptions import PermissionDenied from django.db import connection -from django.db.models import Q, Count, Min, Max +from django.db.models import Q, Count, Min, Max, Value from django.db.models import Sum, F from django.db.models.functions import Coalesce from django.template import Template, Context @@ -41,24 +41,39 @@ def __init__(self, user): @register_service_signal('policy_service.create_or_update') def update_or_create(self, data, user): - if "client_mutation_id" in data: - data.pop('client_mutation_id') - if "client_mutation_label" in data: - data.pop('client_mutation_label') - policy_uuid = data.pop('policy_uuid') if 'policy_uuid' in data else None - # update_or_create(uuid=policy_uuid, ...) - # doesn't work because of explicit attempt to set null to uuid! + policy_uuid = data.get('policy_uuid', None) if policy_uuid: - policy = Policy.objects.get(uuid=policy_uuid) - policy.save_history() - reset_policy_before_update(policy) - [setattr(policy, key, data[key]) for key in data] + return self.update_policy(data, user) else: - policy = Policy.objects.create(**data) + return self.create_policy(data, user) + + @register_service_signal('policy_service.update') + def update_policy(self, data, user): + data = self._clean_mutation_info(data) + policy_uuid = data.pop('policy_uuid') if 'policy_uuid' in data else None + policy = Policy.objects.get(uuid=policy_uuid) + policy.save_history() + reset_policy_before_update(policy) + [setattr(policy, key, data[key]) for key in data] policy.save() update_insuree_policies(policy, user.id_for_audit) return policy + @register_service_signal('policy_service.create') + def create_policy(self, data, user): + data = self._clean_mutation_info(data) + policy = Policy.objects.create(**data) + policy.save() + update_insuree_policies(policy, user.id_for_audit) + return policy + + def _clean_mutation_info(self, data): + if "client_mutation_id" in data: + data.pop('client_mutation_id') + if "client_mutation_label" in data: + data.pop('client_mutation_label') + return data + def set_suspended(self, user, policy): try: policy.save_history() diff --git a/policy/signals.py b/policy/signals.py index 3b281ec..89955ac 100644 --- a/policy/signals.py +++ b/policy/signals.py @@ -1,27 +1,4 @@ -from calculation.services import run_calculation_rules -from core.models import User -from core.service_signals import ServiceSignalBindType -from core.signals import Signal, bind_service_signal -from policy.models import Policy +from core.signals import Signal _check_formal_sector_for_policy_signal_params = ["user", "policy_id"] signal_check_formal_sector_for_policy = Signal(providing_args=_check_formal_sector_for_policy_signal_params) - - -def bind_service_signals(): - bind_service_signal( - 'policy_service.create_or_update', - on_policy_create_or_update, - bind_type=ServiceSignalBindType.AFTER - ) - - -def on_policy_create_or_update(**kwargs): - policy = kwargs.get('result', None) - if policy: - if policy.status in [Policy.STATUS_IDLE, Policy.STATUS_ACTIVE]: - user = User.objects.filter(i_user__id=policy.audit_user_id).first() - # run calcrule for Invoice if there is valid rule - run_calculation_rules(policy, "PolicyCreatedInvoice", user) - # run calcrule for creating Bill if there is valid rule - run_calculation_rules(policy, "PolicyCreated", user) From ee9a26c173de620efc180987c0059b31b52d3d46 Mon Sep 17 00:00:00 2001 From: Eric Darchis Date: Fri, 23 Sep 2022 11:37:25 +0200 Subject: [PATCH 5/7] Django 3.1/Postgres --- policy/services.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/policy/services.py b/policy/services.py index 674d422..14f9a27 100644 --- a/policy/services.py +++ b/policy/services.py @@ -1,6 +1,6 @@ import logging from dataclasses import dataclass -from datetime import datetime as py_datetime +from datetime import datetime as py_datetime, date as py_date import core from claim.models import ClaimService, Claim, ClaimItem @@ -241,7 +241,7 @@ def build_query(self, req): res = res.filter(*core.filter_validity()) if req.active_or_last_expired_only: # sort on status, so that any active policy (status = 2) pops up... - res = res.annotate(not_null_expiry_date=Coalesce('expiry_date', py_datetime.max)) \ + res = res.annotate(not_null_expiry_date=Coalesce('expiry_date', py_date.max)) \ .annotate(not_null_validity_to=Coalesce('validity_to', py_datetime.max)) \ .order_by('product__code', 'status', '-not_null_expiry_date', '-not_null_validity_to', '-validity_from') return res From 439cfcb7347bad6f439dc658bb75c32f3ae5e159 Mon Sep 17 00:00:00 2001 From: Kamil Malinowski Date: Fri, 14 Oct 2022 09:16:53 +0200 Subject: [PATCH 6/7] OP-868 Fixed CI issues --- policy/test_services.py | 98 +++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 48 deletions(-) diff --git a/policy/test_services.py b/policy/test_services.py index 89a4812..f3e86d4 100644 --- a/policy/test_services.py +++ b/policy/test_services.py @@ -17,12 +17,13 @@ class EligibilityServiceTestCase(TestCase): def setUp(self) -> None: + super(EligibilityServiceTestCase, self).setUp() self.user = mock.Mock(is_anonymous=False) self.user.has_perms = mock.MagicMock(return_value=True) def test_eligibility_request_permission_denied(self): with mock.patch("django.db.backends.utils.CursorWrapper") as mock_cursor: - mock_cursor.return_value.__enter__.return_value.description = None + mock_cursor.return_value.description = None mock_user = mock.Mock(is_anonymous=False) mock_user.has_perms = mock.MagicMock(return_value=False) req = EligibilityRequest(chf_id="a") @@ -31,49 +32,51 @@ def test_eligibility_request_permission_denied(self): service.request(req) mock_user.has_perms.assert_called_with(PolicyConfig.gql_query_eligibilities_perms) - @mock.patch('django.db.connections') - def test_eligibility_request_all_good(self, mock_connections): - return_values = [ - list(range(1, 13)), - [ - core.datetime.date(2020, 1, 9), - core.datetime.date(2020, 1, 10), - 20, - 21, - True, - True, - ], - ][::-1] - mock_connections.__getitem__.return_value.cursor.return_value \ - .__enter__.return_value.fetchone = (lambda: return_values.pop()) - mock_user = mock.Mock(is_anonymous=False) - mock_user.has_perm = mock.MagicMock(return_value=True) - req = EligibilityRequest(chf_id="a") - service = StoredProcEligibilityService(mock_user) - res = service.request(req, EligibilityResponse(req)) - - expected = EligibilityResponse( - eligibility_request=req, - prod_id=1, - total_admissions_left=2, - total_visits_left=3, - total_consultations_left=4, - total_surgeries_left=5, - total_deliveries_left=6, - total_antenatal_left=7, - consultation_amount_left=8, - surgery_amount_left=9, - delivery_amount_left=10, - hospitalization_amount_left=11, - antenatal_amount_left=12, - min_date_service=core.datetime.date(2020, 1, 9), - min_date_item=core.datetime.date(2020, 1, 10), - service_left=20, - item_left=21, - is_item_ok=True, - is_service_ok=True, - ) - self.assertEquals(expected, res) + def test_eligibility_request_all_good(self): + with mock.patch("django.db.backends.utils.CursorWrapper") as mock_cursor: + return_values = [ + list(range(1, 13)), + [ + core.datetime.date(2020, 1, 9), + core.datetime.date(2020, 1, 10), + 20, + 21, + True, + True, + ], + ] + # required for all modules tests + mock_cursor.return_value.fetchone.side_effect = return_values + # required for policy module tests + mock_cursor.return_value.__enter__.return_value.fetchone.side_effect = return_values + mock_user = mock.Mock(is_anonymous=False) + mock_user.has_perm = mock.MagicMock(return_value=True) + req = EligibilityRequest(chf_id="a") + service = StoredProcEligibilityService(mock_user) + res = service.request(req, EligibilityResponse(req)) + + expected = EligibilityResponse( + eligibility_request=req, + prod_id=1, + total_admissions_left=2, + total_visits_left=3, + total_consultations_left=4, + total_surgeries_left=5, + total_deliveries_left=6, + total_antenatal_left=7, + consultation_amount_left=8, + surgery_amount_left=9, + delivery_amount_left=10, + hospitalization_amount_left=11, + antenatal_amount_left=12, + min_date_service=core.datetime.date(2020, 1, 9), + min_date_item=core.datetime.date(2020, 1, 10), + service_left=20, + item_left=21, + is_item_ok=True, + is_service_ok=True, + ) + self.assertEquals(expected, res) def test_eligibility_sp_call(self): mock_user = mock.Mock(is_anonymous=False) @@ -301,6 +304,7 @@ class RenewalsTestCase(TestCase): item_1 = None def setUp(self) -> None: + super(RenewalsTestCase, self).setUp() self.i_user = InteractiveUser( login_name="test_batch_run", audit_user_id=978911, id=97891 ) @@ -308,9 +312,6 @@ def setUp(self) -> None: self.item_1 = create_test_item("D") - def tearDown(self) -> None: - self.item_1.delete() - def test_insert_renewals(self): # Given from core import datetime, datetimedelta @@ -464,7 +465,8 @@ def test_insert_renewal_details(self): insuree_newpic = create_test_insuree( custom_props={"photo_date": datetime.datetime.now() - datetimedelta(days=30)}) - insuree_oldpic = create_test_insuree(custom_props={"photo_date": "2010-01-01", "chf_id": "CHFMARK"}) # 5 years by default + insuree_oldpic = create_test_insuree( + custom_props={"photo_date": "2010-01-01", "chf_id": "CHFMARK"}) # 5 years by default product = create_test_product("VISIT") officer = create_test_officer(custom_props={"phone": "+32444444444", "phone_communication": True}) photo_newpic = create_test_photo(insuree_newpic.id, officer.id) From 0454ba042edb76fbff3ac1cce0691d212cdc0d52 Mon Sep 17 00:00:00 2001 From: Eric Darchis Date: Thu, 3 Nov 2022 17:22:02 +0100 Subject: [PATCH 7/7] frontend and GQL type use uuid instead of policyUuid --- policy/services.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/policy/services.py b/policy/services.py index 18f784d..fbaf9e8 100644 --- a/policy/services.py +++ b/policy/services.py @@ -41,7 +41,7 @@ def __init__(self, user): @register_service_signal('policy_service.create_or_update') def update_or_create(self, data, user): - policy_uuid = data.get('policy_uuid', None) + policy_uuid = data.get('uuid', None) if policy_uuid: return self.update_policy(data, user) else: @@ -50,7 +50,7 @@ def update_or_create(self, data, user): @register_service_signal('policy_service.update') def update_policy(self, data, user): data = self._clean_mutation_info(data) - policy_uuid = data.pop('policy_uuid') if 'policy_uuid' in data else None + policy_uuid = data.pop('uuid') if 'uuid' in data else None policy = Policy.objects.get(uuid=policy_uuid) policy.save_history() reset_policy_before_update(policy)