Skip to content

Commit

Permalink
feat: simplifica testes unitários
Browse files Browse the repository at this point in the history
  • Loading branch information
nathansouzaa3 committed Sep 17, 2024
1 parent 30c9fa4 commit 97b76b4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
INTERIM_DATA_DIR = DATA_DIR / "interim"

MODELS_DIR = BASE_DIR / "artifacts" / "models"

NOTEBOOKS_DIR = BASE_DIR / "notebooks"
REPORTS_DIR = BASE_DIR / "reports"
FIGURES_DIR = REPORTS_DIR / "figures"
Expand All @@ -24,3 +23,4 @@
DOCS_DIR = BASE_DIR / "docs"
TESTS_DIR = BASE_DIR / "tests"
UI_DIR = BASE_DIR / "ui"
TEST_LOAD_DATA_CSV = TESTS_DIR / "test_load_data.csv"
27 changes: 12 additions & 15 deletions tests/test_features.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import pytest
import pandas as pd
from unittest.mock import patch, mock_open
from src.pipelines.features import load_data, preprocess, preprocess_data
from config.settings import TEST_LOAD_DATA_CSV


def test_load_data_success():
# Dados CSV fictícios para simular a leitura do arquivo
csv_data = "5.1,3.5,1.4,0.2,setosa\n4.9,3.0,1.4,0.2,setosa"
# Carrega os dados usando a função load_data
df = load_data(TEST_LOAD_DATA_CSV)

# Usar mock_open para simular a leitura do arquivo CSV
with patch("builtins.open", mock_open(read_data=csv_data)):
df = load_data("fake_path.csv")

# Verificar se o DataFrame foi carregado corretamente
# DataFrame esperado
expected_df = pd.DataFrame(
{
"sepal_length": [5.1, 4.9],
Expand Down Expand Up @@ -41,17 +37,18 @@ def test_preprocess_data_no_species_column():
preprocess_data(df)


def test_preprocess_failure(mocker):
mock_echo = mocker.patch("typer.echo")

def test_preprocess_failure(capfd):
# Chamar a função preprocess e garantir que uma exceção seja levantada
with pytest.raises(SystemExit):
preprocess(
input_file="fake_input.csv",
output_train_file="fake_train.csv",
output_test_file="fake_test.csv",
)
# Verificar se a mensagem de erro foi exibida
mock_echo.assert_called_once_with(
"Falha ao carregar os dados. Arquivo não encontrado.", err=True
)

# Capturar a saída de erro (stderr)
captured = capfd.readouterr()
print(captured)

# Verificar se a mensagem de erro está no stderr
assert "Falha ao carregar os dados. Arquivo não encontrado." in captured.err
2 changes: 2 additions & 0 deletions tests/test_load_data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
5.1,3.5,1.4,0.2,setosa
4.9,3.0,1.4,0.2,setosa

0 comments on commit 97b76b4

Please sign in to comment.