Skip to content

Commit

Permalink
Merge pull request #77 from openimis/develop
Browse files Browse the repository at this point in the history
MERGING RELEASE branches
  • Loading branch information
hirensoni913 authored Nov 13, 2023
2 parents 668e3b3 + ebebc7a commit 3ca5716
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 23 deletions.
9 changes: 5 additions & 4 deletions policy/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,9 @@ def _to_item(row):

def build_query(self, req):
# TODO: prevent direct dependency on claim_ded structure?
res = Policy.objects \
.select_related('product') \
.select_related('officer') \
.prefetch_related('claim_ded_rems') \
res = Policy.objects\
.prefetch_related('product') \
.prefetch_related('officer') \
.annotate(total_ded_g=Sum('claim_ded_rems__ded_g')) \
.annotate(total_ded_ip=Sum('claim_ded_rems__ded_ip')) \
.annotate(total_ded_op=Sum('claim_ded_rems__ded_op')) \
Expand All @@ -284,6 +283,8 @@ def build_query(self, req):
.annotate(total_rem_delivery=Sum('claim_ded_rems__rem_delivery')) \
.annotate(total_rem_hospitalization=Sum('claim_ded_rems__rem_hospitalization')) \
.annotate(total_rem_antenatal=Sum('claim_ded_rems__rem_antenatal'))

res.query.group_by = ['id']
if not req.show_history:
res = res.filter(*core.filter_validity())
if req.active_or_last_expired_only:
Expand Down
40 changes: 22 additions & 18 deletions policy/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
from claim.test_helpers import create_test_claim, create_test_claimservice, create_test_claimitem
from claim.validations import validate_claim, validate_assign_prod_to_claimitems_and_services, process_dedrem
from core.models import InteractiveUser, User
from core.test_helpers import create_test_officer
from core.test_helpers import create_test_officer
from django.conf import settings
from django.test import TestCase
from insuree.test_helpers import create_test_photo
from medical.test_helpers import create_test_item, create_test_service
from medical_pricelist.test_helpers import add_service_to_hf_pricelist, add_item_to_hf_pricelist
from insuree.test_helpers import create_test_insuree
from policy.test_helpers import create_test_policy2, create_test_insuree_for_policy
from product.test_helpers import create_test_product, create_test_product_service, create_test_product_item

Expand Down Expand Up @@ -50,29 +51,32 @@ def test_eligibility_request_all_good(self):
# required for policy module tests
mock_cursor.return_value.__enter__.return_value.fetchone.side_effect = return_values
mock_user = mock.Mock(is_anonymous=False)
insuree, family = create_test_insuree_for_policy(custom_props={"chf_id": "tier1234" })
product = create_test_product("ELI1")
create_test_policy2(product, insuree)
mock_user.has_perm = mock.MagicMock(return_value=True)
req = EligibilityRequest(chf_id="a")
req = EligibilityRequest(chf_id="tier1234")
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,
prod_id=product.id,
total_admissions_left=None,
total_visits_left=None,
total_consultations_left=None,
total_surgeries_left=None,
total_deliveries_left=None,
total_antenatal_left=None,
consultation_amount_left=None,
surgery_amount_left=None,
delivery_amount_left=None,
hospitalization_amount_left=None,
antenatal_amount_left=None,
min_date_service=None,
min_date_item=None,
service_left=None,
item_left=None,
is_item_ok=True,
is_service_ok=True,
)
Expand Down
88 changes: 88 additions & 0 deletions policy/tests_gql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import base64
import json
from dataclasses import dataclass
from core.models import User
from core.test_helpers import create_test_interactive_user
from django.conf import settings
from graphene_django.utils.testing import GraphQLTestCase
from graphql_jwt.shortcuts import get_token
#credits https://docs.graphene-python.org/projects/django/en/latest/testing/


@dataclass
class DummyContext:
""" Just because we need a context to generate. """
user: User

class PolicyGraphQLTestCase(GraphQLTestCase):
GRAPHQL_URL = f'/{settings.SITE_ROOT()}graphql'
# This is required by some version of graphene but is never used. It should be set to the schema but the import
# is shown as an error in the IDE, so leaving it as True.
GRAPHQL_SCHEMA = True
admin_user = None

@classmethod
def setUpClass(cls):
super().setUpClass()
cls.admin_user = create_test_interactive_user(username="testLocationAdmin")
cls.admin_token = get_token(cls.admin_user, DummyContext(user=cls.admin_user))

def test_insuree_policy_query(self):

response = self.query(
'''
query {
policies(first: 10,orderBy: ["-enrollDate"])
{
totalCount
pageInfo { hasNextPage, hasPreviousPage, startCursor, endCursor}
edges
{
node
{
uuid,product{id,code,name,location{id}},officer{id,uuid,code,lastName,otherNames},family{id,uuid,headInsuree{id chfId uuid lastName otherNames},location{id,uuid,code,name,type,parent{id,uuid,code,name,type,parent{id,uuid,code,name,type,parent{id,uuid,code,name,type}}}}},enrollDate,effectiveDate,startDate,expiryDate,stage,status,value,sumPremiums,validityFrom,validityTo
}
}
}
}
''',
headers={"HTTP_AUTHORIZATION": f"Bearer {self.admin_token}"},
)

content = json.loads(response.content)

# This validates the status code and if you get errors
self.assertResponseNoErrors(response)

# Add some more asserts if you like
...

def test_query_with_variables(self):
response = self.query(
'''
query policiesByInsuree($chfid: String!, $targetDate: Date! ) {
policiesByInsuree(chfId:$chfid ,targetDate: $targetDate)
{
totalCount
pageInfo { hasNextPage, hasPreviousPage, startCursor, endCursor}
edges
{
node
{
policyUuid,productCode,productName,officerCode,officerName,enrollDate,effectiveDate,startDate,expiryDate,status,policyValue,balance,ded,dedInPatient,dedOutPatient,ceiling,ceilingInPatient,ceilingOutPatient
}
}
}
}
''',
headers={"HTTP_AUTHORIZATION": f"Bearer {self.admin_token}"},
variables={'chfid': "070707070", 'targetDate':"2019-01-01"}
)

content = json.loads(response.content)

# This validates the status code and if you get errors
self.assertResponseNoErrors(response)


2 changes: 1 addition & 1 deletion policy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ def get_queryset_valid_at_date(queryset, date):
validity_to__gte=date,
validity_from__lte=date
)
if filtered_qs.exists():
if len(filtered_qs)>0:
return filtered_qs
return queryset.filter(validity_from__date__lte=date, validity_to__isnull=True)

0 comments on commit 3ca5716

Please sign in to comment.