def test_forecast(reading, expected_forecast, monkeypatch, mock_ws): ''' 1. Assume that the forecaster module imports an external module weatherservice and then instantiates it in the constructor. 2. In this case you cannot inject a mock into forecaster's constructor. 3. Therefore, use a monkeypatching. Pytest will only patch a value for the duration of the test and then remove it. ''' WS = Mock(return_value=mock_ws) monkeypatch.setattr('forecaster.WeatherService', WS) forecaster = Forecaster() mock_ws.barometer.return_value = reading assert forecaster.forecast() == expected_forecast
from forecaster import Forecaster import pandas as pd import numpy as np covid = pd.read_csv("covid_19_data.csv") covid["ObservationDate"] = pd.to_datetime(covid["ObservationDate"]) covid.drop(columns=["SNo"], inplace=True) def incidence_denv(xdata, c1, c2, c3, c4=0): ans = [c1 * np.exp(-((t - c2)**2) / c3) + c4 for t in xdata] return ans caster = Forecaster(covid, "Mainland China", incidence_denv) print("Done 1") caster.forecast() caster.plot()