From 4ea07ca500ab681e1cbc843160ba885af58f407c Mon Sep 17 00:00:00 2001 From: Pedro Nascimento Date: Thu, 11 Jan 2024 13:26:09 -0300 Subject: [PATCH] test: implementing basic tests to raw and std routers --- api/app/models.py | 6 +- api/tests/conftest.py | 40 ++++++++- api/tests/{test_main.py => test_mrg.py} | 7 +- api/tests/test_raw.py | 72 +++++++++++++++ api/tests/test_std.py | 112 ++++++++++++++++++++++++ 5 files changed, 225 insertions(+), 12 deletions(-) rename api/tests/{test_main.py => test_mrg.py} (86%) create mode 100644 api/tests/test_raw.py create mode 100644 api/tests/test_std.py diff --git a/api/app/models.py b/api/app/models.py index 794c9d7..9d37a3c 100644 --- a/api/app/models.py +++ b/api/app/models.py @@ -112,19 +112,19 @@ class State(Model): class Gender(Model): id = fields.UUIDField(pk=True) - slug = fields.CharField(max_length=32, unique=True) + slug = fields.CharEnumField(enum_type=GenderEnum, unique=True) name = fields.CharField(max_length=512) class Race(Model): id = fields.UUIDField(pk=True) - slug = fields.CharField(max_length=32, unique=True) + slug = fields.CharEnumField(enum_type=RaceEnum, unique=True) name = fields.CharField(max_length=512) class Nationality(Model): id = fields.UUIDField(pk=True) - slug = fields.CharField(max_length=32, unique=True) + slug = fields.CharEnumField(enum_type=NationalityEnum, unique=True) name = fields.CharField(max_length=512) diff --git a/api/tests/conftest.py b/api/tests/conftest.py index 3a6181a..1dc6bd4 100644 --- a/api/tests/conftest.py +++ b/api/tests/conftest.py @@ -8,7 +8,8 @@ from app.db import TORTOISE_ORM from app.main import app from app.models import City, Country, DataSource, State, User, Gender, Patient, \ - ConditionCode, PatientCondition, Cns, Race, Nationality, Address, Telecom + ConditionCode, PatientCondition, Cns, Race, Nationality, Address, Telecom, \ + RawPatientRecord, RawPatientCondition from app.utils import password_hash @@ -34,7 +35,7 @@ async def client(): @pytest.fixture(scope="session", autouse=True) -async def initialize_tests(): +async def initialize_tests(patient_cpf: str): await Tortoise.init(config=TORTOISE_ORM) await Tortoise.generate_schemas() @@ -59,7 +60,7 @@ async def initialize_tests(): city = await City.create(name="Rio de Janeiro", state=state, code="00001") gender = await Gender.create(slug="male", name="male") race = await Race.create(slug="parda", name="parda") - nationality = await Nationality.create(slug="b", name="B") + nationality = await Nationality.create(slug="B", name="B") await User.create( username="pedro", @@ -71,7 +72,7 @@ async def initialize_tests(): ) patient = await Patient.create( name="Pedro", - patient_cpf="11111111111", + patient_cpf=patient_cpf, birth_date="2021-01-01", active=True, protected_person=False, @@ -136,3 +137,34 @@ async def email(): @pytest.fixture(scope="session") async def password(): yield "senha" + +@pytest.fixture(scope="session") +async def patient_cpf(): + yield "1111111111" + +@pytest.fixture(scope="session") +async def token(client: AsyncClient, username: str, password: str): + response = await client.post( + "/auth/token", + headers={"content-type": "application/x-www-form-urlencoded"}, + data={"username": username, "password": password}, + ) + yield response.json().get("access_token") + +@pytest.fixture(scope="session") +async def patientrecord_raw_source(): + await Tortoise.init(config=TORTOISE_ORM) + await Tortoise.generate_schemas() + + raw_patientrecord = await RawPatientRecord.first() + + yield str(raw_patientrecord.id) + +@pytest.fixture(scope="session") +async def patientcondition_raw_source(): + await Tortoise.init(config=TORTOISE_ORM) + await Tortoise.generate_schemas() + + raw_patientcondition = await RawPatientCondition.first() + + yield str(raw_patientcondition.id) diff --git a/api/tests/test_main.py b/api/tests/test_mrg.py similarity index 86% rename from api/tests/test_main.py rename to api/tests/test_mrg.py index 81a991c..c9a286c 100644 --- a/api/tests/test_main.py +++ b/api/tests/test_mrg.py @@ -28,12 +28,9 @@ async def test_auth(client: AsyncClient, username: str, password: str): @pytest.mark.anyio @pytest.mark.run(order=2) -async def test_get_patient(client: AsyncClient, username: str, password: str): - token = await test_auth(client, username, password) - - test_cpf = "11111111111" +async def test_get_patient(client: AsyncClient, token: str, patient_cpf : str): response = await client.get( - f"/mrg/patient/{test_cpf}", + f"/mrg/patient/{patient_cpf}", headers={"Authorization": f"Bearer {token}"} ) diff --git a/api/tests/test_raw.py b/api/tests/test_raw.py new file mode 100644 index 0000000..190ceaf --- /dev/null +++ b/api/tests/test_raw.py @@ -0,0 +1,72 @@ +# -*- coding: utf-8 -*- +import sys + +sys.path.insert(0, "../") + +import pytest # noqa +from httpx import AsyncClient # noqa + + +@pytest.mark.anyio +@pytest.mark.run(order=1) +async def test_post_rawpatientrecord(client: AsyncClient, token: str, patient_cpf: str): + response = await client.post( + f"/raw/patientrecord", + headers={"Authorization": f"Bearer {token}"}, + json={ + "patient_cpf": patient_cpf, + "data": { + "name" : "Teste", + "address": "Rua 1, 3000, 22222222, Rio de Janeiro, RJ, Brasil" + } + } + ) + + assert response.status_code == 201 + assert 'id' in response.json() + + +@pytest.mark.anyio +@pytest.mark.run(order=2) +async def test_get_rawpatientrecord(client: AsyncClient, token: str): + response = await client.get( + f"/raw/patientrecord", + headers={"Authorization": f"Bearer {token}"} + ) + + assert response.status_code == 200 + + json_response = response.json() + assert len(json_response) > 0 + + +@pytest.mark.anyio +@pytest.mark.run(order=1) +async def test_post_rawpatientcondition(client: AsyncClient, token: str, patient_cpf: str): + response = await client.post( + f"/raw/patientcondition", + headers={"Authorization": f"Bearer {token}"}, + json={ + "patient_cpf": patient_cpf, + "data": { + "code" : "A001", + "status": "resolved" + } + } + ) + + assert response.status_code == 201 + assert 'id' in response.json() + +@pytest.mark.anyio +@pytest.mark.run(order=2) +async def test_get_rawpatientcondition(client: AsyncClient, token: str): + response = await client.get( + f"/raw/patientcondition", + headers={"Authorization": f"Bearer {token}"} + ) + + assert response.status_code == 200 + + json_response = response.json() + assert len(json_response) > 0 \ No newline at end of file diff --git a/api/tests/test_std.py b/api/tests/test_std.py new file mode 100644 index 0000000..76f9612 --- /dev/null +++ b/api/tests/test_std.py @@ -0,0 +1,112 @@ +# -*- coding: utf-8 -*- +import sys + +sys.path.insert(0, "../") + +import pytest # noqa +from httpx import AsyncClient # noqa + + +@pytest.mark.anyio +@pytest.mark.run(order=10) +async def test_post_stdpatientrecord(client: AsyncClient, token: str, patient_cpf : str, patientrecord_raw_source: str): + response = await client.post( + f"/std/patientrecord", + headers={"Authorization": f"Bearer {token}"}, + json={ + "active": True, + "birth_city": "Rio de Janeiro", + "birth_state": "Rio de Janeiro", + "birth_country": "Brasil", + "birth_date": "2000-01-11", + "patient_cpf": patient_cpf, + "deceased": False, + "deceased_date": "2024-01-11", + "father_name": "João Cardoso Farias", + "gender": "male", + "mother_name": "Gabriela Marques da Cunha", + "name": "Fernando Marques Farias", + "nationality": "B", + "naturalization": "n", + "protected_person": False, + "race": "parda", + "cns_list": [ + { + "value": "1171777717", + "is_main": True + } + ], + "address_list": [ + { + "use": "string", + "type": "work", + "line": "Rua dos Bobos, 0", + "city": "00001", + "country": "00001", + "state": "00001", + "postal_code": "22222222", + "start": "2010-10-02", + "end": "2013-07-11" + } + ], + "telecom_list": [ + { + "system": "phone", + "use": "home", + "value": "32323232", + "rank": 1, + "start": "2010-10-02" + } + ], + "raw_source_id": patientrecord_raw_source + } + ) + + assert response.status_code == 201 + assert 'id' in response.json() + +@pytest.mark.anyio +@pytest.mark.run(order=11) +async def test_get_stdpatientrecord(client: AsyncClient, token: str): + response = await client.get( + f"/raw/patientrecord", + headers={"Authorization": f"Bearer {token}"} + ) + + assert response.status_code == 200 + + json_response = response.json() + assert len(json_response) > 0 + +@pytest.mark.anyio +@pytest.mark.run(order=10) +async def test_post_stdpatientcondition(client: AsyncClient, token: str, patient_cpf : str, patientcondition_raw_source: str): + response = await client.post( + f"/std/patientcondition", + headers={"Authorization": f"Bearer {token}"}, + json={ + "patient_cpf": patient_cpf, + "cid": "A001", + "ciap": None, + "clinical_status": "resolved", + "category": "encounter-diagnosis", + "date": "2024-01-11T16:20:09.832Z", + "raw_source_id": patientcondition_raw_source + } + ) + + assert response.status_code == 201 + assert 'id' in response.json() + +@pytest.mark.anyio +@pytest.mark.run(order=11) +async def test_get_stdpatientcondition(client: AsyncClient, token: str): + response = await client.get( + f"/raw/patientcondition", + headers={"Authorization": f"Bearer {token}"} + ) + + assert response.status_code == 200 + + json_response = response.json() + assert len(json_response) > 0 \ No newline at end of file