Skip to content

Commit

Permalink
Feat - Testing the MagicMock method to unittest the get requests for …
Browse files Browse the repository at this point in the history
…forecast methods
  • Loading branch information
davidusb-geek committed Oct 2, 2022
1 parent 61def2d commit f7da5b4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
Binary file added data/test_response_scrapper_method.pbz2
Binary file not shown.
4 changes: 4 additions & 0 deletions src/emhass/forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ def get_weather_forecast(self, method: Optional[str] = 'scrapper',
freq=freq_scrap).round(freq_scrap)
# Using the clearoutside webpage
response = get("https://clearoutside.com/forecast/"+str(round(self.lat, 2))+"/"+str(round(self.lon, 2))+"?desktop=true")
'''import bz2 # Uncomment to save a serialized response for tests
import _pickle as cPickle
with bz2.BZ2File("test_response_scrapper_method.pbz2", "w") as f:
cPickle.dump(response, f)'''
soup = BeautifulSoup(response.content, 'html.parser')
table = soup.find_all(id='day_0')[0]
list_names = table.find_all(class_='fc_detail_label')
Expand Down
35 changes: 24 additions & 11 deletions tests/test_forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
# -*- coding: utf-8 -*-

import unittest
from unittest.mock import MagicMock, patch
import pandas as pd
import pathlib, pickle, json, copy, yaml
import bz2
import _pickle as cPickle

from emhass.retrieve_hass import retrieve_hass
from emhass.forecast import forecast
Expand Down Expand Up @@ -66,15 +69,6 @@ def setUp(self):
}

def test_get_weather_forecast(self):
# self.assertTrue(self.df_input_data.isnull().sum().sum()==0)
# self.assertIsInstance(self.df_weather_scrap, type(pd.DataFrame()))
# self.assertTrue(col in self.df_weather_scrap.columns for col in ['ghi', 'dni', 'dhi', 'temp_air'])
# self.assertIsInstance(self.df_weather_scrap.index, pd.core.indexes.datetimes.DatetimeIndex)
# self.assertIsInstance(self.df_weather_scrap.index.dtype, pd.core.dtypes.dtypes.DatetimeTZDtype)
# self.assertEqual(self.df_weather_scrap.index.tz, self.fcst.time_zone)
# self.assertTrue(self.fcst.start_forecast < ts for ts in self.df_weather_scrap.index)
# self.assertEqual(len(self.df_weather_scrap),
# int(self.optim_conf['delta_forecast'].total_seconds()/3600/self.fcst.timeStep))
self.df_weather_csv = self.fcst.get_weather_forecast(method='csv')
self.assertEqual(self.fcst.weather_forecast_method, 'csv')
self.assertIsInstance(self.df_weather_csv, type(pd.DataFrame()))
Expand All @@ -91,6 +85,25 @@ def test_get_weather_forecast(self):
self.assertEqual(P_PV_forecast.index.tz, self.fcst.time_zone)
self.assertEqual(len(self.df_weather_csv), len(P_PV_forecast))

@patch('emhass.forecast.requests')
def test_get_weather_forecast_scrapper_method(self, mock_requests):
data = bz2.BZ2File(str(pathlib.Path(root+'/data/test_response_scrapper_method.pbz2')), "rb")
response = cPickle.load(data)
mock_response = MagicMock()
mock_response.content = copy.deepcopy(response.content)
mock_response.status_code = 200
# specify the return value of the get() method
mock_requests.get.return_value = mock_response
df_weather_scrap = self.fcst.get_weather_forecast(method='scrapper')
self.assertEqual(self.fcst.weather_forecast_method, 'scrapper')
self.assertIsInstance(df_weather_scrap, type(pd.DataFrame()))
self.assertIsInstance(df_weather_scrap.index, pd.core.indexes.datetimes.DatetimeIndex)
self.assertIsInstance(df_weather_scrap.index.dtype, pd.core.dtypes.dtypes.DatetimeTZDtype)
self.assertEqual(df_weather_scrap.index.tz, self.fcst.time_zone)
self.assertTrue(self.fcst.start_forecast < ts for ts in df_weather_scrap.index)
self.assertEqual(len(df_weather_scrap),
int(self.optim_conf['delta_forecast'].total_seconds()/3600/self.fcst.timeStep))

def test_get_forecasts_with_lists(self):
with open(root+'/config_emhass.yaml', 'r') as file:
params = yaml.load(file, Loader=yaml.FullLoader)
Expand Down Expand Up @@ -181,7 +194,7 @@ def test_get_power_from_weather(self):
self.plant_conf['strings_per_inverter'] = [1, 1]
self.fcst = forecast(self.retrieve_hass_conf, self.optim_conf, self.plant_conf,
None, root, logger, get_data_from_file=self.get_data_from_file)
df_weather_scrap = self.fcst.get_weather_forecast(method='scrapper')
df_weather_scrap = self.fcst.get_weather_forecast(method='csv')
P_PV_forecast = self.fcst.get_power_from_weather(df_weather_scrap)
self.assertIsInstance(P_PV_forecast, pd.core.series.Series)
self.assertIsInstance(P_PV_forecast.index, pd.core.indexes.datetimes.DatetimeIndex)
Expand All @@ -193,7 +206,7 @@ def test_get_power_from_weather(self):
df_input_data = self.input_data_dict['rh'].df_final.copy()
self.fcst = forecast(self.retrieve_hass_conf, self.optim_conf, self.plant_conf,
params, root, logger, get_data_from_file=self.get_data_from_file)
df_weather_scrap = self.fcst.get_weather_forecast(method='scrapper')
df_weather_scrap = self.fcst.get_weather_forecast(method='csv')
P_PV_forecast = self.fcst.get_power_from_weather(df_weather_scrap, set_mix_forecast=True, df_now=df_input_data)
self.assertIsInstance(P_PV_forecast, pd.core.series.Series)
self.assertIsInstance(P_PV_forecast.index, pd.core.indexes.datetimes.DatetimeIndex)
Expand Down

0 comments on commit f7da5b4

Please sign in to comment.