335 lines
15 KiB
Python
335 lines
15 KiB
Python
import pytest
|
|
from httpx import AsyncClient
|
|
from fastapi import status
|
|
|
|
BASE_URL = "/api/endereco"
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_route_exists(client: AsyncClient):
|
|
response = await client.post(f"{BASE_URL}/get_all")
|
|
assert response.status_code != 404 # Certifica-se de que a rota existe
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.usuarios_permitidos(["admin", "estoque"])
|
|
async def test_create_one(client: AsyncClient, uuid_store_tipo_endereco: dict, uuid_store_pessoa: dict,
|
|
uuid_store_endereco: dict):
|
|
response = await client.post(f"{BASE_URL}/add_one",
|
|
json={"endereco_pessoa_status": True,
|
|
"endereco_pessoa_descricao": "Endereço Descrição 1",
|
|
"endereco_pessoa_numero": "123",
|
|
"endereco_pessoa_complemento": "Complemento 1",
|
|
"endereco_pessoa_cep": "00000000",
|
|
"fk_tipo_endereco_uuid": uuid_store_tipo_endereco["uuid_6"],
|
|
"fk_pessoa_uuid": uuid_store_pessoa["uuid_6"]})
|
|
assert response.status_code == 201
|
|
uuid_store_endereco["uuid_3"] = response.json()["uuid"]
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.usuarios_permitidos(["admin", "estoque"])
|
|
async def test_create_many(client: AsyncClient, uuid_store_tipo_endereco: dict, uuid_store_pessoa: dict,
|
|
uuid_store_endereco: dict, ):
|
|
response = await client.post(f"{BASE_URL}/add_many", json=[
|
|
{"endereco_pessoa_status": True,
|
|
"endereco_pessoa_descricao": "Endereço Descrição 2",
|
|
"endereco_pessoa_numero": "123",
|
|
"endereco_pessoa_complemento": "Complemento 2",
|
|
"endereco_pessoa_cep": "00000000",
|
|
"fk_tipo_endereco_uuid": uuid_store_tipo_endereco["uuid_6"],
|
|
"fk_pessoa_uuid": uuid_store_pessoa["uuid_6"]},
|
|
{"endereco_pessoa_status": True,
|
|
"endereco_pessoa_descricao": "Endereço Descrição 3",
|
|
"endereco_pessoa_numero": "123",
|
|
"endereco_pessoa_complemento": "Complemento 3",
|
|
"endereco_pessoa_cep": "00000000",
|
|
"fk_tipo_endereco_uuid": uuid_store_tipo_endereco["uuid_6"],
|
|
"fk_pessoa_uuid": uuid_store_pessoa["uuid_6"]},
|
|
{"endereco_pessoa_status": True,
|
|
"endereco_pessoa_descricao": "Endereço Descrição 4",
|
|
"endereco_pessoa_numero": "123",
|
|
"endereco_pessoa_complemento": "Complemento 4",
|
|
"endereco_pessoa_cep": "00000000",
|
|
"fk_tipo_endereco_uuid": uuid_store_tipo_endereco["uuid_6"],
|
|
"fk_pessoa_uuid": uuid_store_pessoa["uuid_6"]},
|
|
{"endereco_pessoa_status": True,
|
|
"endereco_pessoa_descricao": "Endereço Descrição 5",
|
|
"endereco_pessoa_numero": "123",
|
|
"endereco_pessoa_complemento": "Complemento 5",
|
|
"endereco_pessoa_cep": "00000000",
|
|
"fk_tipo_endereco_uuid": uuid_store_tipo_endereco["uuid_6"],
|
|
"fk_pessoa_uuid": uuid_store_pessoa["uuid_6"]},
|
|
{"endereco_pessoa_status": True,
|
|
"endereco_pessoa_descricao": "Endereço Descrição 6",
|
|
"endereco_pessoa_numero": "123",
|
|
"endereco_pessoa_complemento": "Complemento 6",
|
|
"endereco_pessoa_cep": "00000000",
|
|
"fk_tipo_endereco_uuid": uuid_store_tipo_endereco["uuid_6"],
|
|
"fk_pessoa_uuid": uuid_store_pessoa["uuid_6"]}
|
|
])
|
|
assert response.status_code == 201
|
|
data = response.json()
|
|
assert len(data) == 5
|
|
uuid_store_endereco["uuid_4"] = data[0]["uuid"]
|
|
uuid_store_endereco["uuid_5"] = data[1]["uuid"]
|
|
uuid_store_endereco["uuid_6"] = data[2]["uuid"]
|
|
uuid_store_endereco["uuid_7"] = data[3]["uuid"]
|
|
uuid_store_endereco["uuid_8"] = data[4]["uuid"]
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.usuarios_permitidos(["admin", "estoque"])
|
|
async def test_get_all(client: AsyncClient):
|
|
response = await client.post(f"{BASE_URL}/get_all")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert isinstance(data, list)
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.usuarios_permitidos(["admin", "estoque"])
|
|
async def test_get_many(client: AsyncClient, uuid_store_endereco: dict):
|
|
uuids = [uuid_store_endereco["uuid_3"], uuid_store_endereco["uuid_4"]]
|
|
response = await client.post(f"{BASE_URL}/get_many", json={"uuids": uuids})
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert isinstance(data, list)
|
|
assert len(data) == 2
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.usuarios_permitidos(["admin", "estoque"])
|
|
async def test_get_one(client: AsyncClient, uuid_store_endereco: dict):
|
|
response = await client.post(f"{BASE_URL}/get_one", json={"uuid": uuid_store_endereco["uuid_3"]})
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert "uuid" in data
|
|
assert data["uuid"] == uuid_store_endereco["uuid_3"]
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.usuarios_permitidos(["admin", "estoque"])
|
|
async def test_update_one_existing_item(client: AsyncClient, uuid_store_endereco: dict, uuid_store_tipo_endereco: dict,
|
|
uuid_store_pessoa: dict):
|
|
response = await client.put(f"{BASE_URL}/update_one",
|
|
json={"uuid": uuid_store_endereco["uuid_8"],
|
|
"endereco_pessoa_status": True,
|
|
"endereco_pessoa_descricao": "Update Endereço Descrição 6",
|
|
"endereco_pessoa_numero": "123",
|
|
"endereco_pessoa_complemento": "Update Complemento 6",
|
|
"endereco_pessoa_cep": "00000000",
|
|
"fk_tipo_endereco_uuid": uuid_store_tipo_endereco["uuid_6"],
|
|
"fk_pessoa_uuid": uuid_store_pessoa["uuid_6"]})
|
|
assert response.status_code == 201
|
|
data = response.json()
|
|
assert data["uuid"] == uuid_store_endereco["uuid_8"]
|
|
assert data["endereco_pessoa_descricao"] == "Update Endereço Descrição 6"
|
|
assert data["endereco_pessoa_complemento"] == "Update Complemento 6"
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.usuarios_permitidos(["admin", "estoque"])
|
|
async def test_update_many_existing_item(client: AsyncClient, uuid_store_endereco: dict, uuid_store_tipo_endereco: dict,
|
|
uuid_store_pessoa: dict):
|
|
response = await client.put(f"{BASE_URL}/update_many", json=[
|
|
{"uuid": uuid_store_endereco["uuid_7"], "endereco_pessoa_status": True,
|
|
"endereco_pessoa_descricao": "Update Endereço Descrição 5",
|
|
"endereco_pessoa_numero": "123",
|
|
"endereco_pessoa_complemento": "Update Complemento 5",
|
|
"endereco_pessoa_cep": "00000000",
|
|
"fk_tipo_endereco_uuid": uuid_store_tipo_endereco["uuid_6"],
|
|
"fk_pessoa_uuid": uuid_store_pessoa["uuid_6"]},
|
|
{"uuid": uuid_store_endereco["uuid_6"], "endereco_pessoa_status": True,
|
|
"endereco_pessoa_descricao": "Update Endereço Descrição 4",
|
|
"endereco_pessoa_numero": "123",
|
|
"endereco_pessoa_complemento": "Update Complemento 4",
|
|
"endereco_pessoa_cep": "00000000",
|
|
"fk_tipo_endereco_uuid": uuid_store_tipo_endereco["uuid_6"],
|
|
"fk_pessoa_uuid": uuid_store_pessoa["uuid_6"]},
|
|
])
|
|
assert response.status_code == 201
|
|
data = response.json()
|
|
assert len(data) == 2
|
|
# Verificando se os valores atualizados são os corretos
|
|
assert data[0]["uuid"] == uuid_store_endereco["uuid_6"]
|
|
assert data[0]["endereco_pessoa_descricao"] == "Update Endereço Descrição 4"
|
|
assert data[0]["endereco_pessoa_complemento"] == "Update Complemento 4"
|
|
assert data[1]["uuid"] == uuid_store_endereco["uuid_7"]
|
|
assert data[1]["endereco_pessoa_descricao"] == "Update Endereço Descrição 5"
|
|
assert data[1]["endereco_pessoa_complemento"] == "Update Complemento 5"
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.usuarios_permitidos(["admin", "estoque"])
|
|
async def test_delete_one_item(client: AsyncClient, uuid_store_endereco):
|
|
response = await client.request(
|
|
method="DELETE",
|
|
url=f"{BASE_URL}/delete_one",
|
|
json={"uuid": uuid_store_endereco["uuid_3"]}
|
|
)
|
|
assert response.status_code == 204
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.usuarios_permitidos(["admin", "estoque"])
|
|
async def test_delete_many_items(client: AsyncClient, uuid_store_endereco):
|
|
uuids = [uuid_store_endereco["uuid_4"], uuid_store_endereco["uuid_5"]]
|
|
response = await client.request(
|
|
method="DELETE",
|
|
url=f"{BASE_URL}/delete_many",
|
|
json={"uuids": uuids} # Envia o corpo da solicitação como JSON
|
|
)
|
|
assert response.status_code == 204
|
|
|
|
|
|
# Testes com dados inválidos
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.usuarios_permitidos(["admin", "estoque"])
|
|
async def test_delete_one_non_existent_item(client: AsyncClient, uuid_store_relacao_comercial):
|
|
# Tentando deletar novamente o primeiro item já deletado
|
|
response = await client.request(
|
|
method="DELETE",
|
|
url=f"{BASE_URL}/delete_one",
|
|
json={"uuid": uuid_store_relacao_comercial["uuid_1"]}
|
|
)
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.usuarios_permitidos(["admin", "estoque"])
|
|
async def test_get_one_non_existent_item(client: AsyncClient, uuid_store_relacao_comercial):
|
|
# Tentando buscar um item deletado
|
|
response = await client.request(
|
|
method="POST",
|
|
url=f"{BASE_URL}/get_one",
|
|
json={"uuid": uuid_store_relacao_comercial["uuid_3"]}
|
|
)
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.usuarios_permitidos(["admin", "estoque"])
|
|
async def test_delete_many_non_existent_item(client: AsyncClient, uuid_store_relacao_comercial):
|
|
uuids = [uuid_store_relacao_comercial["uuid_1"], uuid_store_relacao_comercial["uuid_2"],
|
|
uuid_store_relacao_comercial["uuid_5"]]
|
|
response = await client.request(
|
|
method="DELETE",
|
|
url=f"{BASE_URL}/delete_many",
|
|
json={"uuids": uuids} # Envia o corpo da solicitação como JSON
|
|
)
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.usuarios_permitidos(["admin", "estoque"])
|
|
async def test_update_one_non_existing_item(client: AsyncClient, uuid_store_tipo_endereco: dict,
|
|
uuid_store_endereco: dict, uuid_store_pessoa: dict):
|
|
# Atualizando o segundo item
|
|
response = await client.request(
|
|
method="PUT",
|
|
url=f"{BASE_URL}/update_one",
|
|
json={"uuid": uuid_store_endereco["uuid_1"],
|
|
"endereco_pessoa_status": True,
|
|
"endereco_pessoa_descricao": "Update Endereço Descrição 4",
|
|
"endereco_pessoa_numero": "123",
|
|
"endereco_pessoa_complemento": "Update Complemento 4",
|
|
"endereco_pessoa_cep": "00000000",
|
|
"fk_tipo_endereco_uuid": uuid_store_tipo_endereco["uuid_6"],
|
|
"fk_pessoa_uuid": uuid_store_pessoa["uuid_6"]}
|
|
)
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.usuarios_permitidos(["admin", "estoque"])
|
|
async def test_update_many_non_existing_item(client: AsyncClient, uuid_store_endereco: dict, uuid_store_pessoa: dict,
|
|
uuid_store_tipo_endereco: dict):
|
|
# Atualizando o segundo e terceiro item
|
|
response = await client.request(
|
|
method="PUT",
|
|
url=f"{BASE_URL}/update_many",
|
|
json=[
|
|
{"uuid": uuid_store_endereco["uuid_1"],
|
|
"endereco_pessoa_status": True,
|
|
"endereco_pessoa_descricao": "Update Endereço Descrição 4",
|
|
"endereco_pessoa_numero": "123",
|
|
"endereco_pessoa_complemento": "Update Complemento 4",
|
|
"endereco_pessoa_cep": "00000000",
|
|
"fk_tipo_endereco_uuid": uuid_store_tipo_endereco["uuid_6"],
|
|
"fk_pessoa_uuid": uuid_store_pessoa["uuid_6"]},
|
|
{"uuid": uuid_store_endereco["uuid_2"],
|
|
"endereco_pessoa_status": True,
|
|
"endereco_pessoa_descricao": "Update Endereço Descrição 4",
|
|
"endereco_pessoa_numero": "123",
|
|
"endereco_pessoa_complemento": "Update Complemento 4",
|
|
"endereco_pessoa_cep": "00000000",
|
|
"fk_tipo_endereco_uuid": uuid_store_tipo_endereco["uuid_6"],
|
|
"fk_pessoa_uuid": uuid_store_pessoa["uuid_6"]}
|
|
]
|
|
)
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
|
|
|
|
|
# Teste com dados fora dos limites de tamanho
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.usuarios_permitidos(["admin", "estoque"])
|
|
async def test_create_one_min_length(client: AsyncClient, uuid_store_relacao_comercial: dict):
|
|
response = await client.post(f"{BASE_URL}/add_one",
|
|
json={"endereco_pessoa_descricao": "a"})
|
|
assert response.status_code == 422
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.usuarios_permitidos(["admin", "estoque"])
|
|
async def test_create_one_max_length(client: AsyncClient, uuid_store_relacao_comercial: dict):
|
|
response = await client.post(f"{BASE_URL}/add_one",
|
|
json={"endereco_pessoa_descricao": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
"aaaaaaaaaaa"})
|
|
assert response.status_code == 422
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.usuarios_permitidos(["admin", "estoque"])
|
|
async def test_create_many_max_and_min_length(client: AsyncClient):
|
|
response = await client.post(f"{BASE_URL}/add_many", json=[
|
|
{"endereco_pessoa_descricao": "aa"},
|
|
{"endereco_pessoa_descricao": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
|
|
{"endereco_pessoa_descricao": "aa"},
|
|
{"endereco_pessoa_descricao": "aa"}
|
|
|
|
])
|
|
assert response.status_code == 422
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.usuarios_permitidos(["admin", "estoque"])
|
|
async def test_update_one_existing_item_min_lenght(client: AsyncClient, uuid_store_endereco: dict):
|
|
response = await client.put(f"{BASE_URL}/update_one",
|
|
json={"uuid": uuid_store_endereco["uuid_8"],
|
|
"endereco_pessoa_descricao": "a"})
|
|
assert response.status_code == 422
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.usuarios_permitidos(["admin", "estoque"])
|
|
async def test_update_one_existing_item_max_lenght(client: AsyncClient, uuid_store_endereco: dict):
|
|
response = await client.put(f"{BASE_URL}/update_one",
|
|
json={"uuid": uuid_store_endereco["uuid_8"],
|
|
"endereco_pessoa_descricao": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
"aaaaaa"})
|
|
assert response.status_code == 422
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.usuarios_permitidos(["admin", "estoque"])
|
|
async def test_update_many_existing_item_max_and_min_length(client: AsyncClient, uuid_store_endereco: dict):
|
|
response = await client.put(f"{BASE_URL}/update_many", json=[
|
|
{"uuid": uuid_store_endereco["uuid_7"], "endereco_pessoa_descricao": "aa"},
|
|
{"uuid": uuid_store_endereco["uuid_6"],
|
|
"endereco_pessoa_descricao": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
|
|
])
|
|
assert response.status_code == 422
|