From 018dbe3206579142428746d63955e40b1dd4628e Mon Sep 17 00:00:00 2001 From: thiagotrabach Date: Sun, 3 Sep 2023 12:24:52 -0300 Subject: [PATCH] wip test check telecom --- fhir_utils/fhir/patient/resource.py | 48 ++++++------ tests/unit/fhir/resource/test_patient.py | 94 +++++++++++++++++++++++- 2 files changed, 117 insertions(+), 25 deletions(-) diff --git a/fhir_utils/fhir/patient/resource.py b/fhir_utils/fhir/patient/resource.py index 8a8ad76..1ed4efa 100644 --- a/fhir_utils/fhir/patient/resource.py +++ b/fhir_utils/fhir/patient/resource.py @@ -81,21 +81,20 @@ def format_name(self, name): def format_cep(self): if type(self.address) is list: - for i, _ in enumerate(self.address): + for i, address in enumerate(self.address): try: - self.address[i]["postalCode"] = keep_numeric_characters(self.address[i]["postalCode"]) + self.address[i]["postalCode"] = keep_numeric_characters(address["postalCode"]) except: pass def format_phone(self): # We assume that any phone without state code state are from Rio de Janeiro city if type(self.telecom) is list: - for i, _ in enumerate(self.telecom): - if self.telecom[i]["system"] == "phone": - phone = keep_numeric_characters(self.telecom[i]["value"]) + for i, telecom in enumerate(self.telecom): + if telecom["system"] == "phone": + phone = keep_numeric_characters(telecom["value"]) if phone[0] == "0": # zero on the left for state code phone = phone[1:] - print(phone) if 8 <= len(phone) <= 9: phone = f"5521{phone}" elif 10 <= len(phone) <= 11: @@ -159,22 +158,22 @@ def check_address(self): keys = ["use", "type", "line", "city", "state", "postalCode"] - for i, _ in enumerate(self.address): + for i, address in enumerate(self.address): # if all must have keys are present - if not all(key in self.address[i] for key in keys): + if not all(key in address for key in keys): is_valid = False # accepted values - elif self.address[i]["use"] not in ["home", "work", "temp", "old", "billing"]: + elif address["use"] not in ["home", "work", "temp", "old", "billing"]: is_valid = False - elif self.address[i]["type"] not in ["postal", "physical", "both"]: + elif address["type"] not in ["postal", "physical", "both"]: is_valid = False - elif not 4 <= len(self.address[i]["line"]) <= 5 or self.address[i]["line"][0] not in ["008", "081"]: + elif not 4 <= len(address["line"]) <= 5 or address["line"][0] not in ["008", "081"]: is_valid = False - elif len(keep_numeric_characters(self.address[i]["city"])) != 6: + elif len(keep_numeric_characters(address["city"])) != 6: is_valid = False - elif len(keep_numeric_characters(self.address[i]["state"])) != 2: + elif len(keep_numeric_characters(address["state"])) != 2: is_valid = False - elif len(keep_numeric_characters(self.address[i]["postalCode"])) != 8: + elif len(keep_numeric_characters(address["postalCode"])) != 8: is_valid = False if is_valid: @@ -187,21 +186,24 @@ def check_address(self): def check_telecom(self): # TODO: raise type error execption if not a list is_valid = True - + print(is_valid) if type(self.telecom) is list: keys = ["system", "value", "use"] - - for i, _ in enumerate(self.telecom): + + for i, telecom in enumerate(self.telecom): + print(telecom, i) # if all must have keys are present - if not all(key in self.telecom[i] for key in keys): + if not all(key in telecom for key in keys): is_valid = False # accepted values - elif self.telecom[i]["system"] not in ["phone", "fax", "email", "pager", "url", "sms", "other"]: + elif telecom["system"] not in ["phone", "fax", "email", "pager", "url", "sms", "other"]: + is_valid = False + elif telecom["use"] not in ["home", "work", "temp", "old", "mobile"]: is_valid = False - elif self.telecom[i]["system"] == "phone" and not 12 <= len(keep_alpha_characters(self.telecom[i]["value"])) <=13: # ddi+ddd+phone + elif telecom["system"] == "phone" and not 12 <= len(keep_alpha_characters(telecom["value"])) <=13: # ddi+ddd+phone is_valid = False - elif not re.match(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$", self.telecom[i]["value"]): + elif telecom["system"] == "email" and not re.match(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$", telecom["value"]): is_valid = False if is_valid: @@ -210,9 +212,7 @@ def check_telecom(self): self._is_valid = False self._invalid_elements.append("telecom") return False - - else: - raise TypeError("Telecom must be a list of dicts") + def calculate_register_quality(self): diff --git a/tests/unit/fhir/resource/test_patient.py b/tests/unit/fhir/resource/test_patient.py index 32facc6..e3bcd20 100644 --- a/tests/unit/fhir/resource/test_patient.py +++ b/tests/unit/fhir/resource/test_patient.py @@ -61,4 +61,96 @@ def test_check_cpf(): patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="B") patient.check_cpf() - assert patient.check_cpf() == False + assert patient.check_cpf() == False and "cpf" in patient._invalid_elements + +def test_check_cns(): + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", cns="213321999239456", gender="male", birth_date="1990-01-01", birth_country="B") + assert patient.check_cns() == True + + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", cns="213321999", gender="male", birth_date="1990-01-01", birth_country="B") + assert patient.check_cns() == False and "cns" in patient._invalid_elements + +def test_check_birth_country(): + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="B") + assert patient.check_birth_country() == True + + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="Brasil") + assert patient.check_birth_country() == False and "birth_country" in patient._invalid_elements + +def test_check_birth_date(): + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="B") + assert patient.check_birth_date() == True + + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="01-01-1990", birth_country="B") + assert patient.check_birth_date() == False and "birth_date" in patient._invalid_elements + + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="2100-01-01", birth_country="B") + assert patient.check_birth_date() == False and "birth_date" in patient._invalid_elements + +def test_check_gender(): + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="B") + assert patient.check_gender() == True + + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="m", birth_date="01-01-1990", birth_country="B") + assert patient.check_gender() == False and "gender" in patient._invalid_elements + +def test_check_address(): + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="B", + address=[{"use": "home", + "type": "both", + "line": ["081", + "SQN BLOCO M", + "604", + "APARTAMENTO", + "ASA NORTE"], + "city": "315780", + "state": "53", + "postalCode": "70752130"}]) + + assert patient.check_address() == True + + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="B", + address=[{"use": "home", + "type": "both", + "line": ["RUA", + "SQN BLOCO M", + "604"], + "city": "Rio de Janeiro", + "state": "RJ", + "postalCode": "7075230"}]) + assert patient.check_address() == False and "address" in patient._invalid_elements + + +def test_check_address(): + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="B", + address=[{"use": "home", + "type": "both", + "line": ["081", + "SQN BLOCO M", + "604", + "APARTAMENTO", + "ASA NORTE"], + "city": "315780", + "state": "53", + "postalCode": "70752130"}]) + + assert patient.check_address() == True + + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="B", + address=[{"use": "home", + "type": "both", + "line": ["RUA", + "SQN BLOCO M", + "604"], + "city": "Rio de Janeiro", + "state": "RJ", + "postalCode": "7075230"}]) + assert patient.check_address() == False and "address" in patient._invalid_elements + +def test_check_telecom(): + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="B", + telecom=[{"system": "phone", + "value": "552134567890", + "use": "home"}] ) + assert patient.check_telecom() == True +