Skip to content

Commit

Permalink
test: implementing basic tests to raw and std routers
Browse files Browse the repository at this point in the history
  • Loading branch information
TanookiVerde committed Jan 11, 2024
1 parent 6106449 commit 4ea07ca
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 12 deletions.
6 changes: 3 additions & 3 deletions api/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
40 changes: 36 additions & 4 deletions api/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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()

Expand All @@ -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",
Expand All @@ -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,
Expand Down Expand Up @@ -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)
7 changes: 2 additions & 5 deletions api/tests/test_main.py → api/tests/test_mrg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"}
)

Expand Down
72 changes: 72 additions & 0 deletions api/tests/test_raw.py
Original file line number Diff line number Diff line change
@@ -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
112 changes: 112 additions & 0 deletions api/tests/test_std.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4ea07ca

Please sign in to comment.