ExcelService (41 testes): - Geração de Excel com consultores - Parse de JSON, formatação de filtros - Extração de coordenações, consultoria, prêmios, titulação PDFService (44 testes): - Formatação de datas (completa e curta) - Ordenação por data com múltiplos formatos - Wrappers (ConsultorWrapper, DictWrapper) - Geração de ficha e PDF de equipe (mocked) Correções: - Ajuste nos testes Oracle para acessar params corretamente Cobertura: 54% → 66%
385 lines
13 KiB
Python
385 lines
13 KiB
Python
import pytest
|
|
from io import BytesIO
|
|
import json
|
|
|
|
from openpyxl import load_workbook
|
|
|
|
from src.application.services.excel_service import ExcelService
|
|
|
|
|
|
@pytest.fixture
|
|
def excel_service():
|
|
return ExcelService()
|
|
|
|
|
|
@pytest.fixture
|
|
def consultor_basico():
|
|
return {
|
|
"POSICAO": 1,
|
|
"ID_PESSOA": 12345,
|
|
"NOME": "MARIA SILVA SANTOS",
|
|
"PONTUACAO_TOTAL": 450.0,
|
|
"COMPONENTE_A": 200.0,
|
|
"COMPONENTE_C": 150.0,
|
|
"COMPONENTE_D": 50.0,
|
|
"COMPONENTE_E": 50.0,
|
|
"ATIVO": "S",
|
|
"ANOS_ATUACAO": 10.5,
|
|
"SELOS": "CA,CONS_ATIVO,AUTOR_GP",
|
|
"JSON_DETALHES": json.dumps({
|
|
"coordenacoes_capes": [
|
|
{"tipo": "CA", "area": "CIÊNCIAS DA COMPUTAÇÃO"}
|
|
],
|
|
"consultoria": {"situacao": "Atividade Contínua"},
|
|
"premiacoes": [
|
|
{"premio": "PCT", "tipo": "Grande Prêmio", "ano": 2023}
|
|
],
|
|
"titulacao": "Doutorado - USP (2010)"
|
|
})
|
|
}
|
|
|
|
|
|
@pytest.fixture
|
|
def consultor_inativo():
|
|
return {
|
|
"POSICAO": 100,
|
|
"ID_PESSOA": 99999,
|
|
"NOME": "JOÃO PEREIRA",
|
|
"PONTUACAO_TOTAL": 100.0,
|
|
"COMPONENTE_A": 0.0,
|
|
"COMPONENTE_C": 100.0,
|
|
"COMPONENTE_D": 0.0,
|
|
"COMPONENTE_E": 0.0,
|
|
"ATIVO": "N",
|
|
"ANOS_ATUACAO": 3.0,
|
|
"SELOS": "",
|
|
"JSON_DETALHES": None
|
|
}
|
|
|
|
|
|
class TestGerarRankingExcel:
|
|
|
|
def test_gerar_excel_lista_vazia(self, excel_service):
|
|
resultado = excel_service.gerar_ranking_excel([])
|
|
|
|
assert isinstance(resultado, bytes)
|
|
assert len(resultado) > 0
|
|
|
|
wb = load_workbook(BytesIO(resultado))
|
|
ws = wb.active
|
|
assert ws.title == "Ranking Consultores"
|
|
|
|
def test_gerar_excel_um_consultor(self, excel_service, consultor_basico):
|
|
resultado = excel_service.gerar_ranking_excel([consultor_basico])
|
|
|
|
wb = load_workbook(BytesIO(resultado))
|
|
ws = wb.active
|
|
|
|
assert ws["A1"].value == "RANKING DE CONSULTORES CAPES"
|
|
assert ws["A5"].value == 1
|
|
assert ws["B5"].value == 12345
|
|
assert ws["C5"].value == "MARIA SILVA SANTOS"
|
|
assert ws["D5"].value == 450.0
|
|
|
|
def test_gerar_excel_multiplos_consultores(self, excel_service, consultor_basico, consultor_inativo):
|
|
consultores = [consultor_basico, consultor_inativo]
|
|
resultado = excel_service.gerar_ranking_excel(consultores)
|
|
|
|
wb = load_workbook(BytesIO(resultado))
|
|
ws = wb.active
|
|
|
|
assert ws["C5"].value == "MARIA SILVA SANTOS"
|
|
assert ws["C6"].value == "JOÃO PEREIRA"
|
|
|
|
def test_gerar_excel_com_filtros(self, excel_service, consultor_basico):
|
|
filtros = {"ativo": True, "selos": ["CA", "CONS_ATIVO"]}
|
|
resultado = excel_service.gerar_ranking_excel([consultor_basico], filtros_aplicados=filtros)
|
|
|
|
wb = load_workbook(BytesIO(resultado))
|
|
ws = wb.active
|
|
|
|
subtitulo = ws["A2"].value
|
|
assert "Ativos" in subtitulo
|
|
assert "CA" in subtitulo
|
|
|
|
def test_gerar_excel_consultor_ativo(self, excel_service, consultor_basico):
|
|
resultado = excel_service.gerar_ranking_excel([consultor_basico])
|
|
|
|
wb = load_workbook(BytesIO(resultado))
|
|
ws = wb.active
|
|
|
|
assert ws["I5"].value == "Ativo"
|
|
|
|
def test_gerar_excel_consultor_inativo(self, excel_service, consultor_inativo):
|
|
resultado = excel_service.gerar_ranking_excel([consultor_inativo])
|
|
|
|
wb = load_workbook(BytesIO(resultado))
|
|
ws = wb.active
|
|
|
|
assert ws["I5"].value == "Inativo"
|
|
|
|
def test_gerar_excel_extrai_coordenacoes(self, excel_service, consultor_basico):
|
|
resultado = excel_service.gerar_ranking_excel([consultor_basico])
|
|
|
|
wb = load_workbook(BytesIO(resultado))
|
|
ws = wb.active
|
|
|
|
coord_cell = ws["L5"].value
|
|
assert "CA" in coord_cell
|
|
assert "CIÊNCIAS DA COMPUTAÇÃO" in coord_cell
|
|
|
|
def test_gerar_excel_extrai_consultoria(self, excel_service, consultor_basico):
|
|
resultado = excel_service.gerar_ranking_excel([consultor_basico])
|
|
|
|
wb = load_workbook(BytesIO(resultado))
|
|
ws = wb.active
|
|
|
|
assert ws["M5"].value == "Atividade Contínua"
|
|
|
|
def test_gerar_excel_extrai_premios(self, excel_service, consultor_basico):
|
|
resultado = excel_service.gerar_ranking_excel([consultor_basico])
|
|
|
|
wb = load_workbook(BytesIO(resultado))
|
|
ws = wb.active
|
|
|
|
premios_cell = ws["N5"].value
|
|
assert "PCT" in premios_cell
|
|
assert "2023" in premios_cell
|
|
|
|
def test_gerar_excel_extrai_titulacao(self, excel_service, consultor_basico):
|
|
resultado = excel_service.gerar_ranking_excel([consultor_basico])
|
|
|
|
wb = load_workbook(BytesIO(resultado))
|
|
ws = wb.active
|
|
|
|
assert "Doutorado" in ws["O5"].value
|
|
|
|
def test_gerar_excel_selos_formatados(self, excel_service, consultor_basico):
|
|
resultado = excel_service.gerar_ranking_excel([consultor_basico])
|
|
|
|
wb = load_workbook(BytesIO(resultado))
|
|
ws = wb.active
|
|
|
|
selos_cell = ws["K5"].value
|
|
assert "CA" in selos_cell
|
|
assert ", " in selos_cell
|
|
|
|
|
|
class TestParseJsonDetalhes:
|
|
|
|
def test_parse_json_string_valido(self, excel_service):
|
|
json_str = '{"coordenacoes_capes": [{"tipo": "CA"}]}'
|
|
resultado = excel_service._parse_json_detalhes(json_str)
|
|
|
|
assert resultado == {"coordenacoes_capes": [{"tipo": "CA"}]}
|
|
|
|
def test_parse_json_none(self, excel_service):
|
|
resultado = excel_service._parse_json_detalhes(None)
|
|
assert resultado == {}
|
|
|
|
def test_parse_json_string_vazia(self, excel_service):
|
|
resultado = excel_service._parse_json_detalhes("")
|
|
assert resultado == {}
|
|
|
|
def test_parse_json_invalido(self, excel_service):
|
|
resultado = excel_service._parse_json_detalhes("{invalid json")
|
|
assert resultado == {}
|
|
|
|
def test_parse_json_dict_direto(self, excel_service):
|
|
dados = {"coordenacoes_capes": []}
|
|
resultado = excel_service._parse_json_detalhes(dados)
|
|
assert resultado == dados
|
|
|
|
def test_parse_json_file_like_object(self, excel_service):
|
|
class MockLob:
|
|
def read(self):
|
|
return '{"test": "value"}'
|
|
|
|
resultado = excel_service._parse_json_detalhes(MockLob())
|
|
assert resultado == {"test": "value"}
|
|
|
|
|
|
class TestFormatarFiltros:
|
|
|
|
def test_formatar_filtros_ativo_true(self, excel_service):
|
|
filtros = {"ativo": True}
|
|
resultado = excel_service._formatar_filtros(filtros)
|
|
assert resultado == "Ativos"
|
|
|
|
def test_formatar_filtros_ativo_false(self, excel_service):
|
|
filtros = {"ativo": False}
|
|
resultado = excel_service._formatar_filtros(filtros)
|
|
assert resultado == "Inativos"
|
|
|
|
def test_formatar_filtros_com_selos(self, excel_service):
|
|
filtros = {"selos": ["CA", "CAJ"]}
|
|
resultado = excel_service._formatar_filtros(filtros)
|
|
assert "Selos: CA, CAJ" in resultado
|
|
|
|
def test_formatar_filtros_combinados(self, excel_service):
|
|
filtros = {"ativo": True, "selos": ["CA"]}
|
|
resultado = excel_service._formatar_filtros(filtros)
|
|
assert "Ativos" in resultado
|
|
assert "Selos: CA" in resultado
|
|
|
|
def test_formatar_filtros_vazio(self, excel_service):
|
|
filtros = {}
|
|
resultado = excel_service._formatar_filtros(filtros)
|
|
assert resultado == ""
|
|
|
|
|
|
class TestExtrairCoordenacoesResumo:
|
|
|
|
def test_extrair_coordenacoes_vazio(self, excel_service):
|
|
resultado = excel_service._extrair_coordenacoes_resumo({})
|
|
assert resultado == ""
|
|
|
|
def test_extrair_coordenacoes_lista_vazia(self, excel_service):
|
|
resultado = excel_service._extrair_coordenacoes_resumo({"coordenacoes_capes": []})
|
|
assert resultado == ""
|
|
|
|
def test_extrair_coordenacoes_com_tipo_e_area(self, excel_service):
|
|
detalhes = {
|
|
"coordenacoes_capes": [
|
|
{"tipo": "CA", "area": "MATEMÁTICA"}
|
|
]
|
|
}
|
|
resultado = excel_service._extrair_coordenacoes_resumo(detalhes)
|
|
assert resultado == "CA: MATEMÁTICA"
|
|
|
|
def test_extrair_coordenacoes_apenas_tipo(self, excel_service):
|
|
detalhes = {
|
|
"coordenacoes_capes": [
|
|
{"tipo": "CAJ"}
|
|
]
|
|
}
|
|
resultado = excel_service._extrair_coordenacoes_resumo(detalhes)
|
|
assert resultado == "CAJ"
|
|
|
|
def test_extrair_coordenacoes_multiplas(self, excel_service):
|
|
detalhes = {
|
|
"coordenacoes_capes": [
|
|
{"tipo": "CA", "area": "FÍSICA"},
|
|
{"tipo": "CAJ", "area": "QUÍMICA"}
|
|
]
|
|
}
|
|
resultado = excel_service._extrair_coordenacoes_resumo(detalhes)
|
|
assert "CA: FÍSICA" in resultado
|
|
assert "CAJ: QUÍMICA" in resultado
|
|
assert ";" in resultado
|
|
|
|
def test_extrair_coordenacoes_maximo_tres(self, excel_service):
|
|
detalhes = {
|
|
"coordenacoes_capes": [
|
|
{"tipo": "CA", "area": "A"},
|
|
{"tipo": "CAJ", "area": "B"},
|
|
{"tipo": "CAM", "area": "C"},
|
|
{"tipo": "CAJ_MP", "area": "D"},
|
|
]
|
|
}
|
|
resultado = excel_service._extrair_coordenacoes_resumo(detalhes)
|
|
assert "CA: A" in resultado
|
|
assert "CAJ: B" in resultado
|
|
assert "CAM: C" in resultado
|
|
assert "CAJ_MP" not in resultado
|
|
|
|
|
|
class TestExtrairSituacaoConsultoria:
|
|
|
|
def test_extrair_situacao_vazio(self, excel_service):
|
|
resultado = excel_service._extrair_situacao_consultoria({})
|
|
assert resultado == ""
|
|
|
|
def test_extrair_situacao_sem_consultoria(self, excel_service):
|
|
resultado = excel_service._extrair_situacao_consultoria({"consultoria": None})
|
|
assert resultado == ""
|
|
|
|
def test_extrair_situacao_ativa(self, excel_service):
|
|
detalhes = {"consultoria": {"situacao": "Atividade Contínua"}}
|
|
resultado = excel_service._extrair_situacao_consultoria(detalhes)
|
|
assert resultado == "Atividade Contínua"
|
|
|
|
|
|
class TestExtrairPremiosResumo:
|
|
|
|
def test_extrair_premios_vazio(self, excel_service):
|
|
resultado = excel_service._extrair_premios_resumo({})
|
|
assert resultado == ""
|
|
|
|
def test_extrair_premios_lista_vazia(self, excel_service):
|
|
resultado = excel_service._extrair_premios_resumo({"premiacoes": []})
|
|
assert resultado == ""
|
|
|
|
def test_extrair_premio_completo(self, excel_service):
|
|
detalhes = {
|
|
"premiacoes": [
|
|
{"premio": "PCT", "tipo": "Grande Prêmio", "ano": 2023}
|
|
]
|
|
}
|
|
resultado = excel_service._extrair_premios_resumo(detalhes)
|
|
assert "PCT" in resultado
|
|
assert "Grande Prêmio" in resultado
|
|
assert "2023" in resultado
|
|
|
|
def test_extrair_premio_apenas_nome(self, excel_service):
|
|
detalhes = {
|
|
"premiacoes": [{"premio": "PCT"}]
|
|
}
|
|
resultado = excel_service._extrair_premios_resumo(detalhes)
|
|
assert resultado == "PCT"
|
|
|
|
def test_extrair_premios_mais_de_tres(self, excel_service):
|
|
detalhes = {
|
|
"premiacoes": [
|
|
{"premio": "A"},
|
|
{"premio": "B"},
|
|
{"premio": "C"},
|
|
{"premio": "D"},
|
|
{"premio": "E"},
|
|
]
|
|
}
|
|
resultado = excel_service._extrair_premios_resumo(detalhes)
|
|
assert "A" in resultado
|
|
assert "B" in resultado
|
|
assert "C" in resultado
|
|
assert "+2 outros" in resultado
|
|
|
|
|
|
class TestExtrairTitulacao:
|
|
|
|
def test_extrair_titulacao_vazio(self, excel_service):
|
|
resultado = excel_service._extrair_titulacao({})
|
|
assert resultado == ""
|
|
|
|
def test_extrair_titulacao_string_direta(self, excel_service):
|
|
detalhes = {"titulacao": "Doutorado - UNICAMP (2015)"}
|
|
resultado = excel_service._extrair_titulacao(detalhes)
|
|
assert resultado == "Doutorado - UNICAMP (2015)"
|
|
|
|
def test_extrair_titulacao_do_lattes(self, excel_service):
|
|
detalhes = {
|
|
"lattes": {
|
|
"titulacoes": [
|
|
{"grau": "Doutorado", "ies_sigla": "USP", "ano": 2010}
|
|
]
|
|
}
|
|
}
|
|
resultado = excel_service._extrair_titulacao(detalhes)
|
|
assert "Doutorado" in resultado
|
|
assert "USP" in resultado
|
|
assert "2010" in resultado
|
|
|
|
def test_extrair_titulacao_lattes_apenas_grau(self, excel_service):
|
|
detalhes = {
|
|
"lattes": {
|
|
"titulacoes": [{"grau": "Mestrado"}]
|
|
}
|
|
}
|
|
resultado = excel_service._extrair_titulacao(detalhes)
|
|
assert resultado == "Mestrado"
|
|
|
|
def test_extrair_titulacao_lattes_vazio(self, excel_service):
|
|
detalhes = {"lattes": {"titulacoes": []}}
|
|
resultado = excel_service._extrair_titulacao(detalhes)
|
|
assert resultado == ""
|