From 5e769eab2b8146f9522237795b113647806d7a3c Mon Sep 17 00:00:00 2001 From: thiagotrabach Date: Mon, 4 Sep 2023 20:02:00 -0300 Subject: [PATCH] add new checks and tests --- fhir_utils/fhir/patient/resource.py | 47 ++++- .../fhir/resource/test_data/patient_data.json | 174 ++++++++++++++++ tests/unit/fhir/resource/test_patient.py | 189 ++++++++++++------ 3 files changed, 343 insertions(+), 67 deletions(-) create mode 100644 tests/unit/fhir/resource/test_data/patient_data.json diff --git a/fhir_utils/fhir/patient/resource.py b/fhir_utils/fhir/patient/resource.py index feaa7be..b9a3d22 100644 --- a/fhir_utils/fhir/patient/resource.py +++ b/fhir_utils/fhir/patient/resource.py @@ -65,6 +65,9 @@ def __post_init__(self): self.check_gender() self.check_address() self.check_telecom() + self.check_nationality + self.check_race() + self.check_ethnicity() # calculate register quality self.calculate_register_quality() @@ -121,14 +124,13 @@ def check_cns(self): return True def check_birth_country(self): - if self.birth_country not in ["B", "E", "N"]: + if not 2 <= len(keep_numeric_characters(self.birth_country)) <= 3: self._is_valid = False self._invalid_elements.append("birth_country") return False else: return True - def check_birth_date(self): is_valid = True if not is_valid_date_format(self.birth_date): @@ -214,6 +216,32 @@ def check_telecom(self): self._invalid_elements.append("telecom") return False + def check_race(self): + if self.race != "" and len(self.race) != 2: + self._is_valid = False + self._invalid_elements.append("race") + return False + else: + return True + + def check_ethnicity(self): + if self.ethnicity != "" and len(self.ethnicity) != 4: + self._is_valid = False + self._invalid_elements.append("ethnicity") + return False + else: + return True + + def check_nationality(self): + if self.nationality not in ["B", "E", "N"]: + self._is_valid = False + self._invalid_elements.append("nationality") + return False + else: + return True + + #def check_naturalization(self): + # TODO: check natualization def calculate_register_quality(self): counter = 0 @@ -222,13 +250,16 @@ def calculate_register_quality(self): "active", "address", "birth_city", "deceased", "nationality", "naturalization", "mother", "father", "protected_person", "race", "ethnicity","telecom"] + print(self._invalid_elements) for p in self.__dict__.items(): if p[0] in quality_properties: if p[0] in ["address", "telecom"]: if len(p[1]) > 0 and p[0] not in self._invalid_elements: counter += 1 + print(p[0]) elif p[1] != "" and p[1] != None and p[0] not in self._invalid_elements: counter += 1 + print(p[0]) self.register_quality = int(counter/len(quality_properties) * 100) @@ -326,24 +357,24 @@ def to_fhir(self): # mother if self.mother != "": - mother = {"extension": [{"url": "relationship", + mother = {"url": "http://www.saude.gov.br/fhir/r4/StructureDefinition/BRParentesIndividuo-1.0", + "extension": [{"url": "relationship", "valueCode": "mother"}, {"url": "parent", "valueHumanName": { "use": "official", - "text": self.mother}}], - "url": "http://www.saude.gov.br/fhir/r4/StructureDefinition/BRParentesIndividuo-1.0"} + "text": self.mother}}]} extension.append(mother) # father if self.father != "": - father = {"extension": [{"url": "relationship", + father = {"url": "http://www.saude.gov.br/fhir/r4/StructureDefinition/BRParentesIndividuo-1.0", + "extension": [{"url": "relationship", "valueCode": "father"}, {"url": "parent", "valueHumanName": { "use": "official", - "text": self.father}}], - "url": "http://www.saude.gov.br/fhir/r4/StructureDefinition/BRParentesIndividuo-1.0"} + "text": self.father}}]} extension.append(father) # race and ethnicity diff --git a/tests/unit/fhir/resource/test_data/patient_data.json b/tests/unit/fhir/resource/test_data/patient_data.json new file mode 100644 index 0000000..34bd6f3 --- /dev/null +++ b/tests/unit/fhir/resource/test_data/patient_data.json @@ -0,0 +1,174 @@ +{ + "identifier": [ + { + "use": "official", + "type": { + "coding": [ + { + "system": "http://terminology.hl7.org/CodeSystem/v2-0203", + "code": "TAX" + } + ] + }, + "system": "https://rnds-fhir.saude.gov.br/NamingSystem/cpf", + "value": "87877669810" + }, + { + "use": "official", + "type": { + "coding": [ + { + "system": "http://terminology.hl7.org/CodeSystem/v2-0203", + "code": "HC" + } + ] + }, + "system": "https://rnds-fhir.saude.gov.br/NamingSystem/cns", + "value": "123456789012345" + } + ], + "active": true, + "name": { + "use": "official", + "text": "John Doe" + }, + "telecom": [ + { + "system": "phone", + "value": "5521123456789", + "use": "home" + } + ], + "gender": "male", + "birthDate": "2000-01-01", + "deceasedBoolean": false, + "address": [ + { + "use": "home", + "type": "postal", + "line": [ + "081", + "SQN BLOCO M", + "604", + "APARTAMENTO", + "ASA NORTE" + ], + "city": "315780", + "state": "53", + "postalCode": "70752130" + } + ], + "extension": [ + { + "url": "http://www.saude.gov.br/fhir/r4/StructureDefinition/BRQualidadeCadastroIndividuo-1.0", + "valuePositiveInt": 100 + }, + { + "url": "http://www.saude.gov.br/fhir/r4/StructureDefinition/BRParentesIndividuo-1.0", + "extension": [ + { + "url": "relationship", + "valueCode": "mother" + }, + { + "url": "parent", + "valueHumanName": { + "use": "official", + "text": "Jane Doe" + } + } + ] + }, + { + "url": "http://www.saude.gov.br/fhir/r4/StructureDefinition/BRParentesIndividuo-1.0", + "extension": [ + { + "url": "relationship", + "valueCode": "father" + }, + { + "url": "parent", + "valueHumanName": { + "use": "official", + "text": "John Doe Sr" + } + } + ] + }, + { + "url": "http://www.saude.gov.br/fhir/r4/StructureDefinition/BRRacaCorEtnia-1.0", + "extension": [ + { + "url": "race", + "valueCodeableConcept": { + "coding": [ + { + "system": "http://www.saude.gov.br/fhir/r4/CodeSystem/BRRacaCor-1.0", + "code": "05" + } + ] + } + }, + { + "url": "indigenousEthnicity", + "valueCodeableConcept": { + "coding": [ + { + "system": "http://www.saude.gov.br/fhir/r4/CodeSystem/BREtniaIndigena-1.0", + "code": "X405" + } + ] + } + } + ] + }, + { + "url": "http://www.saude.gov.br/fhir/r4/StructureDefinition/BRMunicipio-1.0", + "valueCodeableConcept": { + "coding": [ + { + "system": "https://rnds-fhir.saude.gov.br/CodeSystem/BRMunicipio-1.0", + "code": "315780" + } + ] + } + }, + { + "url": "http://www.saude.gov.br/fhir/r4/StructureDefinition/BRPais-1.0", + "valueCodeableConcept": { + "coding": [ + { + "system": "https://rnds-fhir.saude.gov.br/CodeSystem/BRPais-1.0", + "code": "10" + } + ] + } + }, + { + "url": "http://www.saude.gov.br/fhir/r4/StructureDefinition/BRNacionalidade", + "valueCodeableConcept": { + "coding": [ + { + "system": "https://rnds-fhir.saude.gov.br/CodeSystem/BRNacionalidade", + "code": "10" + } + ] + } + }, + { + "url": "http://www.saude.gov.br/fhir/r4/StructureDefinition/BRNaturalizacao-1.0", + "valueCodeableConcept": { + "coding": [ + { + "system": "https://rnds-fhir.saude.gov.br/CodeSystem/BRNaturalizacao-1.0", + "code": "10" + } + ] + } + }, + { + "url": "http://www.saude.gov.br/fhir/r4/StructureDefinition/BRIndividuoProtegido-1.0", + "valueBoolean": false + } + ] + } \ No newline at end of file diff --git a/tests/unit/fhir/resource/test_patient.py b/tests/unit/fhir/resource/test_patient.py index a38ffcb..a3a4aa4 100644 --- a/tests/unit/fhir/resource/test_patient.py +++ b/tests/unit/fhir/resource/test_patient.py @@ -1,121 +1,124 @@ import pytest from datetime import datetime from fhir_utils.fhir.patient.resource import Patient -from dataclasses import asdict +from fhir_utils.utils import json_to_dict +import os def test_format_cpf(): - patient = Patient(source="smsrio", name="John Doe", cpf="123.456.789-00", gender="male", birth_date="1990-01-01", birth_country="B") + patient = Patient(source="smsrio", name="John Doe", cpf="123.456.789-00", gender="male", birth_date="1990-01-01", birth_country="10") patient.format_cpf() assert patient.cpf == "12345678900" def test_format_cns(): - patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="B", cns="1234567890123-45") + patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="10", cns="1234567890123-45") patient.format_cns() assert patient.cns == "123456789012345" def test_format_name(): - patient = Patient(source="smsrio", name=" john doe5 ", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="B") + patient = Patient(source="smsrio", name=" john doe5 ", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="10") formatted_name = patient.format_name(patient.name) assert formatted_name == "John Doe" def test_format_cep(): - patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="B", address=[{"postalCode": "12345-678"}]) + patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="10", address=[{"postalCode": "12345-678"}]) patient.format_cep() assert patient.address[0]["postalCode"] == "12345678" def test_format_phone(): # clean numeric chars for phone - patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="B", telecom=[{"system": "phone", "value": "+55 (21) 3456-7890"}]) + patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="10", telecom=[{"system": "phone", "value": "+55 (21) 3456-7890"}]) assert patient.telecom[0]["value"] == "552134567890" # complement phone with country and state code (rio de janeiro) - patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="B", telecom=[{"system": "phone", "value": "3456-7890"}]) + patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="10", telecom=[{"system": "phone", "value": "3456-7890"}]) assert patient.telecom[0]["value"] == "552134567890" - patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="B", telecom=[{"system": "phone", "value": "93456-7890"}]) + patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="10", telecom=[{"system": "phone", "value": "93456-7890"}]) assert patient.telecom[0]["value"] == "5521934567890" # complement phone with country - patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="B", telecom=[{"system": "phone", "value": "213456-7890"}]) + patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="10", telecom=[{"system": "phone", "value": "213456-7890"}]) assert patient.telecom[0]["value"] == "552134567890" - patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="B", telecom=[{"system": "phone", "value": "2193456-7890"}]) + patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="10", telecom=[{"system": "phone", "value": "2193456-7890"}]) assert patient.telecom[0]["value"] == "5521934567890" # complement phone with country removing zero of state code - patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="B", telecom=[{"system": "phone", "value": "0213456-7890"}]) + patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="10", telecom=[{"system": "phone", "value": "0213456-7890"}]) assert patient.telecom[0]["value"] == "552134567890" - patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="B", telecom=[{"system": "phone", "value": "02193456-7890"}]) + patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="10", telecom=[{"system": "phone", "value": "02193456-7890"}]) assert patient.telecom[0]["value"] == "5521934567890" # multiple phones - patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="B", telecom=[{"system": "phone", "value": "+55 (21) 3456-7890"}, + patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="10", telecom=[{"system": "phone", "value": "+55 (21) 3456-7890"}, {"system": "phone", "value": "+55 (21) 93456-7890"}]) assert patient.telecom[0]["value"] == "552134567890" and patient.telecom[1]["value"] == "5521934567890" def test_check_cpf(): - patient = Patient(source="smsrio", name="John Doe", cpf="878.776.698-10", gender="male", birth_date="1990-01-01", birth_country="B") + patient = Patient(source="smsrio", name="John Doe", cpf="878.776.698-10", gender="male", birth_date="1990-01-01", birth_country="10") assert patient.check_cpf() == True - patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="B") + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="10") patient.check_cpf() 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") + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", cns="213321999239456", gender="male", birth_date="1990-01-01", birth_country="10") 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") + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", cns="213321999", gender="male", birth_date="1990-01-01", birth_country="10") 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") + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="10") 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") + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="10") 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") + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="01-01-1990", birth_country="10") 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") + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="2100-01-01", birth_country="10") 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") + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="10") assert patient.check_gender() == True - patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="m", birth_date="01-01-1990", birth_country="B") + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="m", birth_date="01-01-1990", birth_country="10") 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", + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="10", address=[{"use": "home", "type": "both", - "line": ["081", - "SQN BLOCO M", - "604", - "APARTAMENTO", - "ASA NORTE"], + "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", + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="10", address=[{"use": "home", "type": "both", - "line": ["RUA", - "SQN BLOCO M", - "604"], + "line": [ + "RUA", + "SQN BLOCO M", + "604"], "city": "Rio de Janeiro", "state": "RJ", "postalCode": "7075230"}]) @@ -123,89 +126,157 @@ def test_check_address(): def test_check_address(): - patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="B", + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="10", address=[{"use": "home", "type": "both", - "line": ["081", - "SQN BLOCO M", - "604", - "APARTAMENTO", - "ASA NORTE"], + "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", + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="10", address=[{"use": "home", "type": "both", - "line": ["RUA", - "SQN BLOCO M", - "604"], + "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", + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="10", telecom=[{"system": "phone", "value": "552134567890", "use": "home"}] ) assert patient.check_telecom() == True - patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="B", + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="10", telecom=[{"system": "phone", "value": "552134567890"}] ) assert patient.check_telecom() == False and "telecom" in patient._invalid_elements - patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="B", + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="10", telecom=[{"system": "email", "value": "abc.com", "use": "home"}] ) assert patient.check_telecom() == False and "telecom" in patient._invalid_elements +def test_check_nationality(): + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="10", nationality="B") + assert patient.check_nationality() == True + + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="10", nationality="brazilian") + assert patient.check_nationality() == False and "nationality" in patient._invalid_elements + +def test_check_race(): + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="10", race="05") + assert patient.check_race() == True + + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="10", race="white") + assert patient.check_race() == False and "race" in patient._invalid_elements + +def test_check_ethnicity(): + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="10", ethnicity="X405") + assert patient.check_ethnicity() == True + + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="10", ethnicity="non-indigenous") + assert patient.check_ethnicity() == False and "ethnicity" in patient._invalid_elements + + def test_calculate_register_quality(): - patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="B", deceased=False, active=True) + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="10", deceased=False, active=True) assert patient.register_quality == 33 - patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="B", deceased=False, active=True, + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="10", deceased=False, active=True, telecom=[{"system": "phone", "value": "552134567890", "use": "home"}]) assert patient.register_quality == 38 - patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="B", deceased=False, active=True, + patient = Patient(source="smsrio", name="John Doe", cpf="1234567890", gender="male", birth_date="1990-01-01", birth_country="10", deceased=False, active=True, telecom=[{"system": "", "value": "552134567890", "use": "home"}]) assert patient.register_quality == 33 def test_compare(): - patient1 = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="B") - patient2 = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="B") + patient1 = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="10") + patient2 = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="10") assert patient1.compare(patient2) == {} - patient1 = Patient(source="smsrio", name="John", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="B") - patient2 = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="B") + patient1 = Patient(source="smsrio", name="John", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="10") + patient2 = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="10") assert patient1.compare(patient2) == {"values_changed": {"root.name": {"new_value": "John Doe", "old_value": "John"}}} def test_merge(): # valid resource - patient = Patient(source="smsrio", name="John Doe", cpf="878.776.698-10", gender="male", birth_date="1990-01-01", birth_country="B", active= True, father = "Caetano") - patient_new = Patient(source="smsrio", name="John Doe", cpf="878.776.698-10", gender="male", birth_date="1990-01-01", birth_country="B", active= False, mother = "Rita") + patient = Patient(source="smsrio", name="John Doe", cpf="878.776.698-10", gender="male", birth_date="1990-01-01", birth_country="10", active= True, father = "Caetano") + patient_new = Patient(source="smsrio", name="John Doe", cpf="878.776.698-10", gender="male", birth_date="1990-01-01", birth_country="10", active= False, mother = "Rita") - expected_merge = Patient(source="smsrio", name="John Doe", cpf="878.776.698-10", gender="male", birth_date="1990-01-01", birth_country="B", active= False, father= "Caetano", mother = "Rita") + expected_merge = Patient(source="smsrio", name="John Doe", cpf="878.776.698-10", gender="male", birth_date="1990-01-01", birth_country="10", active= False, father= "Caetano", mother = "Rita") merged_patient = patient.merge(patient_new, force_invalid_merge=False) assert merged_patient == expected_merge # invalid resource - patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="B", active= True, father = "Caetano") - patient_new = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="B", active= False, mother = "Rita") + patient = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="10", active= True, father = "Caetano") + patient_new = Patient(source="smsrio", name="John Doe", cpf="12345678900", gender="male", birth_date="1990-01-01", birth_country="10", active= False, mother = "Rita") with pytest.raises(ValueError) as excinfo: patient.merge(patient_new, force_invalid_merge=False) - assert str(excinfo.value) == "Can't merge invalid resource" \ No newline at end of file + assert str(excinfo.value) == "Can't merge invalid resource" + +def test_to_fhir(): + # Create an instance of the class with all properties + patient = Patient( + source="smsrio", + name="John Doe", + cpf="878.776.698-10", + gender="male", + birth_date="2000-01-01", + birth_country="10", + cns="123456789012345", + active=True, + address=[{ + "use": "home", + "type": "postal", + "line": ["081", "SQN BLOCO M", "604", "APARTAMENTO", "ASA NORTE"], + "city": "315780", + "state": "53", + "postalCode": "70752130" + }], + birth_city="315780", + deceased=False, + nationality="10", + naturalization="10", + mother="Jane Doe", + father="John Doe Sr.", + protected_person=False, + race="05", + ethnicity="X405", + telecom=[{ + "system": "phone", + "value": "5521123456789", + "use": "home" + }] + ) + + # Call the to_fhir method + fhir_patient = patient.to_fhir() + + # Load expected output from a JSON file + expected_output = json_to_dict(os.path.join(os.path.dirname(__file__), 'test_data', 'patient_data.json')) + + # Compare the output of the to_fhir method with the expected output + assert fhir_patient == expected_output \ No newline at end of file