implement new sales forecast pipeline
This commit was merged in pull request #8.
This commit is contained in:
@@ -2,6 +2,7 @@ import importlib
|
||||
from datetime import datetime as Datetime
|
||||
from unittest.mock import patch
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
@@ -114,16 +115,6 @@ def test_parse_api_resp_to_df_empty():
|
||||
assert all(col in features for col in df.columns)
|
||||
|
||||
|
||||
# def test_parse_df_to_api_resp_ValidData(valid_df):
|
||||
# ret = fc._parse_df_to_api_resp(valid_df)
|
||||
# assert len(ret.daten) > 0
|
||||
|
||||
|
||||
# def test_parse_df_to_api_resp_InvalidData(invalid_df):
|
||||
# with pytest.raises(ValidationError):
|
||||
# _ = fc._parse_df_to_api_resp(invalid_df)
|
||||
|
||||
|
||||
def test_parse_df_to_results_ValidData(valid_results):
|
||||
ret = fc._parse_df_to_results(valid_results)
|
||||
assert len(ret.daten) > 0
|
||||
@@ -171,9 +162,12 @@ def test_preprocess_sales_FailOnTargetFeature(
|
||||
|
||||
def test_process_sales_Success(sales_data_real_preproc):
|
||||
data = sales_data_real_preproc.copy()
|
||||
# fc._preprocess_sales_per_customer()
|
||||
pipe = PipeResult(data, STATUS_HANDLER.SUCCESS)
|
||||
pipe = fc._process_sales(pipe)
|
||||
pipe = fc._process_sales(
|
||||
pipe,
|
||||
min_num_data_points=36,
|
||||
base_num_data_points_months=1,
|
||||
)
|
||||
|
||||
assert pipe.status == STATUS_HANDLER.SUCCESS
|
||||
assert pipe.data is not None
|
||||
@@ -183,9 +177,12 @@ def test_process_sales_Success(sales_data_real_preproc):
|
||||
def test_process_sales_FailTooFewPoints(sales_data_real_preproc):
|
||||
data = sales_data_real_preproc.copy()
|
||||
data = data.iloc[:20, :]
|
||||
# fc._preprocess_sales_per_customer()
|
||||
pipe = PipeResult(data, STATUS_HANDLER.SUCCESS)
|
||||
pipe = fc._process_sales(pipe)
|
||||
pipe = fc._process_sales(
|
||||
pipe,
|
||||
min_num_data_points=36,
|
||||
base_num_data_points_months=36,
|
||||
)
|
||||
|
||||
assert pipe.status != STATUS_HANDLER.SUCCESS
|
||||
assert pipe.status == STATUS_HANDLER.pipe_states.TOO_FEW_POINTS
|
||||
@@ -193,6 +190,55 @@ def test_process_sales_FailTooFewPoints(sales_data_real_preproc):
|
||||
assert pipe.results is None
|
||||
|
||||
|
||||
def test_process_sales_FailTooFewMonthPoints(sales_data_real_preproc):
|
||||
data = sales_data_real_preproc.copy()
|
||||
pipe = PipeResult(data, STATUS_HANDLER.SUCCESS)
|
||||
pipe = fc._process_sales(
|
||||
pipe,
|
||||
min_num_data_points=36,
|
||||
base_num_data_points_months=36,
|
||||
)
|
||||
|
||||
assert pipe.status != STATUS_HANDLER.SUCCESS
|
||||
assert pipe.status == STATUS_HANDLER.pipe_states.TOO_FEW_MONTH_POINTS
|
||||
assert pipe.data is None
|
||||
assert pipe.results is None
|
||||
|
||||
|
||||
def test_process_sales_FailNoReliableForecast(sales_data_real_preproc):
|
||||
data = sales_data_real_preproc.copy()
|
||||
data["betrag"] = 10000
|
||||
print(data["betrag"])
|
||||
data = data.iloc[:20000, :]
|
||||
pipe = PipeResult(data, STATUS_HANDLER.SUCCESS)
|
||||
|
||||
class PatchSearchCV:
|
||||
def __init__(self, *args, **kwargs) -> None:
|
||||
class Predictor:
|
||||
def predict(self, *args, **kwargs):
|
||||
return np.array([1, 1, 1, 1])
|
||||
|
||||
self.best_estimator_ = Predictor()
|
||||
|
||||
def fit(*args, **kwargs):
|
||||
pass
|
||||
|
||||
with patch(
|
||||
"delta_barth.analysis.forecast.RandomizedSearchCV",
|
||||
new=PatchSearchCV,
|
||||
):
|
||||
pipe = fc._process_sales(
|
||||
pipe,
|
||||
min_num_data_points=1,
|
||||
base_num_data_points_months=-100,
|
||||
)
|
||||
|
||||
assert pipe.status != STATUS_HANDLER.SUCCESS
|
||||
assert pipe.status == STATUS_HANDLER.pipe_states.NO_RELIABLE_FORECAST
|
||||
assert pipe.data is None
|
||||
assert pipe.results is None
|
||||
|
||||
|
||||
def test_postprocess_sales_Success(
|
||||
valid_results,
|
||||
):
|
||||
@@ -234,17 +280,18 @@ def test_export_on_fail():
|
||||
assert res.status.description == status.description
|
||||
|
||||
|
||||
@patch("delta_barth.analysis.forecast.SALES_BASE_NUM_DATAPOINTS_MONTHS", 1)
|
||||
def test_pipeline_sales_prognosis(exmpl_api_sales_prognosis_resp):
|
||||
def mock_request(*args, **kwargs): # pragma: no cover
|
||||
return exmpl_api_sales_prognosis_resp, STATUS_HANDLER.SUCCESS
|
||||
|
||||
with patch(
|
||||
"delta_barth.api.requests.get_sales_prognosis_data",
|
||||
new=mock_request,
|
||||
):
|
||||
importlib.reload(delta_barth.analysis.forecast)
|
||||
"delta_barth.analysis.forecast.get_sales_prognosis_data",
|
||||
# new=mock_request,
|
||||
) as mock:
|
||||
mock.return_value = exmpl_api_sales_prognosis_resp, STATUS_HANDLER.SUCCESS
|
||||
result = fc.pipeline_sales(None) # type: ignore
|
||||
|
||||
print(result)
|
||||
assert result.status == STATUS_HANDLER.SUCCESS
|
||||
assert len(result.response.daten) > 0
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@ def test_status_handler_raise_for_status_Success(status_hdlr):
|
||||
|
||||
def test_status_handler_raise_for_status_PredefinedErrors(status_hdlr):
|
||||
# data related errors (predefined)
|
||||
err_status = status_hdlr.pipe_states.BAD_QUALITY
|
||||
err_status = status_hdlr.pipe_states.NO_RELIABLE_FORECAST
|
||||
err_descr = err_status.description
|
||||
with pytest.raises(errors.UDataProcessingError):
|
||||
try:
|
||||
|
||||
@@ -9,6 +9,7 @@ from delta_barth import pipelines as pl
|
||||
from delta_barth.errors import STATUS_HANDLER
|
||||
|
||||
|
||||
@patch("delta_barth.analysis.forecast.SALES_BASE_NUM_DATAPOINTS_MONTHS", 1)
|
||||
def test_sales_prognosis_pipeline(exmpl_api_sales_prognosis_resp):
|
||||
with patch(
|
||||
"delta_barth.analysis.forecast.get_sales_prognosis_data",
|
||||
|
||||
Reference in New Issue
Block a user