Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better caching #421

Merged
merged 19 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/database/services/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_configuration(self):
time.sleep(1)
configuration = Configuration(
forbidden_country_combinations=[ForbiddenCountryCombination(Country.CZE, Country.AUT)])
save_configuration_to_db(configuration, txm_event.db_id, 1)
save_configuration_to_db(configuration, txm_event, 1)
self.assertEqual(Country.CZE, configuration.forbidden_country_combinations[0].donor_country)
configuration = get_configuration_for_txm_event(txm_event)
self.assertEqual(Country.CZE, configuration.forbidden_country_combinations[0].donor_country)
Expand Down
5 changes: 4 additions & 1 deletion tests/database/services/test_file_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from txmatching.auth.exceptions import InvalidArgumentException
from txmatching.configuration.configuration import Configuration
from txmatching.database.db import db
from txmatching.database.services.patient_service import \
get_patients_persistent_hash
from txmatching.database.services.patient_upload_service import \
replace_or_add_patients_from_excel
from txmatching.database.services.txm_event_service import (
Expand Down Expand Up @@ -34,6 +36,7 @@ def test_saving_patients_from_obfuscated_excel(self):
config = ConfigModel(
txm_event_id=txm_event.db_id,
parameters={},
patients_hash=get_patients_persistent_hash(txm_event),
created_by=user_id
)

Expand Down Expand Up @@ -74,7 +77,7 @@ def test_saving_patients_from_obfuscated_excel(self):
)
for donor in txm_event.active_donors_dict.values()]

self.assertEqual(0, len(configs))
self.assertEqual(1, len(configs))
self.assertEqual(34, len(recipients))
self.assertEqual(38, len(donors))
self.assertEqual(3, len({donor.country for donor in donors}))
Expand Down
77 changes: 70 additions & 7 deletions tests/database/services/test_patient_service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from tests.test_utilities.create_dataclasses import (get_test_donors,
get_test_recipients)
from tests.test_utilities.populate_db import create_or_overwrite_txm_event
from tests.test_utilities.prepare_app import DbTests
from txmatching.data_transfer_objects.patients.upload_dtos.donor_upload_dto import \
Expand All @@ -9,17 +11,21 @@
from txmatching.data_transfer_objects.patients.upload_dtos.recipient_upload_dto import \
RecipientUploadDTO
from txmatching.database.db import db
from txmatching.database.services.patient_service import \
get_patients_persistent_hash
from txmatching.database.services.patient_upload_service import \
replace_or_add_patients_from_one_country
from txmatching.database.services.txm_event_service import get_txm_event
from txmatching.database.sql_alchemy_schema import ConfigModel
from txmatching.patients.patient import DonorType
from txmatching.patients.hla_model import HLAType
from txmatching.patients.patient import DonorType, TxmEvent
from txmatching.utils.blood_groups import BloodGroup
from txmatching.utils.enums import Country, Sex
from txmatching.utils.logged_user import get_current_user_id

TXM_EVENT_NAME = 'test'

DONORS = [
DONOR_UPLOAD_DTOS = [
DonorUploadDTO(
medical_id='D1',
blood_group=BloodGroup.A,
Expand Down Expand Up @@ -61,7 +67,7 @@
),
]

RECIPIENTS = [
RECIPIENT_UPLOAD_DTOS = [
RecipientUploadDTO(
acceptable_blood_groups=[
BloodGroup.A,
Expand Down Expand Up @@ -138,8 +144,8 @@
PATIENT_UPLOAD_DTO = PatientUploadDTOIn(
country=Country.CZE,
txm_event_name=TXM_EVENT_NAME,
donors=DONORS,
recipients=RECIPIENTS
donors=DONOR_UPLOAD_DTOS,
recipients=RECIPIENT_UPLOAD_DTOS
)


Expand All @@ -153,6 +159,7 @@ def test_update_txm_event_patients(self):
config = ConfigModel(
txm_event_id=txm_event.db_id,
parameters={},
patients_hash=get_patients_persistent_hash(txm_event),
created_by=user_id
)

Expand All @@ -163,6 +170,62 @@ def test_update_txm_event_patients(self):

replace_or_add_patients_from_one_country(PATIENT_UPLOAD_DTO)

# Validate that all configs of particular TXM event are deleted.
# Validate that configs of particular TXM event are not deleted.
configs = ConfigModel.query.filter(ConfigModel.txm_event_id == txm_event.db_id).all()
self.assertEqual(0, len(configs))
self.assertEqual(1, len(configs))

# Validate that patients hash has changed
txm_event_new = get_txm_event(txm_event.db_id)
self.assertNotEqual(config.patients_hash, get_patients_persistent_hash(txm_event_new))

def test_get_patients_hash(self):
txm_event_1 = TxmEvent(
1, 'event_name_1',
all_donors=get_test_donors(),
all_recipients=get_test_recipients()
)
hash_1 = get_patients_persistent_hash(txm_event_1)
self.assertEqual(hash_1, 1602329590066289936)

# changing event db id or event name does not change the hash
txm_event_2 = TxmEvent(
2, 'event_name_2',
all_donors=get_test_donors(),
all_recipients=get_test_recipients()
)
hash_2 = get_patients_persistent_hash(txm_event_2)
self.assertEqual(hash_1, hash_2)

# Changing donors changes the hash
txm_event_3 = TxmEvent(
1, 'event_name_1',
all_donors=[],
all_recipients=get_test_recipients()
)
hash_3 = get_patients_persistent_hash(txm_event_3)
self.assertNotEqual(hash_1, hash_3)

# Changing recipients changes the hash
txm_event_4 = TxmEvent(
1, 'event_name_1',
all_donors=get_test_donors(),
all_recipients=[]
)
hash_4 = get_patients_persistent_hash(txm_event_4)
self.assertNotEqual(hash_1, hash_4)

# changing hla type changes the hash
new_donors = get_test_donors()
self.assertEqual(
new_donors[0].parameters.hla_typing.hla_per_groups[0].hla_types[0],
HLAType('A1')
)
new_donors[0].parameters.hla_typing.hla_per_groups[0].hla_types[0] = HLAType('A3')

txm_event_5 = TxmEvent(
1, 'event_name_1',
all_donors=new_donors,
all_recipients=get_test_recipients()
)
hash_5 = get_patients_persistent_hash(txm_event_5)
self.assertNotEqual(hash_1, hash_5)
4 changes: 2 additions & 2 deletions tests/database/services/test_update_donor_recipient.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_update_recipient(self):
), txm_event_db_id)

configs = ConfigModel.query.filter(ConfigModel.txm_event_id == txm_event_db_id).all()
self.assertEqual(0, len(configs))
self.assertEqual(1, len(configs))

self.assertSetEqual({'AB'}, {blood.blood_type for blood in RecipientModel.query.get(1).acceptable_blood})
self.assertSetEqual({'B42', 'DQ6', 'DQA1'}, {code.code for code in RecipientModel.query.get(1).hla_antibodies})
Expand Down Expand Up @@ -83,7 +83,7 @@ def test_update_donor(self):
), txm_event_db_id)

configs = ConfigModel.query.filter(ConfigModel.txm_event_id == txm_event_db_id).all()
self.assertEqual(0, len(configs))
self.assertEqual(1, len(configs))

self.assertSetEqual({'A11', 'DQ6', 'DQA1'},
{hla_type['code'] for hla_type in DonorModel.query.get(1).hla_typing['hla_types_list']})
Expand Down
139 changes: 139 additions & 0 deletions tests/test_utilities/create_dataclasses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
from txmatching.patients.hla_model import (HLAAntibodies, HLAAntibody, HLAType,
HLATyping)
from txmatching.patients.patient import (Donor, DonorType, Recipient,
RecipientRequirements)
from txmatching.patients.patient_parameters import PatientParameters
from txmatching.utils.blood_groups import BloodGroup
from txmatching.utils.enums import Country, Sex

_RAW_CODES = [
'A1',
'A32',
'B7',
'B51',
'DR11',
'DR15'
]


def get_test_raw_codes():
return _RAW_CODES.copy()


def get_test_donors():
return [
Donor(
db_id=1,
medical_id='1',
parameters=PatientParameters(
blood_group=BloodGroup.A,
country_code=Country.CZE,
hla_typing=HLATyping(
hla_types_list=[
HLAType(raw_code=_RAW_CODES[0]),
HLAType(raw_code=_RAW_CODES[1]),
HLAType(raw_code='B44'),
HLAType(raw_code='DR10')
]
),
sex=Sex.M,
height=180,
weight=70,
year_of_birth=1985
),
related_recipient_db_id=None,
donor_type=DonorType.DONOR
),
Donor(
db_id=2,
medical_id='2',
parameters=PatientParameters(
blood_group=BloodGroup.A,
country_code=Country.CZE,
hla_typing=HLATyping(
hla_types_list=[
HLAType(raw_code=_RAW_CODES[1]),
HLAType(raw_code=_RAW_CODES[2]),
HLAType(raw_code='DR10')
]
),
sex=Sex.M,
height=180,
weight=70,
year_of_birth=1985
),
related_recipient_db_id=None,
donor_type=DonorType.DONOR
)
]


def get_test_recipients():
return [
Recipient(
db_id=3,
medical_id='3',
parameters=PatientParameters(
blood_group=BloodGroup.A,
country_code=Country.CZE,
hla_typing=HLATyping(
hla_types_list=[
HLAType(raw_code=_RAW_CODES[1]),
HLAType(raw_code=_RAW_CODES[2]),
HLAType(raw_code='DR1')
]
),
sex=Sex.M,
height=180,
weight=70,
year_of_birth=1985
),
related_donor_db_id=1,
acceptable_blood_groups=[],
recipient_cutoff=None,
hla_antibodies=HLAAntibodies([]),
recipient_requirements=RecipientRequirements(),
waiting_since=None,
previous_transplants=None
),
Recipient(
db_id=4,
medical_id='4',
parameters=PatientParameters(
blood_group=BloodGroup.A,
country_code=Country.CZE,
hla_typing=HLATyping(
hla_types_list=[
HLAType(raw_code='A3'),
HLAType(raw_code='B38'),
HLAType(raw_code=_RAW_CODES[4]),
HLAType(raw_code=_RAW_CODES[5]),
]
),
sex=Sex.M,
height=180,
weight=70,
year_of_birth=1985
),
related_donor_db_id=1,
acceptable_blood_groups=[],
recipient_cutoff=None,
hla_antibodies=HLAAntibodies([]),
recipient_requirements=RecipientRequirements(),
waiting_since=None,
previous_transplants=None
),
]


def get_test_antibodies():
return HLAAntibodies(
hla_antibodies_list=[
HLAAntibody('A7', 1200, 1000, 'A7'),
HLAAntibody('B32', 1200, 1000, 'B32'),
HLAAntibody('DR40', 1200, 1000, 'DR40'),
HLAAntibody('B5', 1200, 1000, 'B5'),
HLAAntibody('DR9', 1200, 1000, 'DR9'),
HLAAntibody('A23', 1200, 1000, 'A23')
]
)
Loading